0

I'm beginning with graphql on express. I'm getting the error "Must provide query string." when I access to the route for it.

It's an express app. Here is my app.js

var createError   = require('http-errors');  
var express       = require('express');  
var path          = require('path');  
var cookieParser  = require('cookie-parser');  
var logger        = require('morgan');  
let bodyParser    = require('body-parser');  
var cors          = require('cors');  
    
const graphqlHTTP = require('express-graphql');  
const MySchema    = require('./schema/schema');  

app.use(cors());
app.use(logger('dev'));
app.use(bodyParser.urlencoded({extended: true}));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

app.use('/graphql', graphqlHTTP({
  schema: MySchema,
  graphiql: true,
}));

And my shema is:

const graphql = require('graphql');
const {
    GraphQLObjectType, 
    GraphQLString, 
    GraphQLSchema,
    GraphQLList
} = graphql;


//MODELS FROM MONGOOSE
const Entidad = require('../models/entidad');


//TYPES GRAPHQL
const EntidadType = new GraphQLObjectType({
    name: 'Entidad',
    fields : () => ({
        id      : {type : GraphQLString},
        nombre  : {type : GraphQLString},
        created_at : { type : GraphQLString},
        updated_at : { type : GraphQLString},
    })
});

//ROOT QUERY
const RootQuery = new GraphQLObjectType({
    name : 'RootQueryType',
    fields : {
        entidad : {
            type: EntidadType,
            args: {id:{type: GraphQLString}},
            resolve(parent, args){
                //code to get data from db
                return Entidad.findById(args.id);
            }
        }
    }
});

module.exports = new GraphQLSchema({
    query: RootQuery
});

I don't know why I'm getting the error when open graphiql.

I test other examples and give me the same error, maybe there is some part that I missed....

Some idea?

Yoedusvany Hdez
  • 415
  • 2
  • 5
  • 15
  • Are you using other middleware on your server? If so, it would be helpful to see that code as well, since middleware like `body-parser` can mess with the logic in `express-graphql`'s middleware. – Daniel Rearden Dec 24 '18 at 15:46
  • Ok I updated the post, now you can see middlewares. I'm using body-parser – Yoedusvany Hdez Dec 24 '18 at 16:06
  • Now I comment the line app.use(bodyParser.urlencoded({extended: true})); and I see the root query on Documentation explorer but now I'm getting the error { "errors": [ { "message": "Syntax Error: Unexpected ", "locations": [ { "line": 29, "column": 1 } ] } ] } – Yoedusvany Hdez Dec 24 '18 at 16:13
  • Hello @DanielRearden, I'm facing the same error when I try to access graphql with AWS lambda function. – Jay Sojitra Jul 11 '19 at 07:11

1 Answers1

2

Well, I commented the line of body-parser and I fix the problem, the other problem is because I don't have a rootValue into the route of graphql. I tested the queries and it's all ok. Thanks.

Yoedusvany Hdez
  • 415
  • 2
  • 5
  • 15