0

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 enter image description here. 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
SaranViji
  • 383
  • 2
  • 6
  • 17
  • 2
    Syntax error has been present in your code. you need to put graphiql: true outside the rootvalue not inside. your graphqlHttp should be ({ schema: buildSchema(), rootvalue: {...something}, graphiql: true}) – Indragith Sep 25 '19 at 10:23
  • Thanks buddy issue solved – SaranViji Sep 25 '19 at 11:05

0 Answers0