@graphql-eslint/eslint-plugin
Version:
GraphQL plugin for ESLint
122 lines (117 loc) • 3.76 kB
JavaScript
import { Kind } from 'graphql';
import { requireGraphQLSchemaFromContext } from '../utils.js';
const RULE_ID = 'no-deprecated';
export 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 */ `
type User {
id: ID!
name: String! @deprecated(reason: "old field, please use fullName instead")
fullName: String!
}
query user {
user {
name
}
}
`,
},
{
title: 'Incorrect (enum value)',
code: /* GraphQL */ `
type Mutation {
changeSomething(type: SomeType): Boolean!
}
enum SomeType {
NEW
OLD @deprecated(reason: "old field, please use NEW instead")
}
mutation {
changeSomething(
type: OLD
) {
...
}
}
`,
},
{
title: 'Correct',
code: /* GraphQL */ `
type User {
id: ID!
name: String! @deprecated(reason: "old field, please use fullName instead")
fullName: String!
}
query user {
user {
id
fullName
}
}
`,
},
],
recommended: true,
},
messages: {
[]: 'This {{ type }} is marked as deprecated in your GraphQL schema (reason: {{ reason }})',
},
schema: [],
},
create(context) {
requireGraphQLSchemaFromContext(RULE_ID, context);
function report(node, reason) {
const nodeName = node.kind === Kind.ENUM ? node.value : node.name.value;
const nodeType = node.kind === 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) {
var _a;
const typeInfo = node.typeInfo();
const reason = (_a = typeInfo.enumValue) === null || _a === void 0 ? void 0 : _a.deprecationReason;
if (reason) {
report(node, reason);
}
},
Field(node) {
var _a;
const typeInfo = node.typeInfo();
const reason = (_a = typeInfo.fieldDef) === null || _a === void 0 ? void 0 : _a.deprecationReason;
if (reason) {
report(node, reason);
}
},
};
},
};