Skip to content

Commit

Permalink
Always guard against undefined type in fieldAvailableOnType (#247)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
vitorbal authored and jnwng committed Sep 20, 2019
1 parent 692c92d commit 2a67417
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -2,6 +2,8 @@

### vNEXT

- Fix an issue that caused `graphql/required-fields` to throw on non-existent field references. [PR #231](https://github.com/apollographql/eslint-plugin-graphql/pull/231) by [Vitor Balocco](https://github.com/vitorbal)

### v3.0.3

- chore: Update dependency `graphql-tools` to `v4.0.4`. [PR #210](https://github.com/apollographql/eslint-plugin-graphql/pull/210)
Expand Down
6 changes: 5 additions & 1 deletion src/customGraphQLValidationRules.js
Expand Up @@ -19,8 +19,12 @@ function getFieldWasRequestedOnNode(node, field) {
}

function fieldAvailableOnType(type, field) {
if (!type) {
return false;
}

return (
(type && type._fields && type._fields[field]) ||
(type._fields && type._fields[field]) ||
(type.ofType && fieldAvailableOnType(type.ofType, field))
);
}
Expand Down
1 change: 1 addition & 0 deletions test/validationRules/required-fields.js
Expand Up @@ -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 }`",
"const x = gql`fragment Id on Node { id ... on NodeA { fieldA } }`",
"const x = gql`query { nodes { id ... on NodeA { fieldA } } }`",
],
Expand Down

0 comments on commit 2a67417

Please sign in to comment.