louisssee
louisssee

Reputation: 15

Getting error {"errors":[{"message":"Must provide query string."}]}

I have this in my app.js

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

const app = express();

app.use(bodyParser.json());

app.use(
  '/myproject',
  graphqlHttp({
    schema: buildSchema(`
        type TypeQuery {
            events: [String!]!
        }
    
        type TypeMutation {
            createEvent(name: String): String
        }
    
        schema {  
            query: TypeQuery
            mutation: TypeMutation
        }
    `),
    rootValue: {
      events: () => {
        return ['Cats', 'Sailing'];
      },
      createEvent: (args) => {
        const eventName = args.name;
        return eventName;
      },
    },
    graphql: true,
  })
);

app.listen(3000);

When I typed in browser 'http://localhost:3000/myproject' I getting this error:

{"errors":[{"message":"Must provide query string."}]}

What I am wrong? In my project only change I made was in app.js. I don't have a frontend. Thank you

Upvotes: 0

Views: 4339

Answers (1)

Lafi
Lafi

Reputation: 1342

First of all we should use port 4000 with graphql endpoints, and app.use("/graphql", ... You're getting the. error because you are not loading the graphiql playground correctly ! just replace graphql: true, by graphiql: true, ?

this is working for me using http://127.0.0.1:4000/graphql :

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

const app = express();

app.use(bodyParser.json());

app.use(
  "/graphql",
  graphqlHttp({
    schema: buildSchema(`
      type TypeQuery {
        events: [String!]!
      }

      type TypeMutation {
        createEvent(name: String): String
      }

      schema {
        query: TypeQuery
        mutation: TypeMutation
      }
    `),
    rootValue: {
      events: () => {
        return ["Cats", "Sailing"];
      },
      createEvent: (args) => {
        const eventName = args.name;
        return eventName;
      },
    },
    graphiql: true,
  })
);

app.listen(4000, () => {
  console.log("server started on port 4000");
});

Upvotes: 2

Related Questions