MCPcopy Create free account
hub / github.com/subquery/subql / checkDepth

Function checkDepth

packages/query/src/graphql/plugins/QueryDepthLimitPlugin.ts:50–85  ·  view source on GitHub ↗
(
  node: ASTNode,
  fragments: Record<string, FragmentDefinitionNode>,
  depthSoFar: number,
  maxDepth: number
)

Source from the content-addressed store, hash-verified

48}
49
50export function checkDepth(
51 node: ASTNode,
52 fragments: Record<string, FragmentDefinitionNode>,
53 depthSoFar: number,
54 maxDepth: number
55): void {
56 if (depthSoFar > maxDepth) {
57 throw new GraphQLError(`Query is too deep. Maximum depth allowed is ${maxDepth}.`, [node]);
58 }
59 switch (node.kind) {
60 case Kind.FIELD: {
61 if (!node.selectionSet) {
62 return;
63 }
64
65 node.selectionSet.selections.forEach((selection: SelectionNode) => {
66 checkDepth(selection, fragments, depthSoFar + 1, maxDepth);
67 });
68
69 return;
70 }
71 case Kind.FRAGMENT_SPREAD: {
72 return checkDepth(fragments[node.name.value], fragments, depthSoFar, maxDepth);
73 }
74 case Kind.INLINE_FRAGMENT:
75 case Kind.FRAGMENT_DEFINITION:
76 case Kind.OPERATION_DEFINITION: {
77 node.selectionSet.selections.forEach((selection: SelectionNode) => {
78 checkDepth(selection, fragments, depthSoFar, maxDepth);
79 });
80 return;
81 }
82 default:
83 break;
84 }
85}
86
87export function queryDepthLimitPlugin(options: {schema: GraphQLSchema; maxDepth?: number}): ApolloServerPlugin {
88 return {

Callers 2

validateQueryDepthFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected