Skip to content

Commit

Permalink
fix(required-fields): don't throw on non-existent field (#65)
Browse files Browse the repository at this point in the history
* Do not throw when referencing non-existent attr

* Test to reproduce thrown error

* Update CHANGELOG
  • Loading branch information
benjie authored and jnwng committed May 31, 2017
1 parent 9652e4e commit a37e7f0
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 3 additions & 0 deletions src/rules.js
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down
3 changes: 2 additions & 1 deletion test/makeRule.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: [
{
Expand Down

0 comments on commit a37e7f0

Please sign in to comment.