Graphql-code-generator: Syntax Error/Schema Error should give line number

Created on 29 May 2020  路  4Comments  路  Source: dotansimha/graphql-code-generator

Describe the bug
The graphql-codegen process should give better diagnostics when a schema file has a syntax error or can't be parsed. Specifically, it should tell you where in the schema file the error was found.

To Reproduce
Using this codegen.yaml:

generates:
  ./src/generated/test.ts:
    schema: schema/test.graphql
    plugins:
      - typescript

And this schema file (notice Number for attorney_id):

schema {
  query: RootQuery
}

type Attorney {
  first_name: String
  last_name: String
  attorney_id: Number
}

Gives the following output when graphql-codegen is run:

 Found 1 error

  脳 ./src/generated/test.ts
    Failed to load schema from schema/test.graphql:

        Type "Number" not found in document.
        Error: Type "Number" not found in document.
    at ASTDefinitionBuilder._resolveType (C:\Users\Justin\Source\foo\express\node_modules\graphql\utilities\buildASTSchema.js:94:13)
    at ASTDefinitionBuilder.getNamedType (C:\Users\Justin\Source\foo\express\node_modules\graphql\utilities\buildASTSchema.js:171:37)
    at ASTDefinitionBuilder.getWrappedType (C:\Users\Justin\Source\foo\express\node_modules\graphql\utilities\buildASTSchema.js:183:17)
    at ASTDefinitionBuilder.buildField (C:\Users\Justin\Source\foo\express\node_modules\graphql\utilities\buildASTSchema.js:212:18)
    at C:\Users\Justin\Source\foo\express\node_modules\graphql\utilities\buildASTSchema.js:303:23
    at C:\Users\Justin\Source\foo\express\node_modules\graphql\jsutils\keyValMap.js:27:24
    at Array.reduce (<anonymous>)
    at keyValMap (C:\Users\Justin\Source\foo\express\node_modules\graphql\jsutils\keyValMap.js:26:15)
    at keyByNameNode (C:\Users\Justin\Source\foo\express\node_modules\graphql\utilities\buildASTSchema.js:396:33)
    at C:\Users\Justin\Source\foo\express\node_modules\graphql\utilities\buildASTSchema.js:302:14
    at resolveThunk (C:\Users\Justin\Source\foo\express\node_modules\graphql\type\definition.js:438:40)
    at defineFieldMap (C:\Users\Justin\Source\foo\express\node_modules\graphql\type\definition.js:625:18)
    at GraphQLObjectType.getFields (C:\Users\Justin\Source\foo\express\node_modules\graphql\type\definition.js:579:27)
    at typeMapReducer (C:\Users\Justin\Source\foo\express\node_modules\graphql\type\schema.js:280:78)
    at Array.reduce (<anonymous>)
    at new GraphQLSchema (C:\Users\Justin\Source\foo\express\node_modules\graphql\type\schema.js:145:28)

        GraphQL Code Generator supports:
          - ES Modules and CommonJS exports (export as default or named export "schema")
          - Introspection JSON File
          - URL of GraphQL endpoint
          - Multiple files with type definitions (glob expression)
          - String in config file

        Try to use one of above options and run codegen again.
    Error: Failed to load schema
        at loadSchema (C:\Users\Justin\Source\foo\express\node_modules\@graphql-codegen\cli\bin.js:406:15)
    Error: Failed to load schema
        at loadSchema (C:\Users\Justin\Source\foo\express\node_modules\@graphql-codegen\cli\bin.js:406:15)

* Code Sandbox *

Live repro at https://codesandbox.io/s/hungry-curie-m2tig?file=/schema.graphql

schema.graphql contains the syntax error.

Expected behavior

I would like the error report to show that line 8 of test.graphql caused the error. I don't care about the stack trace from graphql-codegen's internals.

Environment:

  • OS: Windows 10
  • "@graphql-codegen/cli": "^1.13.5",
  • "@graphql-codegen/typescript-resolvers": "^1.13.5",
  • @graphql-codegen/...:
  • NodeJS: v12.17.0

Additional context

Essentially, any error related to parsing or udnerstanding the schema should give you the location in the schema where the error might have occurred.

Most helpful comment

Thanks very much. Hopefully I can find a way to give back. This project is super useful and seems to be the best of all the generators I examined.

All 4 comments

First, it is not a syntax error in terms of GraphQL SDL. The given SDL is gramatically correct but it is not valid.
Second, your reproduction gives another error which is RootQuery is not found but I added a RootQuery with attorney: Attorney field to reproduce the same error with yours.
Third, that error stack trace comes from GraphQL.js package not graphql-codegen's internals. GraphQL Codegen uses GraphQL.js to parse your SDL from the file you provided and build a schema based on that SDL. So it is something needs to be improved o GraphQL.js side, not GraphQL Codegen.

First, it is not a syntax error in terms of GraphQL SDL. The given SDL is gramatically correct but it is not valid.
Second, your reproduction gives another error which is RootQuery is not found but I added a RootQuery with attorney: Attorney field to reproduce the same error with yours.
Third, that error stack trace comes from GraphQL.js package not graphql-codegen's internals. GraphQL Codegen uses GraphQL.js to parse your SDL from the file you provided and build a schema based on that SDL. So it is something needs to be improved o GraphQL.js side, not GraphQL Codegen.

This is not a response that encourages me to contribute to your project in the future. Does it really matter if this is a type error or syntax error? Does it matter that the repro case had a minor error in it? I took the time to write this up and demonstrate according to your guidelines. Now that just feels like a waste.

Much nicer would have been something along the lines "You are right, it's really painful that we can't report the line number an error occurs on. However, we rely on the graphql library for parsing and they don't implement this feature."

@dotansimha

I do really apologize, I didn't mean that. I just wanted to avoid misleading and give the correct information not only for you but also for the other people who see this issue in the future.
I appreciate your contribution by reporting an issue to us to make us this project better. Let me correct my words then. Yes, you're right. Unfortunately, our major dependency graphql-js doesn't report a helpful stack trace for debugging the syntax and processing errors.

Thanks very much. Hopefully I can find a way to give back. This project is super useful and seems to be the best of all the generators I examined.

Was this page helpful?
0 / 5 - 0 ratings