I am doing sample programs with node, graphql i done basic code to return some events my code is
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);
I had issue while hit localhost:3000/graphql is browser the issue is {"errors":[{"message":"Must provide query string."}]}, Also i checked network tab in browser seen bad request 400 error but is used /graphql only . If any i wrong please correct.I tried few stack overflow solutions but not resolved
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