UNPKG

@graphql-eslint/eslint-plugin

Version:
144 lines (139 loc) 4.17 kB
"use strict"; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: !0 }); }, __copyProps = (to, from, except, desc) => { if (from && typeof from == "object" || typeof from == "function") for (let key of __getOwnPropNames(from)) !__hasOwnProp.call(to, key) && key !== except && __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: !0 }), mod); var no_deprecated_exports = {}; __export(no_deprecated_exports, { rule: () => rule }); module.exports = __toCommonJS(no_deprecated_exports); var import_graphql = require("graphql"), import_utils = require("../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]: "This {{ type }} is marked as deprecated in your GraphQL schema (reason: {{ reason }})" }, schema: [] }, create(context) { (0, import_utils.requireGraphQLSchemaFromContext)(RULE_ID, context); function report(node, reason) { const nodeName = node.kind === import_graphql.Kind.ENUM ? node.value : node.name.value, nodeType = node.kind === import_graphql.Kind.ENUM ? "enum value" : "field"; context.report({ node, messageId: RULE_ID, data: { type: nodeType, reason }, suggest: [ { desc: `Remove \`${nodeName}\` ${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); } }; } }; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { rule });