|
| 1 | +import { Kind } from 'graphql'; |
| 2 | +import { GraphQLESLintRule } from '../types'; |
| 3 | + |
| 4 | +const AVOID_DUPLICATE_FIELDS = 'AVOID_DUPLICATE_FIELDS'; |
| 5 | + |
| 6 | +const ensureUnique = () => { |
| 7 | + const set = new Set<string>(); |
| 8 | + |
| 9 | + return { |
| 10 | + add: (item: string, onError: () => void) => { |
| 11 | + if (set.has(item)) { |
| 12 | + onError(); |
| 13 | + } else { |
| 14 | + set.add(item); |
| 15 | + } |
| 16 | + }, |
| 17 | + }; |
| 18 | +}; |
| 19 | + |
| 20 | +const rule: GraphQLESLintRule<[], false> = { |
| 21 | + meta: { |
| 22 | + type: 'suggestion', |
| 23 | + docs: { |
| 24 | + requiresSchema: false, |
| 25 | + requiresSiblings: false, |
| 26 | + description: |
| 27 | + 'Checks for duplicate fields in selection set, variables in operation definition, or in arguments set of a field.', |
| 28 | + category: 'Stylistic Issues', |
| 29 | + url: 'https://github.com/dotansimha/graphql-eslint/blob/master/docs/rules/avoid-duplicate-fields.md', |
| 30 | + examples: [ |
| 31 | + { |
| 32 | + title: 'Incorrect', |
| 33 | + code: /* GraphQL */ ` |
| 34 | + query getUserDetails { |
| 35 | + user { |
| 36 | + name # first |
| 37 | + email |
| 38 | + name # second |
| 39 | + } |
| 40 | + } |
| 41 | + `, |
| 42 | + }, |
| 43 | + { |
| 44 | + title: 'Incorrect', |
| 45 | + code: /* GraphQL */ ` |
| 46 | + query getUsers { |
| 47 | + users( |
| 48 | + first: 100 |
| 49 | + skip: 50 |
| 50 | + after: "cji629tngfgou0b73kt7vi5jo" |
| 51 | + first: 100 # duplicate argument |
| 52 | + ) { |
| 53 | + id |
| 54 | + } |
| 55 | + } |
| 56 | + `, |
| 57 | + }, |
| 58 | + { |
| 59 | + title: 'Incorrect', |
| 60 | + code: /* GraphQL */ ` |
| 61 | + query getUsers($first: Int!, $first: Int!) { |
| 62 | + # Duplicate variable |
| 63 | + users(first: 100, skip: 50, after: "cji629tngfgou0b73kt7vi5jo") { |
| 64 | + id |
| 65 | + } |
| 66 | + } |
| 67 | + `, |
| 68 | + }, |
| 69 | + ], |
| 70 | + }, |
| 71 | + messages: { |
| 72 | + [AVOID_DUPLICATE_FIELDS]: `{{ type }} "{{ fieldName }}" defined multiple times.`, |
| 73 | + }, |
| 74 | + }, |
| 75 | + create(context) { |
| 76 | + return { |
| 77 | + OperationDefinition(node) { |
| 78 | + const uniqueCheck = ensureUnique(); |
| 79 | + |
| 80 | + for (const arg of node.variableDefinitions || []) { |
| 81 | + uniqueCheck.add(arg.variable.name.value, () => { |
| 82 | + context.report({ |
| 83 | + messageId: AVOID_DUPLICATE_FIELDS, |
| 84 | + data: { |
| 85 | + type: 'Operation variable', |
| 86 | + fieldName: arg.variable.name.value, |
| 87 | + }, |
| 88 | + node: arg, |
| 89 | + }); |
| 90 | + }); |
| 91 | + } |
| 92 | + }, |
| 93 | + Field(node) { |
| 94 | + const uniqueCheck = ensureUnique(); |
| 95 | + |
| 96 | + for (const arg of node.arguments || []) { |
| 97 | + uniqueCheck.add(arg.name.value, () => { |
| 98 | + context.report({ |
| 99 | + messageId: AVOID_DUPLICATE_FIELDS, |
| 100 | + data: { |
| 101 | + type: 'Field argument', |
| 102 | + fieldName: arg.name.value, |
| 103 | + }, |
| 104 | + node: arg, |
| 105 | + }); |
| 106 | + }); |
| 107 | + } |
| 108 | + }, |
| 109 | + SelectionSet(node) { |
| 110 | + const uniqueCheck = ensureUnique(); |
| 111 | + |
| 112 | + for (const selection of node.selections || []) { |
| 113 | + if (selection.kind === Kind.FIELD) { |
| 114 | + const nameToCheck = selection.alias?.value || selection.name.value; |
| 115 | + |
| 116 | + uniqueCheck.add(nameToCheck, () => { |
| 117 | + context.report({ |
| 118 | + messageId: AVOID_DUPLICATE_FIELDS, |
| 119 | + data: { |
| 120 | + type: 'Field', |
| 121 | + fieldName: nameToCheck, |
| 122 | + }, |
| 123 | + node: selection, |
| 124 | + }); |
| 125 | + }); |
| 126 | + } |
| 127 | + } |
| 128 | + }, |
| 129 | + }; |
| 130 | + }, |
| 131 | +}; |
| 132 | + |
| 133 | +export default rule; |
0 commit comments