@graphql-eslint/eslint-plugin
Version:
GraphQL plugin for ESLint
151 lines (144 loc) • 4.61 kB
JavaScript
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";
const rule = {
meta: {
type: "suggestion",
hasSuggestions: true,
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: true,
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: true
},
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 typeInfo = node.typeInfo();
const reason = _optionalChain([typeInfo, 'access', _ => _.enumValue, 'optionalAccess', _2 => _2.deprecationReason]);
if (reason) {
report(node, reason);
}
},
Field(node) {
const typeInfo = node.typeInfo();
const reason = _optionalChain([typeInfo, 'access', _3 => _3.fieldDef, 'optionalAccess', _4 => _4.deprecationReason]);
if (reason) {
report(node, reason);
}
},
Argument(node) {
const typeInfo = node.typeInfo();
const reason = _optionalChain([typeInfo, 'access', _5 => _5.argument, 'optionalAccess', _6 => _6.deprecationReason]);
if (reason) {
report(node, reason);
}
},
ObjectValue(node) {
const { inputType } = node.typeInfo();
if (!inputType) return;
if ("getFields" in inputType) {
const fields = inputType.getFields();
for (const field of node.fields) {
const fieldName = field.name.value;
const reason = fields[fieldName].deprecationReason;
if (reason) {
report(field, reason);
}
}
}
}
};
}
};
exports.rule = rule;
;