GraphQL无法接收mysql数据

发布于 2025-01-10 07:21:13 字数 1197 浏览 0 评论 0原文

我正在尝试在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

月野兔 2025-01-17 07:21:13

我已经修复了它,我只需要做出包含查询的承诺(如下所示):

async resolve(parent, args) {
                var p = new Promise((resolve, reject) => {
                    db.query("SELECT * FROM myTable", (err, result) => {
                        console.log(JSON.parse(JSON.stringify(result)))
                        resolve(JSON.parse(JSON.stringify(result)))
                    })
                })
                return p
            }

I have fixed it, I just needed to make a promise that included the query (as shown below):

async resolve(parent, args) {
                var p = new Promise((resolve, reject) => {
                    db.query("SELECT * FROM myTable", (err, result) => {
                        console.log(JSON.parse(JSON.stringify(result)))
                        resolve(JSON.parse(JSON.stringify(result)))
                    })
                })
                return p
            }
橘味果▽酱 2025-01-17 07:21:13

您的 resolve 方法返回 db.query 返回的任何内容,但这不是 GraphQL 所期望的:它期望查询的结果或承诺就得到了这个结果。您可以使用 util.promisify 以获得这样的承诺。

如果您还想记录结果,请使用 .then 方法,如下所示:(

resolve(parent, args) {
  return util.promisify(db.query)("SELECT * FROM myTable")
  .then(result => {
    console.log(result);
    return result;
  });
}

JSON.parse(JSON.stringify(...)) 的目的是什么 ?)

Your resolve method returns whatever db.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 use util.promisify to obtain such a promise.

If you also want to log the result, use a .then method as shown here:

resolve(parent, args) {
  return util.promisify(db.query)("SELECT * FROM myTable")
  .then(result => {
    console.log(result);
    return result;
  });
}

(What's the purpose of JSON.parse(JSON.stringify(...))?)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文