deno.land / x / pothos@release-1713397530 / packages / plugin-complexity / validator.ts

نووسراو ببینە
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
// @ts-nocheckimport { FragmentDefinitionNode, GraphQLError, Kind, ValidationRule } from 'https://cdn.skypack.dev/graphql?dts';import { PothosValidationError } from '../core/index.ts';import { complexityFromSelectionSet } from './calculate-complexity.ts';export function createComplexityRule({ variableValues, context, maxComplexity, maxBreadth, maxDepth, validate, onResult, }: { context: object; variableValues: Record<string, unknown>; maxComplexity?: number; maxDepth?: number; maxBreadth?: number; validate?: (result: { complexity: number; depth: number; breadth: number; }, reportError: (error: GraphQLError) => void) => void; onResult?: (result: { complexity: number; depth: number; breadth: number; }, errors: GraphQLError[]) => void;}) { const complexityValidationRule: ValidationRule = (validationContext) => { const state = { complexity: 0, depth: 0, breadth: 0, }; const schema = validationContext.getSchema(); const fragments: Record<string, FragmentDefinitionNode> = {}; validationContext.getDocument().definitions.forEach((def) => { if (def.kind === Kind.FRAGMENT_DEFINITION) { fragments[def.name.value] = def; } }); return { OperationDefinition: { enter: (node) => { const type = schema.getRootType(node.operation); if (!type) { throw new PothosValidationError(`Could not find root type for operation ${node.operation}`); } const complexity = complexityFromSelectionSet(context, { fragments, variableValues, schema, }, node.selectionSet, type); state.complexity += complexity.complexity; state.depth = Math.max(state.depth, complexity.depth); state.breadth = Math.max(state.breadth, complexity.breadth); }, leave: () => { const errors: GraphQLError[] = []; const reportError = (error: GraphQLError) => { errors.push(error); }; if (validate) { validate(state, (error) => { reportError(error); }); } else { if (typeof maxComplexity === "number" && state.complexity > maxComplexity) { reportError(new GraphQLError(`Query complexity of ${state.complexity} exceeds max complexity of ${maxComplexity}`, { extensions: { queryComplexity: { max: maxComplexity, actual: state.complexity, }, code: "QUERY_COMPLEXITY", }, })); } if (typeof maxDepth === "number" && state.depth > maxDepth) { reportError(new GraphQLError(`Query depth of ${state.depth} exceeds max depth of ${maxDepth}`, { extensions: { queryDepth: { max: maxDepth, actual: state.depth, }, code: "QUERY_DEPTH", }, })); } if (typeof maxBreadth === "number" && state.breadth > maxBreadth) { reportError(new GraphQLError(`Query breadth of ${state.breadth} exceeds max breadth of ${maxBreadth}`, { extensions: { queryBreadth: { max: maxBreadth, actual: state.breadth, }, code: "QUERY_BREADTH", }, })); } } for (const error of errors) { validationContext.reportError(error); } onResult?.(state, errors); }, }, }; }; return complexityValidationRule;}
pothos

Version Info

Tagged at
a year ago