图形质量从Oracle DB中提取数据:查询响应结果集,但未出现在Graphql解析器窗口中 [英] GraphQL & Pulling Data from Oracle DB: Query Responds with the Resultset and Doesn't Come to The Graphql resolver Window
问题描述
我是GraphQL的新手.
I am new to GraphQL.
开始开发GraphQL应用程序以从oracle数据库中提取数据.
Started developing an GraphQL app to pull the data from oracle database.
这是一个非常简单的应用程序.查询以resultset
响应,并且可以在console.log
中看到结果;但是,它不会出现在graphql
窗口(响应/解析器窗口)中.引发错误
It is very simple application. The query responds with the resultset
and can be seen results in console.log
; however, it doesn't come to the graphql
window (response/resolver window). It throws the error
对于非User.email不能返回null
Cannot return null for non User.email
我在oracle连接中尝试了promise.不确定,为什么它不在GraphQL中显示数据.
I tried the promise in the oracle connection. Not sure, why it is not showing the data in GraphQL.
UserType.js
UserType.js
module.exports = new GraphQLObjectType({
name: 'User',
fields: () => {
return{
id: { type: GraphQLID },
email: { type: new GraphQLNonNull(GraphQLString) }
}
}
});
DBConnection.js
DBConnection.js
module.exports = oraPool => {
return {
getUsers(apiKey){
return oracledb.createPool(oraConfig).then(function() {
console.log("Connection Pool created");
return oracledb.getConnection().then(function(conn) {
return conn.execute(
//`SELECT 'User ' || JSON_OBJECT ('id' VALUE id, 'email' VALUE email) FROM users where id = :id`
`SELECT * FROM users WHERE id = :id`
,[apiKey]).then(result => {
//console.log(result.metaData);
console.log(humps.camelizeKeys(result.rows[0]));
conn.close();
return humps.camelizeKeys(result.rows[0]);
})
.catch(function(err) {
console.log(err.message);
return connection.close();
});
})
.catch(function(err) {
console.error(err.message);
});
})
}
}
}
Type.js
const RootQueryType = new GraphQLObjectType({
name: 'RootQueryType',
fields: {
user: {
type: UserType,
args: {
key: { type: new GraphQLNonNull(GraphQLString) }
},
resolve: (obj, args, { oraPool }) => {
return oradb(oraPool).getUsers(args.key);
}
}
}
});
推荐答案
这段代码很复杂,我建议从头开始.例如,您每次运行查询时都在调用createPool
.您应该在应用初始化期间创建池.
This code is pretty tangled, I recommend starting from scratch. For example, you're calling createPool
each time a query is run. You should create the pool during the initialization of your app instead.
尽管您说这将是一个简单的应用程序,但它可能会不断发展.从头开始创建GraphQL服务器并非易事.我建议通过join-monster带来一些帮助.不幸的是,join-monster不再被积极开发.但是它非常稳定,比从头开始要好得多.这是一个很好的工作原理概述: https://github.com/stems/join-monster/tree/master/src
Although you said this will be a simple app, it could always grow. Creating a from scratch GraphQL server isn't trivial. I recommend bringing in some help via join-monster. Unfortunately, join-monster is no longer being actively developed. But it's pretty stable and it's a lot better than starting from scratch. Here's a nice overview of how it works: https://github.com/stems/join-monster/tree/master/src
I recently did a talk on GraphQL which you can see here: https://www.slideshare.net/DanielMcGhan/intro-to-graphql-for-database-developers
对于该演示,我采用了一个简单的API模式,该模式在 https://www.dropbox.com/s/cnvyrlik7irtbwm/graphql-api.zip?dl=0
For the demo, I took a simple API pattern I describe in this blog series and adapted it to be a GraphQL server on the common EMP and DEPT demo tables. You can access the code here: https://www.dropbox.com/s/cnvyrlik7irtbwm/graphql-api.zip?dl=0
此外,我的另一位同事在这里谈论GraphQL: https://blogs.oracle.com/opal/demo:- graphql-with-node-oracledb
Also, another one of my colleagues talks about GraphQL here: https://blogs.oracle.com/opal/demo:-graphql-with-node-oracledb
这篇关于图形质量从Oracle DB中提取数据:查询响应结果集,但未出现在Graphql解析器窗口中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!