Skip to content

Commit 480f774

Browse files
committed
Simplify Unknown Args Validation
TypeInfo has a utility this should be using.
1 parent ce0a4b9 commit 480f774

File tree

1 file changed

+11
-23
lines changed

1 file changed

+11
-23
lines changed

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)