Graphql-code-generator: error: Syntax Error: Expected Name, found $

Created on 17 Sep 2018  路  23Comments  路  Source: dotansimha/graphql-code-generator

Describe the bug
when running the gql-gen tool on the schema.json file below I get the error: error: Syntax Error: Expected Name, found $ and nothing else, all other tools iv tried out does not give me any errors, so I am clueless on how to find the actual error here... a better error message could maybe help?

I have used GraphQL Playground and Apollo Engine to try and look for errors in the scema, but no luck...

To Reproduce
Steps to reproduce the behavior:

  1. Use schema.json as Schema
  2. Run the following command: gql-gen --schema schema.json --template graphql-codegen-typescript-template --out types.ts

Expected behavior
A types.ts file that compiles.

Environment:

  • OS: Windows 10
  • Codegen: 0.10.7
  • Node: 10.0.0

DEBUG Output
debug.log

Most helpful comment

@furier , If you do have GraphQL documents, you can try v0.12.4. Thanks to @jonaskello , now the error are printed in a better and readable way, and it's pointing to the file with the errors.

All 23 comments

error: Syntax Error: Expected Name, found $

It's an error that comes from GraphQL library. I guess it's caused because of some syntax issues in your documents, your GraphQL operations. You probably try to put a variables directly and they should be named. An example:

query feed($first: Int) {
  feed($first) {
    id
    name
  }
}

Or something similar. Maybe you use variables directly in the fields without defining them in the operation.

Try to find queries / mutations etc where you've got arguments so we can help you.


This error comes from GraphQL library, it doesn't tell much but @dotansimha maybe we could catch it and re-throw with name of the operation or something, or a whole document (in debug mode).

@kamilkisiela I think it's the same issue as this: https://github.com/dotansimha/graphql-code-generator/issues/621 , right?

In terms of displaying an error, yes.

@furier I'm not sure if that's something with GitHub, but the schema.json file you uploaded is not valid JSON, it's breaking in the middle of it:
image

Can you please try to upload it to a different place?

Also, @furier, according to your command: gql-gen --schema schema.json --template graphql-codegen-typescript-template --out types.ts, you are generating typings for the schema, without GraphQL documents, right?

@furier , If you do have GraphQL documents, you can try v0.12.4. Thanks to @jonaskello , now the error are printed in a better and readable way, and it's pointing to the file with the errors.

@dotansimha the schema.json file is updated, and it look like it is not the problem, the problem lies in the frontend graphql queries and mutations in gql tags in ts files.

@furier How many queries do you have? Maybe try to find the ones with $ sign and then put them here https://astexplorer.net, it should show what's wrong

got any easy ways to extract everything from all gql tags in *.ts files?

Also, @furier, according to your command: gql-gen --schema schema.json --template graphql-codegen-typescript-template --out types.ts, you are generating typings for the schema, without GraphQL documents, right?

Sorry @dotansimha I do include documents but since the braking change happened after backend changes i assumed it was the backend that was the problem and left it out, however I am no longer convinced of that. I guess there is a matching from schema.json on gql tags on frontend and there is a mismatch witch causes the breaking.

@furier @dotansimha the errors says you're doing something like this:
https://astexplorer.net/#/gist/bdd952b0ca07b0aa28c6c54b1c0b02be/b88c1b9b125220da25b3e88db1d03e0b3b4890e2

I will try to find out how to validate those documents easily.

I also upgraded to 0.12.4 and I am still only seeing the error: Syntax Error: Expected Name, found $ I have searched all ~155 tags for errors regarding $ but all seem to check out by the naked eye.

I don't think code gen uses schema to validates them, it happens while parsing.

Yeah @kamilkisiela is right, the error comes during the parse and not during validate.
We will add a fix for it in a few minutes, but again, it's not an issue with the codegen, but an issue with one of your queries. We hope that it will help you find it faster :)

@furier please try to use 0.12.5, it should show you the document/filename that caused this error.

Thank God (no I really mena you) for that fix, a lifesaver!

The line with the error seems to be

const query = `query EntityProperties { ${queries.join(' ')} } ${entityFragment}`;

It is a generated query passed in to a gql tag at later stage like this gql(query), I thought that the parser should only parse graphql within gql tags and not simple strings?

It works on version 0.10.3 and when we updated to version 0.10.7 and up the error started happening

We are parsing the documents as string, and then pass them to GraphQL parser... for now (with an exception for fragments support, https://github.com/dotansimha/graphql-code-generator/pull/624 , that we might implement soon)
We might change it in the future to load the file as code, but then it's impossible to know which export is the correct one to use...

@dotansimha has there been any changes to how the tool finds fragments? we are getting a lot of Unknown fragment "..." errors when the GraphQL documents are validated against the schema, which we didn't use to have :O

@furier I think this is the error that I have a test for in #624. I am thinking maybe we could make a quickfix for it where we just ignore the unknown fragment errors.

I did a proposal for a quickfix for this in #628.

@furier The change that was made was that each file is validated against the schema separately in order to be able to print which file caused which error.

Before all graphql from all files were concatenated into one big AST and then that was validated. This made it possible to find all fragments as they existed in the single AST, however it made it impossible to know which file caused which validation error. So we got some errors but no clue in which file it occured.

With the change we can trace all validation errors to files, however we cannot fully validate cases where a file imports a fragment from another file as that now will result in an unknown fragment error. This is because fragments are not resolved which I think they should be, but until then I think my quickfix would make it possible for you to run again.

@jonaskello thanks!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

fvisticot picture fvisticot  路  3Comments

steebchen picture steebchen  路  3Comments

SimenB picture SimenB  路  3Comments

NickClark picture NickClark  路  3Comments

leebenson picture leebenson  路  3Comments