UNPKG

@graphql-eslint/eslint-plugin

Version:
137 lines (132 loc) 3.56 kB
import { displayNodeName, requireGraphQLSchema } from "../../utils.js"; const RULE_ID = "no-deprecated", rule = { meta: { type: "suggestion", hasSuggestions: !0, docs: { category: "Operations", description: "Enforce that deprecated fields or enum values are not in use by operations.", url: `https://the-guild.dev/graphql/eslint/rules/${RULE_ID}`, requiresSchema: !0, examples: [ { title: "Incorrect (field)", code: ( /* GraphQL */ ` # In your schema type User { id: ID! name: String! @deprecated(reason: "old field, please use fullName instead") fullName: String! } # Query query user { user { name # This is deprecated, so you'll get an error } } ` ) }, { title: "Incorrect (enum value)", code: ( /* GraphQL */ ` # In your schema type Mutation { changeSomething(type: SomeType): Boolean! } enum SomeType { NEW OLD @deprecated(reason: "old field, please use NEW instead") } # Mutation mutation { changeSomething( type: OLD # This is deprecated, so you'll get an error ) { ... } } ` ) }, { title: "Correct", code: ( /* GraphQL */ ` # In your schema type User { id: ID! name: String! @deprecated(reason: "old field, please use fullName instead") fullName: String! } # Query query user { user { id fullName } } ` ) } ], recommended: !0 }, messages: { [RULE_ID]: "{{ type }} is marked as deprecated in your GraphQL schema (reason: {{ reason }})" }, schema: [] }, create(context) { requireGraphQLSchema(RULE_ID, context); function report(node, reason) { const nodeType = displayNodeName(node); context.report({ node, messageId: RULE_ID, data: { type: nodeType[0].toUpperCase() + nodeType.slice(1), reason }, suggest: [ { desc: `Remove ${nodeType}`, fix: (fixer) => fixer.remove(node) } ] }); } return { EnumValue(node) { const reason = node.typeInfo().enumValue?.deprecationReason; reason && report(node, reason); }, Field(node) { const reason = node.typeInfo().fieldDef?.deprecationReason; reason && report(node, reason); }, Argument(node) { const reason = node.typeInfo().argument?.deprecationReason; reason && report(node, reason); }, ObjectValue(node) { const { inputType } = node.typeInfo(); if (inputType && "getFields" in inputType) { const fields = inputType.getFields(); for (const field of node.fields) { const fieldName = field.name.value, reason = fields[fieldName].deprecationReason; reason && report(field, reason); } } } }; } }; export { rule };