GraphQL Code Generator "Syntax Error: Unexpected Name" in CI
GraphQL Code Generator parses your SDL and operation documents with the graphql-js parser. A malformed type or operation raises "Syntax Error: Unexpected Name ..." with the file and position of the token the parser did not expect.
What this error means
graphql-codegen fails with "Syntax Error: Unexpected Name \"<token>\"." and a source location, usually pointing at a typo in a schema or query file.
Syntax Error: Unexpected Name "tpye".
GraphQL request:3:3
2 | type User {
3 | tpye String
| ^Common causes
A typo or missing punctuation in SDL
A misspelled keyword, a missing colon between a field and its type, or a stray token makes the parser report an unexpected name.
A non-GraphQL file matched the documents glob
The documents glob picked up a file that is not valid GraphQL, so the parser chokes on its contents.
How to fix it
Fix the SDL at the reported position
- Open the file and line from the error.
- Correct the typo or add the missing
:/punctuation. - Re-run codegen to confirm the document parses.
type User {
type: String
}Narrow the documents glob
Restrict the glob so it only matches real .graphql/.gql files and excludes anything that is not GraphQL.
# codegen.yml
documents:
- 'src/**/*.graphql'
- '!src/**/__generated__/**'How to prevent it
- Validate SDL and operations with a linter before codegen.
- Keep the documents glob tight so it only matches GraphQL files.
- Run codegen locally so syntax errors surface before CI.