Graphql-js: Unclear Error Output on Language/Parser.js

Created on 7 Mar 2017  路  12Comments  路  Source: graphql/graphql-js

I'm assuming I introduced the following error, but it seems to originate deep within the module and is not clear on exactly what it is that's failing

/home/zmg/Thinkful/Goalzapp/server/node_modules/graphql/language/parser.js:966 throw (0, _error.syntaxError)(lexer.source, token.start, 'Expected ' + kind + ', found ' + (0, _lexer.getTokenDesc)(token)); ^ GraphQLError at syntaxError (/home/zmg/Thinkful/Goalzapp/server/node_modules/graphql/error/syntaxError.js:28:15) at expect (/home/zmg/Thinkful/Goalzapp/server/node_modules/graphql/language/parser.js:966:32) at parseFieldDefinition (/home/zmg/Thinkful/Goalzapp/server/node_modules/graphql/language/parser.js:715:3) at any (/home/zmg/Thinkful/Goalzapp/server/node_modules/graphql/language/parser.js:1002:16) at parseObjectTypeDefinition (/home/zmg/Thinkful/Goalzapp/server/node_modules/graphql/language/parser.js:683:16) at parseTypeSystemDefinition (/home/zmg/Thinkful/Goalzapp/server/node_modules/graphql/language/parser.js:607:16) at parseDefinition (/home/zmg/Thinkful/Goalzapp/server/node_modules/graphql/language/parser.js:148:16) at parseDocument (/home/zmg/Thinkful/Goalzapp/server/node_modules/graphql/language/parser.js:106:22) at parse (/home/zmg/Thinkful/Goalzapp/server/node_modules/graphql/language/parser.js:43:10) at buildSchema (/home/zmg/Thinkful/Goalzapp/server/node_modules/graphql/utilities/buildASTSchema.js:461:43) at Object.<anonymous> (/home/zmg/Thinkful/Goalzapp/server/index.js:15:16) at Module._compile (module.js:571:32) at Object.Module._extensions..js (module.js:580:10) at Module.load (module.js:488:32) at tryModuleLoad (module.js:447:12) at Function.Module._load (module.js:439:3)

Most helpful comment

@leebyron

I'm able to repro this in one instance:

Naively, I removed an argument from one of the sub-types in my schema but left the parens, like this:

type RootQuery {
  viewer(webtoken: String!): User
  user(): User
  users: [User] 
  item(_id: String!): Item 
  items(menuCategory: MenuCategory): [Item] 
  allItems: [Item]
  order(_id: String!): Order 
  allOrders (): [Order] 
  menu: Menu
}

Notice users and allOrders. Removing the offending empty parens fixes the error below:

node_modules/graphql/language/parser.js:966
  throw (0, _error.syntaxError)(lexer.source, token.start, 'Expected ' + kind + ', found ' + (0, _lexer.getTokenDesc)(token));
  ^
GraphQLError
    at syntaxError (/Users/marcfawzi/c0de/graffalojs/server/node_modules/graphql/error/syntaxError.js:28:15)
    at expect (/Users/marcfawzi/c0de/graffalojs/server/node_modules/graphql/language/parser.js:966:32)
    at parseName (/Users/marcfawzi/c0de/graffalojs/server/node_modules/graphql/language/parser.js:88:15)
    at parseInputValueDef (/Users/marcfawzi/c0de/graffalojs/server/node_modules/graphql/language/parser.js:743:14)
    at many (/Users/marcfawzi/c0de/graffalojs/server/node_modules/graphql/language/parser.js:1015:16)
    at parseArgumentDefs (/Users/marcfawzi/c0de/graffalojs/server/node_modules/graphql/language/parser.js:735:10)
    at parseFieldDefinition (/Users/marcfawzi/c0de/graffalojs/server/node_modules/graphql/language/parser.js:714:14)
    at any (/Users/marcfawzi/c0de/graffalojs/server/node_modules/graphql/language/parser.js:1002:16)
    at parseObjectTypeDefinition (/Users/marcfawzi/c0de/graffalojs/server/node_modules/graphql/language/parser.js:683:16)
    at parseTypeSystemDefinition (/Users/marcfawzi/c0de/graffalojs/server/node_modules/graphql/language/parser.js:607:16)
    at parseDefinition (/Users/marcfawzi/c0de/graffalojs/server/node_modules/graphql/language/parser.js:148:16)
    at parseDocument (/Users/marcfawzi/c0de/graffalojs/server/node_modules/graphql/language/parser.js:106:22)
    at Object.parse (/Users/marcfawzi/c0de/graffalojs/server/node_modules/graphql/language/parser.js:43:10)
    at buildSchemaFromTypeDefinitions (/Users/marcfawzi/c0de/graffalojs/server/node_modules/graphql-tools/src/schemaGenerator.ts:158:19)
    at _generateSchema (/Users/marcfawzi/c0de/graffalojs/server/node_modules/graphql-tools/src/schemaGenerator.ts:72:18)
    at makeExecutableSchema (/Users/marcfawzi/c0de/graffalojs/server/node_modules/graphql-tools/src/schemaGenerator.ts:97:20)
    at Function.module.exports (/Users/marcfawzi/c0de/graffalojs/server/services/graphql/index.js:12:28)
    at Function.configure (/Users/marcfawzi/c0de/graffalojs/server/node_modules/feathers/lib/application.js:138:8)
    at Function.module.exports (/Users/marcfawzi/c0de/graffalojs/server/services/index.js:23:7)
    at Function.configure (/Users/marcfawzi/c0de/graffalojs/server/node_modules/feathers/lib/application.js:138:8)
    at Object.<anonymous> (/Users/marcfawzi/c0de/graffalojs/server/app.js:30:4)
    at Module._compile (module.js:571:32)
    at loader (/Users/marcfawzi/c0de/graffalojs/server/node_modules/babel-register/lib/node.js:144:5)
    at Object.require.extensions.(anonymous function) [as .js] (/Users/marcfawzi/c0de/graffalojs/server/node_modules/babel-register/lib/node.js:154:7)
    at Module.load (module.js:488:32)
    at tryModuleLoad (module.js:447:12)
    at Function.Module._load (module.js:439:3)
    at Module.require (module.js:498:17)
error Command failed with exit code 1.

All 12 comments

From what context did you get this error? Was there no message above the stack?

Typically syntax errors display the problem lines of query along with a highlighted section to help communicate the issue. However if there's a context where this display isn't showing, then I'd like to help fix that

Same thing. It seems like it crashes when try to build the error message.

Currently the only way to figure out what's wrong is randomly commenting out GraphQL and see if error disappear...

node_modules/graphql/language/parser.js:966
  throw (0, _error.syntaxError)(lexer.source, token.start, 'Expected ' + kind + ', found ' + (0, _lexer.getTokenDesc)(token));
  ^
GraphQLError
    at syntaxError (/node_modules/graphql/error/syntaxError.js:28:15)
    at expect (/graphql/language/parser.js:966:32)

...

@apetrushin or @zach-is-my-name: Are you able to come up with a minimal repro case showing how to observe this problem?

I can't reproduce I'm sorry.

@leebyron

I'm able to repro this in one instance:

Naively, I removed an argument from one of the sub-types in my schema but left the parens, like this:

type RootQuery {
  viewer(webtoken: String!): User
  user(): User
  users: [User] 
  item(_id: String!): Item 
  items(menuCategory: MenuCategory): [Item] 
  allItems: [Item]
  order(_id: String!): Order 
  allOrders (): [Order] 
  menu: Menu
}

Notice users and allOrders. Removing the offending empty parens fixes the error below:

node_modules/graphql/language/parser.js:966
  throw (0, _error.syntaxError)(lexer.source, token.start, 'Expected ' + kind + ', found ' + (0, _lexer.getTokenDesc)(token));
  ^
GraphQLError
    at syntaxError (/Users/marcfawzi/c0de/graffalojs/server/node_modules/graphql/error/syntaxError.js:28:15)
    at expect (/Users/marcfawzi/c0de/graffalojs/server/node_modules/graphql/language/parser.js:966:32)
    at parseName (/Users/marcfawzi/c0de/graffalojs/server/node_modules/graphql/language/parser.js:88:15)
    at parseInputValueDef (/Users/marcfawzi/c0de/graffalojs/server/node_modules/graphql/language/parser.js:743:14)
    at many (/Users/marcfawzi/c0de/graffalojs/server/node_modules/graphql/language/parser.js:1015:16)
    at parseArgumentDefs (/Users/marcfawzi/c0de/graffalojs/server/node_modules/graphql/language/parser.js:735:10)
    at parseFieldDefinition (/Users/marcfawzi/c0de/graffalojs/server/node_modules/graphql/language/parser.js:714:14)
    at any (/Users/marcfawzi/c0de/graffalojs/server/node_modules/graphql/language/parser.js:1002:16)
    at parseObjectTypeDefinition (/Users/marcfawzi/c0de/graffalojs/server/node_modules/graphql/language/parser.js:683:16)
    at parseTypeSystemDefinition (/Users/marcfawzi/c0de/graffalojs/server/node_modules/graphql/language/parser.js:607:16)
    at parseDefinition (/Users/marcfawzi/c0de/graffalojs/server/node_modules/graphql/language/parser.js:148:16)
    at parseDocument (/Users/marcfawzi/c0de/graffalojs/server/node_modules/graphql/language/parser.js:106:22)
    at Object.parse (/Users/marcfawzi/c0de/graffalojs/server/node_modules/graphql/language/parser.js:43:10)
    at buildSchemaFromTypeDefinitions (/Users/marcfawzi/c0de/graffalojs/server/node_modules/graphql-tools/src/schemaGenerator.ts:158:19)
    at _generateSchema (/Users/marcfawzi/c0de/graffalojs/server/node_modules/graphql-tools/src/schemaGenerator.ts:72:18)
    at makeExecutableSchema (/Users/marcfawzi/c0de/graffalojs/server/node_modules/graphql-tools/src/schemaGenerator.ts:97:20)
    at Function.module.exports (/Users/marcfawzi/c0de/graffalojs/server/services/graphql/index.js:12:28)
    at Function.configure (/Users/marcfawzi/c0de/graffalojs/server/node_modules/feathers/lib/application.js:138:8)
    at Function.module.exports (/Users/marcfawzi/c0de/graffalojs/server/services/index.js:23:7)
    at Function.configure (/Users/marcfawzi/c0de/graffalojs/server/node_modules/feathers/lib/application.js:138:8)
    at Object.<anonymous> (/Users/marcfawzi/c0de/graffalojs/server/app.js:30:4)
    at Module._compile (module.js:571:32)
    at loader (/Users/marcfawzi/c0de/graffalojs/server/node_modules/babel-register/lib/node.js:144:5)
    at Object.require.extensions.(anonymous function) [as .js] (/Users/marcfawzi/c0de/graffalojs/server/node_modules/babel-register/lib/node.js:154:7)
    at Module.load (module.js:488:32)
    at tryModuleLoad (module.js:447:12)
    at Function.Module._load (module.js:439:3)
    at Module.require (module.js:498:17)
error Command failed with exit code 1.

Strange that you're not seeing the message of the syntax error, which is what I see when I run a test like require('graphql').parse('{{'):

GraphQLError: Syntax Error: Expected Name, found {

Also, if you're calling parse() directly, then I recommend catching the parser errors and handling them in a more convenient way, such as:

try {
  return parse(text);
} catch (syntaxError) {
  console.error('Failed to parse the GraphQL document.\n' + syntaxError);
  process.exit(1);
}

I removed an argument ... but left the parens.
Removing the offending empty parens fixes the error below

@idibidiart Thanks, that was helpful! I was getting this error as well (just a bunch of mentions of /app/node_modules/graphql/language/parser.js, but no locations or files in my code).

Removing the empty parentheses fixed it for me too.

thanks alot @idibidiart

type LaunchConnection {
    cursor: String!
    hasMore: Boolean
    launches:  
}

issue in my case was that i didn't add a type for lauches after the colon.
expected graphql to have pointed me to where my error is coming from though, instead, i its points to a line parser.js

your answer pointed me to the right direction 馃槑

``Javascript import { Secret } from './config/secret'; const blog = { title: '', body: '', } axios.get(https://newsapi.org/v2/top-headlines?country=us&apiKey=${Secret}) .then(res => { blog.title = res.data.articles[1].title; blog.body = res.data.articles[1].content; return
${blog.title},
${blog.body}`
})
.catch(err => err);

const typeDefs = type Query{ post: Post! type Post{ title: String body: String } }
const resolvers = {
Query: {
post() {
return {
title: blog.title,
body: blog.body,
}
}
}
}

```
I am getting this error and unable to locate why this is occuring

``` Javascript C:\Users\Kiran\Documents\playground\graphnode_modules\graphql\language\parser.js:1463
throw (0, _error.syntaxError)(lexer.source, token.start, "Expected ".concat(kind, ", found ").concat((0, _lexer.getTokenDesc)(token)));
^
Syntax Error: Expected :, found Name "Post"

GraphQL request (4:10)
3: post: Post!
4: type Post{
^
5: title: String

at syntaxError (C:\Users\Kiran\Documents\playground\graph\node_modules\graphql\error\syntaxError.js:24:10)
at expect (C:\Users\Kiran\Documents\playground\graph\node_modules\graphql\language\parser.js:1463:32)
at parseFieldDefinition (C:\Users\Kiran\Documents\playground\graph\node_modules\graphql\language\parser.js:886:3)
at many (C:\Users\Kiran\Documents\playground\graph\node_modules\graphql\language\parser.js:1523:16)
at parseFieldsDefinition (C:\Users\Kiran\Documents\playground\graph\node_modules\graphql\language\parser.js:873:50)
at parseObjectTypeDefinition (C:\Users\Kiran\Documents\playground\graph\node_modules\graphql\language\parser.js:826:16)
at parseTypeSystemDefinition (C:\Users\Kiran\Documents\playground\graph\node_modules\graphql\language\parser.js:724:16)
at parseDefinition (C:\Users\Kiran\Documents\playground\graph\node_modules\graphql\language\parser.js:142:16)
at many (C:\Users\Kiran\Documents\playground\graph\node_modules\graphql\language\parser.js:1520:16)
at parseDocument (C:\Users\Kiran\Documents\playground\graph\node_modules\graphql\language\parser.js:113:18)```

Hello, new to GraphQL but was able to figure this out.

Basically, I had a javascript syntax comment on schema.graphql file:

// server/src/schema.graphql

type Query {
...

Once I removed the comment, the error was gone. I guess the // threw it off!

@walame GraphQL comments should start with #, see:
https://graphql.github.io/graphql-spec/June2018/#sec-Comments

hi, that's because it was expecting something like ":" .. eg:

Mutation {
newUser(_input:_ UserInput): String
}

That's it. Bye!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

davide-ganito picture davide-ganito  路  4Comments

henry74 picture henry74  路  4Comments

galki picture galki  路  3Comments

thomasdingemanse picture thomasdingemanse  路  4Comments

pranshuchittora picture pranshuchittora  路  3Comments