Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeError: Cannot read property '0' of undefined #308

Closed
sbienkow-ninja opened this issue May 9, 2022 · 7 comments · Fixed by #309
Closed

TypeError: Cannot read property '0' of undefined #308

sbienkow-ninja opened this issue May 9, 2022 · 7 comments · Fixed by #309

Comments

@sbienkow-ninja
Copy link

After installing following packages:

$ npm install -g graphql-schema-linter@3.0.0 graphql@16.3.0

And running the graphql-schema-linter:

$ graphql-schema-linter **/*.graphql
It looks like you may have hit a bug in graphql-schema-linter.

It would be super helpful if you could report this here: https://github.com/cjoudrey/graphql-schema-linter/issues/new

TypeError: Cannot read property '0' of undefined
    at /opt/hostedtoolcache/node/14.19.1/x64/lib/node_modules/graphql-schema-linter/lib/validator.js:79:23
    at Array.sort (<anonymous>)
    at sortErrors (/opt/hostedtoolcache/node/14.19.1/x64/lib/node_modules/graphql-schema-linter/lib/validator.js:78:17)
    at validateSchemaDefinition (/opt/hostedtoolcache/node/14.19.1/x64/lib/node_modules/graphql-schema-linter/lib/validator.js:71:24)
    at run (/opt/hostedtoolcache/node/14.19.1/x64/lib/node_modules/graphql-schema-linter/lib/runner.js:68:58)
Error: Process completed with exit code 3.

I hate to post such empty issue, but no clue what more information I could include (apart from the .graphql files which I can't make public).

@modosc
Copy link

modosc commented May 9, 2022

we hit it too. i added some debugging in lib/validator.js:

function sortErrors(errors) {
  console.log({ errors: errors.filter(e => !e.locations) })
  return errors.sort((a, b) => {
    return a.locations[0].line - b.locations[0].line;
  });
}

and (ironically) this seems to be one error that's exploding for me:

  errors: [
    GraphQLError: Too many validation errors, error limit reached. Validation aborted.
        at ValidationContext._onError (/Users/jonathan.schatz/git/data-spec/node_modules/graphql/validation/validate.js:73:11)
        at ValidationContext.reportError (/Users/jonathan.schatz/git/data-spec/node_modules/graphql/validation/ValidationContext.js:36:10)
        at Object.FieldDefinition (/Users/jonathan.schatz/git/data-spec/node_modules/graphql-schema-linter/lib/rules/fields_have_descriptions.js:23:15)
        at Object.enter (/Users/jonathan.schatz/git/data-spec/node_modules/graphql/language/visitor.js:301:32)
        at Object.enter (/Users/jonathan.schatz/git/data-spec/node_modules/graphql/utilities/TypeInfo.js:391:27)
        at visit (/Users/jonathan.schatz/git/data-spec/node_modules/graphql/language/visitor.js:197:21)
        at validate (/Users/jonathan.schatz/git/data-spec/node_modules/graphql/validation/validate.js:91:24)
        at validateSchemaDefinition (/Users/jonathan.schatz/git/data-spec/node_modules/graphql-schema-linter/lib/validator.js:70:43)
        at run (/Users/jonathan.schatz/git/data-spec/node_modules/graphql-schema-linter/lib/runner.js:68:58) {
      path: undefined,
      locations: undefined,
      extensions: [Object: null prototype] {}
    }
  ]

@cjoudrey
Copy link
Owner

cjoudrey commented May 10, 2022

Ah, darn. 😢 It would definitely be easier to debug this with a schema.

It could be worth double checking if this happens with graphql@15.8.0.

Even better, if you can make it crash with a shareable subset of your schema that would make fixing this easier.

Is there anything about your schema that might be out of the ordinary? I ask because I've got a series of small fake schemas that work fine.

Edit: Are you using comments as type descriptions by any chance? 🤔

@sbienkow-ninja
Copy link
Author

@cjoudrey I just checked - with graphql@15.8.0 this does not happen. It does happen with 16.3.0 and 16.5.0.

I've tried to pin-point which file would be causing this, but the schema is so interlinked that, without more knowledge about GraphQL itself, I'm afraid I'm not able to.

Could you provide an example of what you mean by Are you using comments as type descriptions? From what I found that's the only way to create descriptions, right?

@cjoudrey
Copy link
Owner

cjoudrey commented May 12, 2022

Thanks for the debugging! 🙂

I suspect that might be the issue, but the linter should gracefully handle this. Crashing is not a great user experience.

Comment descriptions have been deprecated for some time now. As of GraphQL 16.x, I believe comment descriptions were officially removed and replaced with string literal descriptions. I'll need to confirm this is the issue once I am in front of my computer.

Hope that helps for now.

@sbienkow-ninja
Copy link
Author

@cjoudrey Oh, do you mean this:

type Example {
  # This is a comment description
  comment: String
  "This is a string literal description"
  literal: String
}

I grepped all our schemas, # comments are only used to disable/enable lint, there is nothing besides that.

@modosc
Copy link

modosc commented May 12, 2022

i also checked and even with all # comments removed we still get the error.

i wonder if just fixing sortErrors to handle path: undefined would help?

@cjoudrey
Copy link
Owner

This issue should be addressed by: #309.

I'll make a release shortly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants