Home  >  Q&A  >  body text

Unable to receive mysql data via GraphQL

<p>I'm trying to use GraphQL and mysql in a Node.js Express server. </p> <p>But every time I run the query I get the following error:</p> <p>The error message is as follows:</p> <pre class="brush:php;toolbar:false;">{ "errors": [ { "message": "Expected Iterable, but did not find one for field \"RootQueryType.getAllGoals\".", "locations": [ { "line": 2, "column": 3 } ], "path": [ "getAllGoals" ] } ], "data": { "getAllGoals": null } }</pre> <p>This is my GraphQL query: </p> <pre class="brush:php;toolbar:false;">query { getAllGoals { title progress goal } }</pre> <p>I get the expected result from "SELECT * FROM (my table)", but when I try to return it as a GraphQL resolver, it gives me an error like this: < ;/p> <pre class="brush:php;toolbar:false;">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)) }) } } } })</pre> <p>I've checked my GraphQLObjectType GoalType for any conflicts, but found nothing. </p>
P粉092778585P粉092778585418 days ago452

reply all(1)I'll reply

  • P粉340264283

    P粉3402642832023-08-30 12:30:40

    I already fixed it, I just had to create a promise containing the query (like 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
                }

    reply
    0
  • Cancelreply