必须在graphql中提供查询字符串

时间:2019-09-25 07:37:36

标签: node.js express graphql express-graphql

我正在用node做示例程序,graphql我完成了基本代码以返回一些我的代码是

const express = require('express');
const bodyParser = require('body-parser');
const graphqlHttp = require('express-graphql');
const { buildSchema } = require('graphql');

const app = express();

app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.use(bodyParser.text({ type: 'application/graphql' }));

app.use('/graphql',
    graphqlHttp({
        schema: buildSchema(`
    type RootQuery{
        events: [String!]!
    }
    type RootMutation{
        createEvent(name: String): String
    }
    schema {
        query: RootQuery
        mutation: RootMutation
    }
    `),
        rootValue: {
            events: () => {
                return ['coding', 'night coding', 'always coding'];
            },
            createEvent: (args) => {
                const eventName = args.name;
                return eventName;
            },
            graphiql: true
        }
    }));

app.listen(3000);   

我在访问localhost:3000 / graphql时遇到问题,浏览器的问题是{“错误”:[{“消息”:“必须提供查询字符串。”}]},我也检查了浏览器中的网络选项卡,发现请求不正确400错误,但仅用于/ graphql enter image description here。如果有任何错误,请纠正。我尝试了一些堆栈溢出解决方案,但未解决

1.
 app.use(bodyParser.text({ type: 'application/graphql' }));
2. 
app.use(/\/((?!graphql).)*/, bodyParser.urlencoded({ extended: true }));
app.use(/\/((?!graphql).)*/, bodyParser.json());
3.
Also checked Contant-type: application/json is going in header i need help on this

0 个答案:

没有答案