From 8847078f260e98aeac45ac8124e44b531e50b333 Mon Sep 17 00:00:00 2001 From: Lee Byron Date: Thu, 14 Dec 2017 13:30:44 -0800 Subject: [PATCH] Simplify Unknown Args Validation TypeInfo has a utility this should be using. --- .../__tests__/KnownArgumentNames-test.js | 33 ++++++++++++++++++ src/validation/rules/KnownArgumentNames.js | 34 ++++++------------- 2 files changed, 44 insertions(+), 23 deletions(-) diff --git a/src/validation/__tests__/KnownArgumentNames-test.js b/src/validation/__tests__/KnownArgumentNames-test.js index bb80b079f7..7cf5d67959 100644 --- a/src/validation/__tests__/KnownArgumentNames-test.js +++ b/src/validation/__tests__/KnownArgumentNames-test.js @@ -134,6 +134,18 @@ describe('Validate: Known argument names', () => { ); }); + it('misspelled directive args are reported', () => { + expectFailsRule( + KnownArgumentNames, + ` + { + dog @skip(iff: true) + } + `, + [unknownDirectiveArg('iff', 'skip', ['if'], 3, 19)], + ); + }); + it('invalid arg name', () => { expectFailsRule( KnownArgumentNames, @@ -146,6 +158,27 @@ describe('Validate: Known argument names', () => { ); }); + it('misspelled arg name is reported', () => { + expectFailsRule( + KnownArgumentNames, + ` + fragment invalidArgName on Dog { + doesKnowCommand(dogcommand: true) + } + `, + [ + unknownArg( + 'dogcommand', + 'doesKnowCommand', + 'Dog', + ['dogCommand'], + 3, + 25, + ), + ], + ); + }); + it('unknown args amongst known args', () => { expectFailsRule( KnownArgumentNames, diff --git a/src/validation/rules/KnownArgumentNames.js b/src/validation/rules/KnownArgumentNames.js index 77b81a9344..eb8b120597 100644 --- a/src/validation/rules/KnownArgumentNames.js +++ b/src/validation/rules/KnownArgumentNames.js @@ -9,11 +9,9 @@ import type { ValidationContext } from '../index'; import { GraphQLError } from '../../error'; -import find from '../../jsutils/find'; -import invariant from '../../jsutils/invariant'; import suggestionList from '../../jsutils/suggestionList'; import quotedOrList from '../../jsutils/quotedOrList'; -import * as Kind from '../../language/kinds'; +import { FIELD, DIRECTIVE } from '../../language/kinds'; export function unknownArgMessage( argName: string, @@ -53,17 +51,13 @@ export function unknownDirectiveArgMessage( export function KnownArgumentNames(context: ValidationContext): any { return { Argument(node, key, parent, path, ancestors) { - const argumentOf = ancestors[ancestors.length - 1]; - if (argumentOf.kind === Kind.FIELD) { - const fieldDef = context.getFieldDef(); - if (fieldDef) { - const fieldArgDef = find( - fieldDef.args, - arg => arg.name === node.name.value, - ); - if (!fieldArgDef) { - const parentType = context.getParentType(); - invariant(parentType); + const argDef = context.getArgument(); + if (!argDef) { + const argumentOf = ancestors[ancestors.length - 1]; + if (argumentOf.kind === FIELD) { + const fieldDef = context.getFieldDef(); + const parentType = context.getParentType(); + if (fieldDef && parentType) { context.reportError( new GraphQLError( unknownArgMessage( @@ -79,15 +73,9 @@ export function KnownArgumentNames(context: ValidationContext): any { ), ); } - } - } else if (argumentOf.kind === Kind.DIRECTIVE) { - const directive = context.getDirective(); - if (directive) { - const directiveArgDef = find( - directive.args, - arg => arg.name === node.name.value, - ); - if (!directiveArgDef) { + } else if (argumentOf.kind === DIRECTIVE) { + const directive = context.getDirective(); + if (directive) { context.reportError( new GraphQLError( unknownDirectiveArgMessage(