Unable to receive mysql data via GraphQL
P粉092778585
P粉092778585 2023-08-29 18:02:24
0
1
460
<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粉092778585
P粉092778585

reply all(1)
P粉340264283

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
            }
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!