|
| 1 | +import { requireGraphQLSchemaFromContext } from '../utils'; |
| 2 | +import { GraphQLESLintRule } from '../types'; |
| 3 | + |
| 4 | +const NO_DEPRECATED = 'NO_DEPRECATED'; |
| 5 | + |
| 6 | +const rule: GraphQLESLintRule<[], true> = { |
| 7 | + meta: { |
| 8 | + type: 'suggestion', |
| 9 | + docs: { |
| 10 | + category: 'Best Practices', |
| 11 | + description: `This rule allow you to enforce that deprecated fields or enum values are not in use by operations.`, |
| 12 | + url: `https://github.com/dotansimha/graphql-eslint/blob/master/docs/rules/no-deprecated.md`, |
| 13 | + requiresSchema: true, |
| 14 | + requiresSiblings: false, |
| 15 | + examples: [ |
| 16 | + { |
| 17 | + title: 'Incorrect (field)', |
| 18 | + code: /* GraphQL */ ` |
| 19 | + # In your schema |
| 20 | + type User { |
| 21 | + id: ID! |
| 22 | + name: String! @deprecated(reason: "old field, please use fullName instead") |
| 23 | + fullName: String! |
| 24 | + } |
| 25 | +
|
| 26 | + # Query |
| 27 | + query user { |
| 28 | + user { |
| 29 | + name # This is deprecated, so you'll get an error |
| 30 | + } |
| 31 | + } |
| 32 | + `, |
| 33 | + }, |
| 34 | + { |
| 35 | + title: 'Incorrect (enum value)', |
| 36 | + code: /* GraphQL */ ` |
| 37 | + # In your schema |
| 38 | + type Mutation { |
| 39 | + changeSomething(type: SomeType): Boolean! |
| 40 | + } |
| 41 | +
|
| 42 | + enum SomeType { |
| 43 | + NEW |
| 44 | + OLD @deprecated(reason: "old field, please use NEW instead") |
| 45 | + } |
| 46 | +
|
| 47 | + # Mutation |
| 48 | + mutation { |
| 49 | + changeSomething( |
| 50 | + type: OLD # This is deprecated, so you'll get an error |
| 51 | + ) { |
| 52 | + ... |
| 53 | + } |
| 54 | + } |
| 55 | + `, |
| 56 | + }, |
| 57 | + { |
| 58 | + title: 'Correct', |
| 59 | + code: /* GraphQL */ ` |
| 60 | + # In your schema |
| 61 | + type User { |
| 62 | + id: ID! |
| 63 | + name: String! @deprecated(reason: "old field, please use fullName instead") |
| 64 | + fullName: String! |
| 65 | + } |
| 66 | +
|
| 67 | + # Query |
| 68 | + query user { |
| 69 | + user { |
| 70 | + id |
| 71 | + fullName |
| 72 | + } |
| 73 | + } |
| 74 | + `, |
| 75 | + }, |
| 76 | + ], |
| 77 | + }, |
| 78 | + messages: { |
| 79 | + [NO_DEPRECATED]: `This {{ type }} is marked as deprecated in your GraphQL schema {{ reason }}`, |
| 80 | + }, |
| 81 | + }, |
| 82 | + create(context) { |
| 83 | + return { |
| 84 | + EnumValue(node) { |
| 85 | + requireGraphQLSchemaFromContext('no-deprecated', context); |
| 86 | + const typeInfo = node.typeInfo(); |
| 87 | + |
| 88 | + if (typeInfo && typeInfo.enumValue) { |
| 89 | + if (typeInfo.enumValue.isDeprecated) { |
| 90 | + context.report({ |
| 91 | + loc: node.loc, |
| 92 | + messageId: NO_DEPRECATED, |
| 93 | + data: { |
| 94 | + type: 'enum value', |
| 95 | + reason: typeInfo.enumValue.deprecationReason ? `(reason: ${typeInfo.enumValue.deprecationReason})` : '', |
| 96 | + }, |
| 97 | + }); |
| 98 | + } |
| 99 | + } |
| 100 | + }, |
| 101 | + Field(node) { |
| 102 | + requireGraphQLSchemaFromContext('no-deprecated', context); |
| 103 | + const typeInfo = node.typeInfo(); |
| 104 | + |
| 105 | + if (typeInfo && typeInfo.fieldDef) { |
| 106 | + if (typeInfo.fieldDef.isDeprecated) { |
| 107 | + context.report({ |
| 108 | + loc: node.loc, |
| 109 | + messageId: NO_DEPRECATED, |
| 110 | + data: { |
| 111 | + type: 'field', |
| 112 | + reason: typeInfo.fieldDef.deprecationReason ? `(reason: ${typeInfo.fieldDef.deprecationReason})` : '', |
| 113 | + }, |
| 114 | + }); |
| 115 | + } |
| 116 | + } |
| 117 | + }, |
| 118 | + }; |
| 119 | + }, |
| 120 | +}; |
| 121 | + |
| 122 | +export default rule; |
0 commit comments