UNPKG

@graphql-eslint/eslint-plugin

Version:
137 lines (130 loc) 4.49 kB
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }var _utilsjs = require('../../utils.js'); const RULE_ID = "no-deprecated", rule = exports.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) { _utilsjs.requireGraphQLSchema.call(void 0, RULE_ID, context); function report(node, reason) { const nodeType = _utilsjs.displayNodeName.call(void 0, 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 = _optionalChain([node, 'access', _ => _.typeInfo, 'call', _2 => _2(), 'access', _3 => _3.enumValue, 'optionalAccess', _4 => _4.deprecationReason]); reason && report(node, reason); }, Field(node) { const reason = _optionalChain([node, 'access', _5 => _5.typeInfo, 'call', _6 => _6(), 'access', _7 => _7.fieldDef, 'optionalAccess', _8 => _8.deprecationReason]); reason && report(node, reason); }, Argument(node) { const reason = _optionalChain([node, 'access', _9 => _9.typeInfo, 'call', _10 => _10(), 'access', _11 => _11.argument, 'optionalAccess', _12 => _12.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); } } } }; } }; exports.rule = rule;