Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify Unknown Args Validation #1147

Merged
merged 1 commit into from
Dec 14, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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