@graphql-eslint/eslint-plugin
Version:
GraphQL plugin for ESLint
125 lines (120 loc) • 3.88 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.rule = void 0;
const graphql_1 = require("graphql");
const utils_js_1 = require("../utils.js");
const RULE_ID = 'no-deprecated';
exports.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]: 'This {{ type }} is marked as deprecated in your GraphQL schema (reason: {{ reason }})',
},
schema: [],
},
create(context) {
(0, utils_js_1.requireGraphQLSchemaFromContext)(RULE_ID, context);
function report(node, reason) {
const nodeName = node.kind === graphql_1.Kind.ENUM ? node.value : node.name.value;
const nodeType = node.kind === graphql_1.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);
}
},
};
},
};