Skip to content

Commit 302f4b9

Browse files
validateSchema: inline 'getAllNodes' function (#2937)
1 parent 334ceb0 commit 302f4b9

File tree

1 file changed

+12
-25
lines changed

1 file changed

+12
-25
lines changed

src/type/validate.js

Lines changed: 12 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -143,13 +143,10 @@ function getOperationTypeNode(
143143
schema: GraphQLSchema,
144144
operation: OperationTypeNode,
145145
): ?ASTNode {
146-
const operationNodes = getAllSubNodes(schema, (node) => node.operationTypes);
147-
for (const node of operationNodes) {
148-
if (node.operation === operation) {
149-
return node.type;
150-
}
151-
}
152-
return undefined;
146+
// istanbul ignore next (See: 'https://github.com/graphql/graphql-js/issues/2203')
147+
return getAllNodes(schema)
148+
.flatMap((schemaNode) => schemaNode.operationTypes ?? [])
149+
.find((operationNode) => operationNode.operation === operation)?.type;
153150
}
154151

155152
function validateDirectives(context: SchemaValidationContext): void {
@@ -626,34 +623,24 @@ function getAllNodes<T: ASTNode, K: ASTNode>(
626623
: extensionASTNodes ?? [];
627624
}
628625

629-
function getAllSubNodes<T: ASTNode, K: ASTNode, L: ASTNode>(
630-
object: SDLDefinedObject<T, K>,
631-
getter: (T | K) => ?(L | $ReadOnlyArray<L>),
632-
): $ReadOnlyArray<L> {
633-
let subNodes = [];
634-
for (const node of getAllNodes(object)) {
635-
// istanbul ignore next (See: 'https://github.com/graphql/graphql-js/issues/2203')
636-
subNodes = subNodes.concat(getter(node) ?? []);
637-
}
638-
return subNodes;
639-
}
640-
641626
function getAllImplementsInterfaceNodes(
642627
type: GraphQLObjectType | GraphQLInterfaceType,
643628
iface: GraphQLInterfaceType,
644629
): $ReadOnlyArray<NamedTypeNode> {
645-
return getAllSubNodes(type, (typeNode) => typeNode.interfaces).filter(
646-
(ifaceNode) => ifaceNode.name.value === iface.name,
647-
);
630+
// istanbul ignore next (See: 'https://github.com/graphql/graphql-js/issues/2203')
631+
return getAllNodes(type)
632+
.flatMap((typeNode) => typeNode.interfaces ?? [])
633+
.filter((ifaceNode) => ifaceNode.name.value === iface.name);
648634
}
649635

650636
function getUnionMemberTypeNodes(
651637
union: GraphQLUnionType,
652638
typeName: string,
653639
): ?$ReadOnlyArray<NamedTypeNode> {
654-
return getAllSubNodes(union, (unionNode) => unionNode.types).filter(
655-
(typeNode) => typeNode.name.value === typeName,
656-
);
640+
// istanbul ignore next (See: 'https://github.com/graphql/graphql-js/issues/2203')
641+
return getAllNodes(union)
642+
.flatMap((unionNode) => unionNode.types ?? [])
643+
.filter((typeNode) => typeNode.name.value === typeName);
657644
}
658645

659646
function getDeprecatedDirectiveNode(

0 commit comments

Comments
 (0)