Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unexpected <EOF> while using graphql

Getting EOF error every time at same line, changed code many times and even degraded to previous versions of graphql but no positive results. My code is:

const graphql = require('graphql')
const _ = require('lodash')
 const {
     GraphQLObjectType,
     GraphQLString,
     GraphQLInt,
     GraphQLSchema
 } = graphql

const users = [
    {id: '1', firstName: 'Ansh', age: 20},
    {id: '2', firstName: 'Ram', age: 21},
    {id: '3', firstName: 'Sham', age: 20}
]

 const UserType = new GraphQLObjectType({
     name: 'User',
     fields: {
        id: {type: GraphQLString},
        firstName: {type: GraphQLString},
        age: {type: GraphQLInt}
     }
 })

 const RootQuery = new GraphQLObjectType({
     name: 'RootQueryType',
          fields: {
             user: {
                 type: UserType,
                 args: {id: {type: GraphQLString}},
                 resolve(parentValue, args) { 
                    return _.find(users, {id: args.id})
                 }
             }
         }
     })

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

Error is:

    {
        "errors": [
        {
            "message": "Syntax Error GraphQL request (30:1) Unexpected <EOF>\n\n29: \n30: \n    ^\n",
            "locations": [
            {
                "line": 30,
                "column": 1
            }
            ]
        }
        ]
    }
like image 603
Ansh Gujral Avatar asked Jun 12 '18 23:06

Ansh Gujral


3 Answers

The issue is because the query you're passing might be empty.

For example:

curl -X POST http://localhost:4000/graphql \ 
-H "Content-Type: application/json" \
-d '{"query": "{ user { id } }"}'

works fine.

But if you make something like:

curl -X POST http://localhost:4000/graphql \
-H "Content-Type: application/json" \
-d '{"query": ""}'

You'll get unexpected < EOF >

Also, check GraphQL end of line issue.

like image 168
Marco Daniel Avatar answered Oct 16 '22 14:10

Marco Daniel


I had comments in the schema.graphql file:

"""
Some comments
"""

I removed the comments and the Unexpected <EOF> error went away.

like image 42
Sharkey Avatar answered Oct 16 '22 13:10

Sharkey


It's because there's no actual query so you get unexpected EOF (End of File).

Guessing that you're using GraphiQL (because your EOF message says line 30); you need to add a query on the left-hand panel of GraphiQL in the browser. Something which conforms to your RootQuery like:

{
  user(id: "1") {
    id,
    firstName,
    age
  }
}
like image 1
Mallory-Erik Avatar answered Oct 16 '22 14:10

Mallory-Erik