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

Always guard against undefined type in fieldAvailableOnType #247

Merged
merged 1 commit into from
Sep 20, 2019

Conversation

vitorbal
Copy link
Contributor

Previously, only the first half of the check inside fieldAvailableOnType guarded against type being undefined. This meant that the function would throw an error.

To fix this, and o prevent it from accidentally happening again if we ever add another check to this logic, I moved the check for !type up into it's own guard, with an early return.

This change makes the required-fields rule more resilient (which uses this fieldAvailableOnType function internally).

Without this change, I see errors when trying to run eslint-plugin-graphql against my project:

Cannot read property 'ofType' of undefined
TypeError: Cannot read property 'ofType' of undefined
    at fieldAvailableOnType (/Code/vitorbal/node_modules/eslint-plugin-graphql/lib/customGraphQLValidationRules.js:33:62)
    at /Code/vitorbal/node_modules/eslint-plugin-graphql/lib/customGraphQLValidationRules.js:49:13
    at Array.forEach (<anonymous>)
    at Object.FragmentDefinition (/Code/vitorbal/node_modules/eslint-plugin-graphql/lib/customGraphQLValidationRules.js:42:22)
    at Object.enter (/Code/vitorbal/node_modules/graphql/language/visitor.js:332:29)
    at Object.enter (/Code/vitorbal/node_modules/graphql/language/visitor.js:383:25)
    at visit (/Code/vitorbal/node_modules/graphql/language/visitor.js:250:26)
    at validate (/Code/vitorbal/node_modules/graphql/validation/validate.js:63:22)
    at handleTemplateTag (/Code/vitorbal/node_modules/eslint-plugin-graphql/lib/createRule.js:133:57)
    at TaggedTemplateExpression (/Code/vitorbal/node_modules/eslint-plugin-graphql/lib/createRule.js:226:20)

TODO:

  • Make sure all of the significant new logic is covered by tests
  • Rebase your changes on master so that they can be merged easily
  • Make sure all tests pass
  • Update CHANGELOG.md with your change
  • If this was a change that affects the external API, update the README

Previously, only the first half of the check inside
`fieldAvailableOnType` guarded against `type` being
`undefined`.

To prevent this from accidentally happening again if
we ever have to add another check to this logic,
I moved the check for `!type` up into it's own guard,
with an early return.
Copy link
Contributor Author

@vitorbal vitorbal left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Annotations 👇

@@ -11,6 +11,7 @@ const requiredFieldsTestCases = {
"const x = gql`query { greetings { id, hello, foo } }`",
"const x = gql`query { greetings { id ... on Greetings { hello } } }`",
"const x = gql`fragment Name on Greetings { id hello }`",
"const x = gql`fragment Foo on FooBar { id, hello, foo }`",
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added this test case to prevent a regression – it would throw an error if not for the fix in src/customGraphQLValidationRules.js introduced by this PR.

@vitorbal
Copy link
Contributor Author

@jnwng here's the new PR, rebased to master! 🙏

@jnwng jnwng merged commit 2a67417 into apollographql:master Sep 20, 2019
@jnwng
Copy link
Contributor

jnwng commented Sep 20, 2019

live in eslint-plugin-graphql@3.1.0

@vitorbal vitorbal deleted the fix-required-fields branch October 9, 2019 10:01
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 this pull request may close these issues.

2 participants