( node: ASTNode, fragments: Record<string, FragmentDefinitionNode>, depthSoFar: number, maxDepth: number )
| 48 | } |
| 49 | |
| 50 | export 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 | |
| 87 | export function queryDepthLimitPlugin(options: {schema: GraphQLSchema; maxDepth?: number}): ApolloServerPlugin { |
| 88 | return { |
no outgoing calls
no test coverage detected