Ansh Gujral
Ansh Gujral

Reputation: 291

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
            }
            ]
        }
        ]
    }

Upvotes: 18

Views: 39764

Answers (9)

azizkale
azizkale

Reputation: 77

In my case on the frontend side (Angular), since I just wanted to observe something, I have this code: enter image description here

I got this error:

Error: Unexpected <EOF>.

and I made it comment-line and the error was gone

Upvotes: 0

&#233;niocarlos
&#233;niocarlos

Reputation: 179

How to fix it: Go to node_mudules > graphql > syntaxError.js and delete all comments from this file and the error should be gone.

Upvotes: -1

Sandip Subedi
Sandip Subedi

Reputation: 1077

This is because your graphql.gql looks like this:

# ------------------------------------------------------
# THIS FILE WAS AUTOMATICALLY GENERATED (DO NOT MODIFY)
# ------------------------------------------------------

directive @key(fields: String!) on OBJECT | INTERFACE

directive @extends on OBJECT | INTERFACE

directive @external on OBJECT | FIELD_DEFINITION

directive @requires(fields: String!) on FIELD_DEFINITION

directive @provides(fields: String!) on FIELD_DEFINITION

One thing you can do is create a dummy resolver.

// src/graphql/resolvers/user.resolver.ts

import { Resolver, Query } from '@nestjs/graphql';

import { User } from 'models/User.model';

@Resolver(() => User)
export class UserResolver {
  @Query(() => User)
  async ids() {
    return [];
  }
}

and create a graphql model:

// src/graphql/models

import { Field, ObjectType } from '@nestjs/graphql';

@ObjectType()
export class User {
  @Field()
  createdAt: Date;
}

And make sure that resolver is imported by a module and the module is added to App.module

This should fix it!

Upvotes: 0

Sharkey
Sharkey

Reputation: 41

I had comments in the schema.graphql file:

"""
Some comments
"""

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

Upvotes: 3

Dinush Chathurya
Dinush Chathurya

Reputation: 512

In most cases passing an empty query is caused to this error. To avoid this try this in your graphiql

query { 
   user {
     fields you need to retrieve
  }
}

Upvotes: 0

Muhammad Usama Rabani
Muhammad Usama Rabani

Reputation: 776

It looks like if we are doing the schema-first approach, but we do not have any .graphql files in your project/empty. I added this and the error went away:

src/example.graphql

type Query {
  hello: String
}

The "GraphQLError: Syntax Error: Unexpected " error is coming from the graphql package. It looks like @nestjs/graphql is assuming that there will always be at least one .graphql file, so this is probably a bug to try and parse an empty schema vs display a more developer-friendly message or ignore it.

Upvotes: 0

amirNamdar
amirNamdar

Reputation: 31

I got this error while trying to stitch my schema the reason for this error was that I delcared a type but didn't implemet it, had empty body, for example:

type User {

}

implementing the type fixed it

Upvotes: 0

Mallory-Erik
Mallory-Erik

Reputation: 1820

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
  }
}

Upvotes: 1

Marco Daniel
Marco Daniel

Reputation: 5765

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.

Upvotes: 22

Related Questions