Skip to content

Commit

Permalink
Simplify Unknown Args Validation
Browse files Browse the repository at this point in the history
TypeInfo has a utility this should be using.
  • Loading branch information
leebyron committed Dec 14, 2017
1 parent ce0a4b9 commit 8847078
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 23 deletions.
33 changes: 33 additions & 0 deletions src/validation/__tests__/KnownArgumentNames-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down
34 changes: 11 additions & 23 deletions src/validation/rules/KnownArgumentNames.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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(
Expand All @@ -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(
Expand Down

0 comments on commit 8847078

Please sign in to comment.