Skip to content

Commit

Permalink
validateSchema: inline 'getAllNodes' function
Browse files Browse the repository at this point in the history
  • Loading branch information
IvanGoncharov committed Feb 21, 2021
1 parent 334ceb0 commit b122456
Showing 1 changed file with 9 additions and 25 deletions.
34 changes: 9 additions & 25 deletions src/type/validate.js
Expand Up @@ -143,13 +143,9 @@ function getOperationTypeNode(
schema: GraphQLSchema,
operation: OperationTypeNode,
): ?ASTNode {
const operationNodes = getAllSubNodes(schema, (node) => node.operationTypes);
for (const node of operationNodes) {
if (node.operation === operation) {
return node.type;
}
}
return undefined;
return getAllNodes(schema)
.flatMap((schemaNode) => schemaNode.operationTypes ?? [])
.find((operationNode) => operationNode.operation === operation).type;
}

function validateDirectives(context: SchemaValidationContext): void {
Expand Down Expand Up @@ -626,34 +622,22 @@ function getAllNodes<T: ASTNode, K: ASTNode>(
: extensionASTNodes ?? [];
}

function getAllSubNodes<T: ASTNode, K: ASTNode, L: ASTNode>(
object: SDLDefinedObject<T, K>,
getter: (T | K) => ?(L | $ReadOnlyArray<L>),
): $ReadOnlyArray<L> {
let subNodes = [];
for (const node of getAllNodes(object)) {
// istanbul ignore next (See: 'https://github.com/graphql/graphql-js/issues/2203')
subNodes = subNodes.concat(getter(node) ?? []);
}
return subNodes;
}

function getAllImplementsInterfaceNodes(
type: GraphQLObjectType | GraphQLInterfaceType,
iface: GraphQLInterfaceType,
): $ReadOnlyArray<NamedTypeNode> {
return getAllSubNodes(type, (typeNode) => typeNode.interfaces).filter(
(ifaceNode) => ifaceNode.name.value === iface.name,
);
return getAllNodes(type)
.flatMap((typeNode) => typeNode.interfaces ?? [])
.filter((ifaceNode) => ifaceNode.name.value === iface.name);
}

function getUnionMemberTypeNodes(
union: GraphQLUnionType,
typeName: string,
): ?$ReadOnlyArray<NamedTypeNode> {
return getAllSubNodes(union, (unionNode) => unionNode.types).filter(
(typeNode) => typeNode.name.value === typeName,
);
return getAllNodes(union)
.flatMap((unionNode) => unionNode.types ?? [])
.filter((typeNode) => typeNode.name.value === typeName);
}

function getDeprecatedDirectiveNode(
Expand Down

0 comments on commit b122456

Please sign in to comment.