Node.js 必须在graphql中提供查询字符串
我正在用node、graphql编写示例程序,我编写了基本代码以返回一些事件,我的代码是Node.js 必须在graphql中提供查询字符串,node.js,express,graphql,express-graphql,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(b
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。如果有任何错误,请更正。我尝试了一些堆栈溢出解决方案,但没有解决
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
代码中出现语法错误。您需要将graphiql:true放在rootvalue之外而不是里面。您的graphqlHttp应该是({schema:buildSchema(),rootvalue:{…something},graphiql:true}),问题已经解决