无法通过GraphQL接收mysql数据
P粉092778585
P粉092778585 2023-08-29 18:02:24
[MySQL讨论组]
<p>我正在尝试在一个Node.js Express服务器中使用GraphQL和mysql。</p> <p>但是每次运行查询时都会出现以下错误:</p> <p>错误信息如下:</p> <pre class="brush:php;toolbar:false;">{ &quot;errors&quot;: [ { &quot;message&quot;: &quot;Expected Iterable, but did not find one for field \&quot;RootQueryType.getAllGoals\&quot;.&quot;, &quot;locations&quot;: [ { &quot;line&quot;: 2, &quot;column&quot;: 3 } ], &quot;path&quot;: [ &quot;getAllGoals&quot; ] } ], &quot;data&quot;: { &quot;getAllGoals&quot;: null } }</pre> <p>这是我的GraphQL查询:</p> <pre class="brush:php;toolbar:false;">query { getAllGoals { title progress goal } }</pre> <p>我从&quot;SELECT * FROM (我的表)&quot;中得到了预期的结果,但是当我尝试将其作为GraphQL的resolver返回时,它给了我一个错误,如下所示:</p> <pre class="brush:php;toolbar:false;">const RootQuery = new GraphQLObjectType({ name: &quot;RootQueryType&quot;, fields: { getAllGoals: { type: new GraphQLList(GoalType), resolve(parent, args) { return db.query(&quot;SELECT * FROM myTable&quot;, (err, result) =&gt; { if (err) throw err console.log(JSON.parse(JSON.stringify(result))) return JSON.parse(JSON.stringify(result)) }) } } } })</pre> <p>我已经检查过我的GraphQLObjectType GoalType是否有任何冲突,但是没有发现任何问题。</p>
P粉092778585
P粉092778585

全部回复(1)
P粉340264283

我已经修复了,我只需要创建一个包含查询的promise(如下所示):

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
            }
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号