@graphql-eslint/eslint-plugin
Version:
GraphQL plugin for ESLint
169 lines (162 loc) • 5.64 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: !0 });
}, __copyProps = (to, from, except, desc) => {
if (from && typeof from == "object" || typeof from == "function")
for (let key of __getOwnPropNames(from))
!__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: !0 }), mod);
var strict_id_in_types_exports = {};
__export(strict_id_in_types_exports, {
rule: () => rule
});
module.exports = __toCommonJS(strict_id_in_types_exports);
var import_graphql = require("graphql"), import_utils = require("../utils.js");
const RULE_ID = "strict-id-in-types", schema = {
type: "array",
maxItems: 1,
items: {
type: "object",
additionalProperties: !1,
properties: {
acceptedIdNames: {
...import_utils.ARRAY_DEFAULT_OPTIONS,
default: ["id"]
},
acceptedIdTypes: {
...import_utils.ARRAY_DEFAULT_OPTIONS,
default: ["ID"]
},
exceptions: {
type: "object",
additionalProperties: !1,
properties: {
types: {
...import_utils.ARRAY_DEFAULT_OPTIONS,
description: "This is used to exclude types with names that match one of the specified values."
},
suffixes: {
...import_utils.ARRAY_DEFAULT_OPTIONS,
description: "This is used to exclude types with names with suffixes that match one of the specified values."
}
}
}
}
}
}, rule = {
meta: {
type: "suggestion",
docs: {
description: "Requires output types to have one unique identifier unless they do not have a logical one. Exceptions can be used to ignore output types that do not have unique identifiers.",
category: "Schema",
recommended: !0,
url: `https://the-guild.dev/graphql/eslint/rules/${RULE_ID}`,
requiresSchema: !0,
examples: [
{
title: "Incorrect",
usage: [
{
acceptedIdNames: ["id", "_id"],
acceptedIdTypes: ["ID"],
exceptions: { suffixes: ["Payload"] }
}
],
code: (
/* GraphQL */
`
# Incorrect field name
type InvalidFieldName {
key: ID!
}
# Incorrect field type
type InvalidFieldType {
id: String!
}
# Incorrect exception suffix
type InvalidSuffixResult {
data: String!
}
# Too many unique identifiers. Must only contain one.
type InvalidFieldName {
id: ID!
_id: ID!
}
`
)
},
{
title: "Correct",
usage: [
{
acceptedIdNames: ["id", "_id"],
acceptedIdTypes: ["ID"],
exceptions: { types: ["Error"], suffixes: ["Payload"] }
}
],
code: (
/* GraphQL */
`
type User {
id: ID!
}
type Post {
_id: ID!
}
type CreateUserPayload {
data: String!
}
type Error {
message: String!
}
`
)
}
]
},
schema
},
create(context) {
const options = {
acceptedIdNames: ["id"],
acceptedIdTypes: ["ID"],
exceptions: {},
...context.options[0]
}, schema2 = (0, import_utils.requireGraphQLSchemaFromContext)(RULE_ID, context);
return {
[`ObjectTypeDefinition[name.value!=/^(${[
schema2.getQueryType(),
schema2.getMutationType(),
schema2.getSubscriptionType()
].filter(import_utils.truthy).map((type) => type.name).join("|")})$/]`](node) {
const typeName = node.name.value;
if (options.exceptions.types?.includes(typeName) || options.exceptions.suffixes?.some((suffix) => typeName.endsWith(suffix)))
return;
if (node.fields?.filter((field) => {
const fieldNode = field.rawNode(), isValidIdName = options.acceptedIdNames.includes(fieldNode.name.value);
let isValidIdType = !1;
return fieldNode.type.kind === import_graphql.Kind.NON_NULL_TYPE && fieldNode.type.type.kind === import_graphql.Kind.NAMED_TYPE && (isValidIdType = options.acceptedIdTypes.includes(fieldNode.type.type.name.value)), isValidIdName && isValidIdType;
})?.length !== 1) {
const pluralNamesSuffix = options.acceptedIdNames.length > 1 ? "s" : "", pluralTypesSuffix = options.acceptedIdTypes.length > 1 ? "s" : "";
context.report({
node: node.name,
message: `${(0, import_utils.displayNodeName)(node)} must have exactly one non-nullable unique identifier.
Accepted name${pluralNamesSuffix}: ${(0, import_utils.englishJoinWords)(options.acceptedIdNames)}.
Accepted type${pluralTypesSuffix}: ${(0, import_utils.englishJoinWords)(options.acceptedIdTypes)}.`
});
}
}
};
}
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
rule
});