尝试将graphql与oracle数据库连接并返回一些数据。 尝试这样做时,我看到数据正在返回(基于行数),但是数据显示为空
当我在控制台中通过函数打印数据库的输出时,我看到返回的结果作为数组对象没有任何问题。
我正在尝试确定解析器功能是否与无法将输出映射到graphQL的ClientType有关
我试图解析数据并更新各种格式的typeDefs,但似乎仍然无法工作
const graphql = require("graphql");
const oracledb = require("oracledb");
const {
GraphQLObjectType,
GraphQLString,
GraphQLInt,
GraphQLSchema,
GraphQLID,
GraphQLList
} = graphql;
async function getAllClientsHelper() {
let sql = "SELECT p.user_id as id, p.org_nm as orgnm FROM hr.userdetails p";
let conn = await oracledb.getConnection();
let result = await conn.execute(sql);
await conn.close();
let j = [];
for (let r of result.rows) {
j.push(r);
}
return j;
}
async function getOneClientHelper(id) {
let sql =
"SELECT p.user_id as id, p.org_nm as orgnm FROM hr.userdetails p where hr.user_id = :id";
let binds = [id];
let conn = await oracledb.getConnection();
let result = await conn.execute(sql, binds);
await conn.close();
return result.rows;
}
const ClientType = new GraphQLObjectType({
name: "Client",
fields: () => ({
id: {
type: GraphQLID
},
orgnm: {
type: GraphQLString
}
})
});
const RootQuery = new GraphQLObjectType({
name: "RootQueryType",
fields: {
client: {
type: ClientType,
args: {
id: {
type: GraphQLID
}
},
resolve(parent, args) {
// code to get the data from databse / source
return getOneClientHelper(args.id);
}
},
clients: {
type: new GraphQLList(ClientType),
resolve(parent, args) {
return getAllClientsHelper();
}
}
}
});
module.exports = new GraphQLSchema({
query: RootQuery
});
当我尝试从getOneClient()
上的控制台打印时,这里是输出
[1,测试机构]
我正在尝试查看以下针对选定客户端的查询输出
{
client(id: 1){
orgnm: "Test Organization"
}