Skip to content

Commit 1aa12df

Browse files
authored
Simplify Unknown Args Validation (#1147)
TypeInfo has a utility this should be using.
1 parent ce0a4b9 commit 1aa12df

File tree

2 files changed

+44
-23
lines changed

2 files changed

+44
-23
lines changed

src/validation/__tests__/KnownArgumentNames-test.js

+33
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,18 @@ describe('Validate: Known argument names', () => {
134134
);
135135
});
136136

137+
it('misspelled directive args are reported', () => {
138+
expectFailsRule(
139+
KnownArgumentNames,
140+
`
141+
{
142+
dog @skip(iff: true)
143+
}
144+
`,
145+
[unknownDirectiveArg('iff', 'skip', ['if'], 3, 19)],
146+
);
147+
});
148+
137149
it('invalid arg name', () => {
138150
expectFailsRule(
139151
KnownArgumentNames,
@@ -146,6 +158,27 @@ describe('Validate: Known argument names', () => {
146158
);
147159
});
148160

161+
it('misspelled arg name is reported', () => {
162+
expectFailsRule(
163+
KnownArgumentNames,
164+
`
165+
fragment invalidArgName on Dog {
166+
doesKnowCommand(dogcommand: true)
167+
}
168+
`,
169+
[
170+
unknownArg(
171+
'dogcommand',
172+
'doesKnowCommand',
173+
'Dog',
174+
['dogCommand'],
175+
3,
176+
25,
177+
),
178+
],
179+
);
180+
});
181+
149182
it('unknown args amongst known args', () => {
150183
expectFailsRule(
151184
KnownArgumentNames,

src/validation/rules/KnownArgumentNames.js

+11-23
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,9 @@
99

1010
import type { ValidationContext } from '../index';
1111
import { GraphQLError } from '../../error';
12-
import find from '../../jsutils/find';
13-
import invariant from '../../jsutils/invariant';
1412
import suggestionList from '../../jsutils/suggestionList';
1513
import quotedOrList from '../../jsutils/quotedOrList';
16-
import * as Kind from '../../language/kinds';
14+
import { FIELD, DIRECTIVE } from '../../language/kinds';
1715

1816
export function unknownArgMessage(
1917
argName: string,
@@ -53,17 +51,13 @@ export function unknownDirectiveArgMessage(
5351
export function KnownArgumentNames(context: ValidationContext): any {
5452
return {
5553
Argument(node, key, parent, path, ancestors) {
56-
const argumentOf = ancestors[ancestors.length - 1];
57-
if (argumentOf.kind === Kind.FIELD) {
58-
const fieldDef = context.getFieldDef();
59-
if (fieldDef) {
60-
const fieldArgDef = find(
61-
fieldDef.args,
62-
arg => arg.name === node.name.value,
63-
);
64-
if (!fieldArgDef) {
65-
const parentType = context.getParentType();
66-
invariant(parentType);
54+
const argDef = context.getArgument();
55+
if (!argDef) {
56+
const argumentOf = ancestors[ancestors.length - 1];
57+
if (argumentOf.kind === FIELD) {
58+
const fieldDef = context.getFieldDef();
59+
const parentType = context.getParentType();
60+
if (fieldDef && parentType) {
6761
context.reportError(
6862
new GraphQLError(
6963
unknownArgMessage(
@@ -79,15 +73,9 @@ export function KnownArgumentNames(context: ValidationContext): any {
7973
),
8074
);
8175
}
82-
}
83-
} else if (argumentOf.kind === Kind.DIRECTIVE) {
84-
const directive = context.getDirective();
85-
if (directive) {
86-
const directiveArgDef = find(
87-
directive.args,
88-
arg => arg.name === node.name.value,
89-
);
90-
if (!directiveArgDef) {
76+
} else if (argumentOf.kind === DIRECTIVE) {
77+
const directive = context.getDirective();
78+
if (directive) {
9179
context.reportError(
9280
new GraphQLError(
9381
unknownDirectiveArgMessage(

0 commit comments

Comments
 (0)