diff --git a/CHANGELOG.md b/CHANGELOG.md index 6263d91..ffa0d78 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ ### vNEXT +- Fix `graphql/required-fields` throwing on non-existent field reference [Benjie](https://github.com/benjie) in [#65](https://github.com/apollographql/eslint-plugin-graphql/pull/65) + ### v0.8.0 - Updated `graphql` dependency to resolve test failures ([wording change](https://github.com/graphql/graphql-js/commit/ba401e154461bca5323ca9121c6dacaee10ebe88), no API change) [jnwng](https://github.com/jnwng) diff --git a/src/rules.js b/src/rules.js index ad06ae5..ca1c701 100644 --- a/src/rules.js +++ b/src/rules.js @@ -16,6 +16,9 @@ export function RequiredFields(context, options) { return { Field(node) { const def = context.getFieldDef(); + if (!def) { + return; + } const { requiredFields } = options; requiredFields.forEach(field => { const fieldAvaliableOnType = def.type && def.type._fields && def.type._fields[field]; diff --git a/test/makeRule.js b/test/makeRule.js index 1903aac..5dc41f6 100644 --- a/test/makeRule.js +++ b/test/makeRule.js @@ -796,7 +796,8 @@ const namedOperationsValidatorCases = { const requiredFieldsTestCases = { pass: [ 'const x = gql`query { allFilms { films { title } } }`', - 'const x = gql`query { stories { id comments { text } } }`' + 'const x = gql`query { stories { id comments { text } } }`', + 'const x = gql`query { greetings { id, hello, foo } }`' ], fail: [ {