|
1 |
| -import { GraphQLESLintRule } from "@graphql-eslint/types"; |
| 1 | +import { GraphQLESLintRule, getBaseType } from "@graphql-eslint/types"; |
2 | 2 | import { GraphQLInterfaceType, GraphQLObjectType } from "graphql";
|
3 | 3 |
|
4 | 4 | const REQUIRE_ID_WHEN_AVAILABLE = "REQUIRE_ID_WHEN_AVAILABLE";
|
5 |
| -const ID_FIELD_NAME = "id"; |
| 5 | +const DEFAULT_ID_FIELD_NAME = "id"; |
6 | 6 |
|
7 |
| -const rule: GraphQLESLintRule<any, true> = { |
| 7 | +type RequireIdWhenAvailableRuleConfig = { fieldName: string }; |
| 8 | + |
| 9 | +const rule: GraphQLESLintRule<RequireIdWhenAvailableRuleConfig, true> = { |
8 | 10 | meta: {
|
9 | 11 | messages: {
|
10 |
| - [REQUIRE_ID_WHEN_AVAILABLE]: `Field "id" must be selected when it's available on a type. Please make sure to include it in your selection set!`, |
| 12 | + [REQUIRE_ID_WHEN_AVAILABLE]: `Field "{{ fieldName }}" must be selected when it's available on a type. Please make sure to include it in your selection set!`, |
| 13 | + }, |
| 14 | + schema: { |
| 15 | + type: "array", |
| 16 | + additionalItems: false, |
| 17 | + minItems: 0, |
| 18 | + maxItems: 1, |
| 19 | + items: { |
| 20 | + type: "object", |
| 21 | + properties: { |
| 22 | + fieldName: { |
| 23 | + type: "string", |
| 24 | + default: DEFAULT_ID_FIELD_NAME, |
| 25 | + }, |
| 26 | + }, |
| 27 | + }, |
11 | 28 | },
|
12 | 29 | },
|
13 | 30 | create(context) {
|
14 | 31 | return {
|
15 | 32 | SelectionSet(node) {
|
16 |
| - if (!node.selections || node.selections.length > 0) { |
| 33 | + const fieldName = |
| 34 | + (context.options[0] || {}).fieldName || DEFAULT_ID_FIELD_NAME; |
| 35 | + |
| 36 | + if (!node.selections || node.selections.length === 0) { |
17 | 37 | return;
|
18 | 38 | }
|
19 | 39 |
|
20 | 40 | if (node.typeInfo && node.typeInfo.gqlType) {
|
| 41 | + const rawType = getBaseType(node.typeInfo.gqlType); |
21 | 42 | if (
|
22 |
| - node.typeInfo.gqlType instanceof GraphQLObjectType || |
23 |
| - node.typeInfo.gqlType instanceof GraphQLInterfaceType |
| 43 | + rawType instanceof GraphQLObjectType || |
| 44 | + rawType instanceof GraphQLInterfaceType |
24 | 45 | ) {
|
25 |
| - const fields = node.typeInfo.gqlType.getFields(); |
26 |
| - const hasIdFieldInType = !!fields[ID_FIELD_NAME]; |
27 |
| - const hasIdFieldInSelectionSet = !!node.selections.find( |
28 |
| - (s) => s.kind === "Field" && s.name.value === ID_FIELD_NAME |
29 |
| - ); |
30 |
| - |
31 |
| - if (hasIdFieldInType && !hasIdFieldInSelectionSet) { |
32 |
| - context.report({ |
33 |
| - loc: { |
34 |
| - start: { |
35 |
| - line: node.loc.start.line, |
36 |
| - column: node.loc.start.column - 1, |
| 46 | + const fields = rawType.getFields(); |
| 47 | + const hasIdFieldInType = !!fields[fieldName]; |
| 48 | + |
| 49 | + if (hasIdFieldInType) { |
| 50 | + const hasIdFieldInSelectionSet = !!node.selections.find( |
| 51 | + (s) => s.kind === "Field" && s.name.value === fieldName |
| 52 | + ); |
| 53 | + |
| 54 | + if (!hasIdFieldInSelectionSet) { |
| 55 | + context.report({ |
| 56 | + loc: { |
| 57 | + start: { |
| 58 | + line: node.loc.start.line, |
| 59 | + column: node.loc.start.column - 1, |
| 60 | + }, |
| 61 | + end: { |
| 62 | + line: node.loc.end.line, |
| 63 | + column: node.loc.end.column - 1, |
| 64 | + }, |
37 | 65 | },
|
38 |
| - end: { |
39 |
| - line: node.loc.end.line, |
40 |
| - column: node.loc.end.column - 1, |
| 66 | + messageId: REQUIRE_ID_WHEN_AVAILABLE, |
| 67 | + data: { |
| 68 | + fieldName, |
41 | 69 | },
|
42 |
| - }, |
43 |
| - messageId: REQUIRE_ID_WHEN_AVAILABLE, |
44 |
| - data: { |
45 |
| - nodeType: node.kind, |
46 |
| - }, |
47 |
| - }); |
| 70 | + }); |
| 71 | + } |
48 | 72 | }
|
49 | 73 | }
|
50 | 74 | }
|
|
0 commit comments