GraphQL无法接收mysql数据
我正在尝试在 Node js Express 服务器中将 GraphQL 与 mysql 一起使用。
但每次运行查询时都会收到此错误
,这是错误:
{
"errors": [
{
"message": "Expected Iterable, but did not find one for field \"RootQueryType.getAllGoals\".",
"locations": [
{
"line": 2,
"column": 3
}
],
"path": [
"getAllGoals"
]
}
],
"data": {
"getAllGoals": null
}
}
这是我的 Graphql 查询:
query {
getAllGoals {
title
progress
goal
}
}
我从“SELECT * FROM(我的表)”中得到了预期的结果,但当我尝试返回它时,它给了我一个错误对于来自解析器的 GraphQL,如下面的代码所示:
const RootQuery = new GraphQLObjectType({
name: "RootQueryType",
fields: {
getAllGoals: {
type: new GraphQLList(GoalType),
resolve(parent, args) {
return db.query("SELECT * FROM myTable", (err, result) => {
if (err) throw err
console.log(JSON.parse(JSON.stringify(result)))
return JSON.parse(JSON.stringify(result))
})
}
}
}
})
我已检查我的 GraphQLObjectType GoalType 中是否有任何冲突,但没有。
I am trying to use GraphQL with mysql in a node js express server.
but I am getting this error everytime I run my query
here is the error:
{
"errors": [
{
"message": "Expected Iterable, but did not find one for field \"RootQueryType.getAllGoals\".",
"locations": [
{
"line": 2,
"column": 3
}
],
"path": [
"getAllGoals"
]
}
],
"data": {
"getAllGoals": null
}
}
here is my Graphql Query:
query {
getAllGoals {
title
progress
goal
}
}
I get the result expected from "SELECT * FROM (my table)", but it gives me an error when I try to return it for GraphQL from the resolver as shown in the code below:
const RootQuery = new GraphQLObjectType({
name: "RootQueryType",
fields: {
getAllGoals: {
type: new GraphQLList(GoalType),
resolve(parent, args) {
return db.query("SELECT * FROM myTable", (err, result) => {
if (err) throw err
console.log(JSON.parse(JSON.stringify(result)))
return JSON.parse(JSON.stringify(result))
})
}
}
}
})
I have checked to see if I have any conflicts in my GraphQLObjectType GoalType, but I had none.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我已经修复了它,我只需要做出包含查询的承诺(如下所示):
I have fixed it, I just needed to make a promise that included the query (as shown below):
您的
resolve
方法返回db.query
返回的任何内容,但这不是 GraphQL 所期望的:它期望查询的结果或承诺就得到了这个结果。您可以使用util.promisify
以获得这样的承诺。如果您还想记录结果,请使用
.then
方法,如下所示:(JSON.parse(JSON.stringify(...)) 的目的是什么
?)Your
resolve
method returns whateverdb.query
returns, but this is not what GraphQL expects: It expects the result of the query, or a promise that resolves to this result. You can useutil.promisify
to obtain such a promise.If you also want to log the result, use a
.then
method as shown here:(What's the purpose of
JSON.parse(JSON.stringify(...))
?)