@graphql-eslint/eslint-plugin
Version:
GraphQL plugin for ESLint
158 lines (153 loc) • 4.43 kB
JavaScript
;
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: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__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: true }), mod);
var no_deprecated_exports = {};
__export(no_deprecated_exports, {
rule: () => rule
});
module.exports = __toCommonJS(no_deprecated_exports);
var import_graphql = require("graphql");
var import_utils = 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]: "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;
const 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) {
var _a;
const typeInfo = node.typeInfo();
const reason = (_a = typeInfo.enumValue) == null ? void 0 : _a.deprecationReason;
if (reason) {
report(node, reason);
}
},
Field(node) {
var _a;
const typeInfo = node.typeInfo();
const reason = (_a = typeInfo.fieldDef) == null ? void 0 : _a.deprecationReason;
if (reason) {
report(node, reason);
}
}
};
}
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
rule
});