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 #231

Closed
wants to merge 2 commits into from
Closed

Always guard against undefined type in fieldAvailableOnType #231

wants to merge 2 commits into from

Conversation

vitorbal
Copy link
Contributor

@vitorbal vitorbal commented Jun 22, 2019

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

vitorbal added 2 commits June 22, 2019 04:27
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 👇

@@ -5,7 +5,7 @@
"author": "Sashko Stubailo",
"main": "lib/index.js",
"scripts": {
"test": "tav --ci && mocha test/index.js",
"test": "tav",
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Tests have been failing in CI for a while, because in CI tests rung against all versions of graphql that satisfy the semver ranges defined in .tav.yml:

versions: ^0.12.0 || ^0.13.0 || ^14.0.0

Amongst those was v14.3.1, which shows a slightly different error message for the ProvidedRequiredArguments validator. You can see the error here: https://travis-ci.org/apollographql/eslint-plugin-graphql/jobs/548949602

If you look at recent PRs, all of them are failing with the same error. What made this confusing to me was that tav was only being run on CI, and not locally. So locally tests were passing. For that reason, I thought it would be best if tav also runs locally now –– hence why the --ci flag is removed here. If that's not okay, I can revert this part of the PR.

Anyways, the fix for the actual error can be seen below in test/validationRules/built-in.js

@@ -162,7 +162,7 @@ const validatorCases = {
errors: [
{
message:
'Field "sum" argument "b" of type "Int!" is required but not provided.',
/Field "sum" argument "b" of type "Int!" is required/,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Make these regexes so they pass in all versions of graphql that we test (the error messages have slight variations depending on the version).

@@ -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.

This test case would throw an error if not for the fix in src/customGraphQLValidationRules.js above

@vitorbal
Copy link
Contributor Author

@jnwng @abernix 👋 sorry for the ping, but it looks like you're the active maintainers of this repo. Any chance you could take a look at this when you have a spare moment? Thanks in advance 🙂

@vitorbal
Copy link
Contributor Author

vitorbal commented Jul 3, 2019

@jnwng @abernix friendly ping, any chance you could take a look at this one? Should be an easy bug fix. Thanks in advance!

@vitorbal
Copy link
Contributor Author

Hey @jnwng it's been almost 3 months, any chance you'd have time to take a look at this? It's blocking me from using eslint-plugin-graphql at work.
Thanks in advance!

@jnwng
Copy link
Contributor

jnwng commented Sep 18, 2019 via email

@vitorbal
Copy link
Contributor Author

@jnwng thanks so much for the reply! 🙏 I know y'all must be pretty busy, so I appreciate it!

I had an issue with the original branch this PR was based on (says "unknown repository" now 🤔), so I recreated the PR here.

I'll close this one and tag you over there!

@vitorbal vitorbal closed this Sep 18, 2019
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