javascript - 哪问大神解释下GraphQL 里的mutation 查询 not found问题
怪我咯
怪我咯 2017-04-11 13:15:03
[JavaScript讨论组]

参考http://taobaofed.org/blog/201...
例子里的 GraphQL 教程
遇到 GraphQL schema里的定义的 mutation 查询 返回值无效
使用Postman 模拟post请求,body数据为mutation RootMutationType{updateCount},返回值为Not Found
mutation 查询的node后台执行语句为

graphql(schema, ctx.request.body).then((result) => {
        ctx.body=JSON.stringify(result);
})

result 结果值为{ data: { updateCount: 1 } }
貌似将其赋值给ctx.body是无效的。
控制台的请求日志为
--> POST /graphql/test 404 53ms -

query RootQueryType { count } 查询则不存在这个问题

是不是2种查询机制 存在差异

graphq版本"^0.7.1"
koa版本"^2.0.0-alpha.7"
koa-router版本"^7.0.1"

代码如下

//schema.js
import {
    GraphQLObjectType,
    GraphQLSchema,
    GraphQLInt
} from 'graphql';
let count = 0;
let schema = new GraphQLSchema({
    query: new GraphQLObjectType({
        name: 'RootQueryType',
        fields: {
            count: {
                type: GraphQLInt,
                description: '数量!',
                resolve: function () {
                    return count;
                }
            }
        }
    }),
    mutation: new GraphQLObjectType({
        name: 'RootMutationType',
        fields: {
            updateCount: {
                type: GraphQLInt,
                description: '更新数量',
                resolve: function () {
                    count += 1;
                    return count;
                }
            }
        }
    })
});
export default schema;
//路由
//graphqlTest.js
import router from 'koa-router';
import schema from '../model/schema';
import { graphql } from 'graphql';
var graphqlTest=router();

graphqlTest.post('/graphql/test', function (ctx, next) {
    graphql(schema, ctx.request.body).then((result) => {
    ctx.body=JSON.stringify(result);
  })
});
export default graphqlTest;
怪我咯
怪我咯

走同样的路,发现不同的人生

全部回复(1)
黄舟
graphqlTest.post('/graphql/test', function (ctx, next) {
    graphql(schema, ctx.request.body).then((result) => {
    ctx.body=JSON.stringify(result);
  })
}); 

将这个改成

graphqlTest.post('/graphql/test', async function (ctx, next) {
    await graphql(schema, ctx.request.body).then((result) => {
    ctx.body=JSON.stringify(result);
  })
}); 

就好了

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

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