|
1 |
| -import { Kind, validate, GraphQLSchema, DocumentNode } from 'graphql'; |
| 1 | +import { Kind, validate, GraphQLSchema, DocumentNode, ASTNode, ValidationRule, specifiedRules } from 'graphql'; |
2 | 2 | import { GraphQLESTreeNode } from '../estree-parser';
|
3 | 3 | import { GraphQLESLintRule, GraphQLESlintRuleContext } from '../types';
|
4 | 4 | import { requireGraphQLSchemaFromContext } from '../utils';
|
5 | 5 |
|
6 |
| -function validateDoc(context: GraphQLESlintRuleContext, schema: GraphQLSchema, documentNode: DocumentNode) { |
| 6 | +function validateDoc( |
| 7 | + sourceNode: GraphQLESTreeNode<ASTNode>, |
| 8 | + context: GraphQLESlintRuleContext, |
| 9 | + schema: GraphQLSchema, |
| 10 | + documentNode: DocumentNode, |
| 11 | + rules: ReadonlyArray<ValidationRule> |
| 12 | +) { |
7 | 13 | if (documentNode && documentNode.definitions && documentNode.definitions.length > 0) {
|
8 |
| - const validationErrors = validate(schema, documentNode); |
| 14 | + try { |
| 15 | + const validationErrors = validate(schema, documentNode, rules); |
9 | 16 |
|
10 |
| - for (const error of validationErrors) { |
11 |
| - const node = (error.nodes[0] as any) as GraphQLESTreeNode<typeof error.nodes[0]>; |
| 17 | + for (const error of validationErrors) { |
| 18 | + const node = (error.nodes[0] as any) as GraphQLESTreeNode<ASTNode>; |
12 | 19 |
|
| 20 | + context.report({ |
| 21 | + loc: node.loc, |
| 22 | + message: error.message, |
| 23 | + }); |
| 24 | + } |
| 25 | + } catch (e) { |
13 | 26 | context.report({
|
14 |
| - loc: node.loc, |
15 |
| - message: error.message, |
| 27 | + node: sourceNode, |
| 28 | + message: e.message, |
16 | 29 | });
|
17 | 30 | }
|
18 | 31 | }
|
19 | 32 | }
|
20 | 33 |
|
21 |
| -const rule: GraphQLESLintRule = { |
| 34 | +export type ValidateAgainstSchemaRuleConfig = [ |
| 35 | + { |
| 36 | + overrideRules?: string[]; |
| 37 | + disableRules?: string[]; |
| 38 | + } |
| 39 | +]; |
| 40 | + |
| 41 | +const rule: GraphQLESLintRule<ValidateAgainstSchemaRuleConfig> = { |
22 | 42 | meta: {
|
23 | 43 | docs: {
|
24 | 44 | url: `https://github.com/dotansimha/graphql-eslint/blob/master/docs/rules/validate-against-schema.md`,
|
25 | 45 | recommended: true,
|
26 | 46 | description: `This rule validates GraphQL operations against your GraphQL schema, and reflects the error as lint errors.`,
|
27 | 47 | },
|
| 48 | + schema: { |
| 49 | + type: 'array', |
| 50 | + minItems: 0, |
| 51 | + maxItems: 1, |
| 52 | + items: { |
| 53 | + allOf: [ |
| 54 | + { |
| 55 | + type: 'object', |
| 56 | + properties: { |
| 57 | + overrideRules: { |
| 58 | + type: 'array', |
| 59 | + items: { |
| 60 | + type: 'string', |
| 61 | + }, |
| 62 | + }, |
| 63 | + }, |
| 64 | + }, |
| 65 | + { |
| 66 | + type: 'object', |
| 67 | + properties: { |
| 68 | + disableRules: { |
| 69 | + type: 'array', |
| 70 | + items: { |
| 71 | + type: 'string', |
| 72 | + }, |
| 73 | + }, |
| 74 | + }, |
| 75 | + }, |
| 76 | + ], |
| 77 | + }, |
| 78 | + }, |
28 | 79 | type: 'problem',
|
29 | 80 | },
|
30 | 81 | create(context) {
|
| 82 | + const config = context.options[0] || {}; |
| 83 | + let rulesArr = specifiedRules; |
| 84 | + |
| 85 | + if (config.disableRules && config.disableRules.length > 0) { |
| 86 | + rulesArr = specifiedRules.filter(r => !config.disableRules.includes(r.name)); |
| 87 | + } else if (config.overrideRules && config.overrideRules.length > 0) { |
| 88 | + rulesArr = specifiedRules.filter(r => config.overrideRules.includes(r.name)); |
| 89 | + } |
| 90 | + |
31 | 91 | return {
|
32 | 92 | OperationDefinition(node) {
|
33 | 93 | const schema = requireGraphQLSchemaFromContext(context);
|
34 |
| - |
35 |
| - validateDoc(context, schema, { |
36 |
| - kind: Kind.DOCUMENT, |
37 |
| - definitions: [node.rawNode()], |
38 |
| - }); |
| 94 | + validateDoc( |
| 95 | + node, |
| 96 | + context, |
| 97 | + schema, |
| 98 | + { |
| 99 | + kind: Kind.DOCUMENT, |
| 100 | + definitions: [node.rawNode()], |
| 101 | + }, |
| 102 | + rulesArr |
| 103 | + ); |
39 | 104 | },
|
40 | 105 | FragmentDefinition(node) {
|
41 | 106 | const schema = requireGraphQLSchemaFromContext(context);
|
42 |
| - |
43 |
| - validateDoc(context, schema, { |
44 |
| - kind: Kind.DOCUMENT, |
45 |
| - definitions: [node.rawNode()], |
46 |
| - }); |
| 107 | + validateDoc( |
| 108 | + node, |
| 109 | + context, |
| 110 | + schema, |
| 111 | + { |
| 112 | + kind: Kind.DOCUMENT, |
| 113 | + definitions: [node.rawNode()], |
| 114 | + }, |
| 115 | + rulesArr |
| 116 | + ); |
47 | 117 | },
|
48 | 118 | };
|
49 | 119 | },
|
|
0 commit comments