UNPKG

@graphql-eslint/eslint-plugin

Version:
114 lines (113 loc) 3.29 kB
"use strict"; 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_root_type_exports = {}; __export(no_root_type_exports, { rule: () => rule }); module.exports = __toCommonJS(no_root_type_exports); var import_utils = require("../utils.js"); const schema = { type: "array", minItems: 1, maxItems: 1, items: { type: "object", additionalProperties: false, required: ["disallow"], properties: { disallow: { ...import_utils.ARRAY_DEFAULT_OPTIONS, items: { enum: ["mutation", "subscription"] } } } } }; const rule = { meta: { type: "suggestion", hasSuggestions: true, docs: { category: "Schema", description: "Disallow using root types `mutation` and/or `subscription`.", url: "https://the-guild.dev/graphql/eslint/rules/no-root-type", requiresSchema: true, isDisabledForAllConfig: true, examples: [ { title: "Incorrect", usage: [{ disallow: ["mutation", "subscription"] }], code: ( /* GraphQL */ ` type Mutation { createUser(input: CreateUserInput!): User! } ` ) }, { title: "Correct", usage: [{ disallow: ["mutation", "subscription"] }], code: ( /* GraphQL */ ` type Query { users: [User!]! } ` ) } ] }, schema }, create(context) { const schema2 = (0, import_utils.requireGraphQLSchemaFromContext)("no-root-type", context); const disallow = new Set(context.options[0].disallow); const rootTypeNames = [ disallow.has("mutation") && schema2.getMutationType(), disallow.has("subscription") && schema2.getSubscriptionType() ].filter(import_utils.truthy).map((type) => type.name).join("|"); if (!rootTypeNames) { return {}; } const selector = `:matches(ObjectTypeDefinition, ObjectTypeExtension) > .name[value=/^(${rootTypeNames})$/]`; return { [selector](node) { const typeName = node.value; context.report({ node, message: `Root type \`${typeName}\` is forbidden.`, suggest: [ { desc: `Remove \`${typeName}\` type`, fix: (fixer) => fixer.remove(node.parent) } ] }); } }; } }; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { rule });