Skip to content

Commit 2a67417

Browse files
vitorbaljnwng
authored andcommitted
Always guard against undefined type in fieldAvailableOnType (#247)
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.
1 parent 692c92d commit 2a67417

File tree

3 files changed

+8
-1
lines changed

3 files changed

+8
-1
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
### vNEXT
44

5+
- 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)
6+
57
### v3.0.3
68

79
- chore: Update dependency `graphql-tools` to `v4.0.4`. [PR #210](https://github.com/apollographql/eslint-plugin-graphql/pull/210)

src/customGraphQLValidationRules.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,12 @@ function getFieldWasRequestedOnNode(node, field) {
1919
}
2020

2121
function fieldAvailableOnType(type, field) {
22+
if (!type) {
23+
return false;
24+
}
25+
2226
return (
23-
(type && type._fields && type._fields[field]) ||
27+
(type._fields && type._fields[field]) ||
2428
(type.ofType && fieldAvailableOnType(type.ofType, field))
2529
);
2630
}

test/validationRules/required-fields.js

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ const requiredFieldsTestCases = {
1111
"const x = gql`query { greetings { id, hello, foo } }`",
1212
"const x = gql`query { greetings { id ... on Greetings { hello } } }`",
1313
"const x = gql`fragment Name on Greetings { id hello }`",
14+
"const x = gql`fragment Foo on FooBar { id, hello, foo }`",
1415
"const x = gql`fragment Id on Node { id ... on NodeA { fieldA } }`",
1516
"const x = gql`query { nodes { id ... on NodeA { fieldA } } }`",
1617
],

0 commit comments

Comments
 (0)