|
| 1 | +import { GraphQLESLintRule } from '../types'; |
| 2 | +import depthLimit from 'graphql-depth-limit'; |
| 3 | +import { DocumentNode, FragmentDefinitionNode, GraphQLError, Kind, OperationDefinitionNode } from 'graphql'; |
| 4 | +import { GraphQLESTreeNode } from '../estree-parser'; |
| 5 | +import { requireSiblingsOperations } from '../utils'; |
| 6 | +import { SiblingOperations } from '../sibling-operations'; |
| 7 | + |
| 8 | +type SelectionSetDepthRuleConfig = [{ maxDepth: number; ignore?: string[] }]; |
| 9 | + |
| 10 | +const rule: GraphQLESLintRule<SelectionSetDepthRuleConfig> = { |
| 11 | + meta: { |
| 12 | + docs: { |
| 13 | + category: 'Best Practices', |
| 14 | + description: `Limit the complexity of the GraphQL operations solely by their depth. Based on https://github.com/stems/graphql-depth-limit .`, |
| 15 | + url: `https://github.com/dotansimha/graphql-eslint/blob/master/docs/rules/selection-set-depth.md`, |
| 16 | + requiresSchema: false, |
| 17 | + requiresSiblings: true, |
| 18 | + examples: [ |
| 19 | + { |
| 20 | + title: 'Incorrect', |
| 21 | + usage: [{ maxDepth: 1 }], |
| 22 | + code: ` |
| 23 | + query deep2 { |
| 24 | + viewer { # Level 0 |
| 25 | + albums { # Level 1 |
| 26 | + title # Level 2 |
| 27 | + } |
| 28 | + } |
| 29 | + } |
| 30 | + `, |
| 31 | + }, |
| 32 | + { |
| 33 | + title: 'Correct', |
| 34 | + usage: [{ maxDepth: 4 }], |
| 35 | + code: ` |
| 36 | + query deep2 { |
| 37 | + viewer { # Level 0 |
| 38 | + albums { # Level 1 |
| 39 | + title # Level 2 |
| 40 | + } |
| 41 | + } |
| 42 | + } |
| 43 | + `, |
| 44 | + }, |
| 45 | + { |
| 46 | + title: 'Correct (ignored field)', |
| 47 | + usage: [{ maxDepth: 1, ignore: ['albums'] }], |
| 48 | + code: ` |
| 49 | + query deep2 { |
| 50 | + viewer { # Level 0 |
| 51 | + albums { # Level 1 |
| 52 | + title # Level 2 |
| 53 | + } |
| 54 | + } |
| 55 | + } |
| 56 | + `, |
| 57 | + }, |
| 58 | + ], |
| 59 | + }, |
| 60 | + type: 'suggestion', |
| 61 | + schema: { |
| 62 | + type: 'array', |
| 63 | + additionalItems: false, |
| 64 | + minItems: 1, |
| 65 | + maxItems: 1, |
| 66 | + items: { |
| 67 | + type: 'object', |
| 68 | + require: ['maxDepth'], |
| 69 | + properties: { |
| 70 | + maxDepth: { |
| 71 | + type: 'number', |
| 72 | + }, |
| 73 | + ignore: { |
| 74 | + type: 'array', |
| 75 | + items: { |
| 76 | + type: 'string', |
| 77 | + }, |
| 78 | + }, |
| 79 | + }, |
| 80 | + }, |
| 81 | + }, |
| 82 | + }, |
| 83 | + create(context) { |
| 84 | + let siblings: SiblingOperations | null = null; |
| 85 | + |
| 86 | + try { |
| 87 | + siblings = requireSiblingsOperations('selection-set-depth', context); |
| 88 | + } catch (e) { |
| 89 | + // eslint-disable-next-line no-console |
| 90 | + console.warn( |
| 91 | + `Rule "selection-set-depth" works best with sibligns operations loaded. For more info: http://bit.ly/graphql-eslint-operations` |
| 92 | + ); |
| 93 | + } |
| 94 | + |
| 95 | + const maxDepth = context.options[0].maxDepth; |
| 96 | + const ignore = context.options[0].ignore || []; |
| 97 | + const checkFn = depthLimit(maxDepth, { ignore }); |
| 98 | + |
| 99 | + return ['OperationDefinition', 'FragmentDefinition'].reduce((prev, nodeType) => { |
| 100 | + return { |
| 101 | + ...prev, |
| 102 | + [nodeType]: (node: GraphQLESTreeNode<OperationDefinitionNode> | GraphQLESTreeNode<FragmentDefinitionNode>) => { |
| 103 | + try { |
| 104 | + const rawNode = node.rawNode(); |
| 105 | + const fragmentsInUse = siblings ? siblings.getFragmentsInUse(rawNode, true) : []; |
| 106 | + const document: DocumentNode = { |
| 107 | + kind: Kind.DOCUMENT, |
| 108 | + definitions: [rawNode, ...fragmentsInUse], |
| 109 | + }; |
| 110 | + |
| 111 | + checkFn({ |
| 112 | + getDocument: () => document, |
| 113 | + reportError: (error: GraphQLError) => { |
| 114 | + context.report({ |
| 115 | + loc: error.locations[0], |
| 116 | + message: error.message, |
| 117 | + }); |
| 118 | + }, |
| 119 | + }); |
| 120 | + } catch (e) { |
| 121 | + // eslint-disable-next-line no-console |
| 122 | + console.warn( |
| 123 | + `Rule "selection-set-depth" check failed due to a missing siblings operations. For more info: http://bit.ly/graphql-eslint-operations`, |
| 124 | + e |
| 125 | + ); |
| 126 | + } |
| 127 | + }, |
| 128 | + }; |
| 129 | + }, {}); |
| 130 | + }, |
| 131 | +}; |
| 132 | + |
| 133 | +export default rule; |
0 commit comments