UNPKG

@graphql-eslint/eslint-plugin

Version:
104 lines (103 loc) 3.21 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: !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 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: !1, required: ["disallow"], properties: { disallow: { ...import_utils.ARRAY_DEFAULT_OPTIONS, items: { enum: ["mutation", "subscription"] } } } } }, rule = { meta: { type: "suggestion", hasSuggestions: !0, docs: { category: "Schema", description: "Disallow using root types `mutation` and/or `subscription`.", url: "https://the-guild.dev/graphql/eslint/rules/no-root-type", requiresSchema: !0, 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!]! } ` ) } ], configOptions: [{ disallow: ["mutation", "subscription"] }] }, schema }, create(context) { const schema2 = (0, import_utils.requireGraphQLSchemaFromContext)("no-root-type", context), disallow = new Set(context.options[0].disallow), rootTypeNames = [ disallow.has("mutation") && schema2.getMutationType(), disallow.has("subscription") && schema2.getSubscriptionType() ].filter(import_utils.truthy).map((type) => type.name).join("|"); return rootTypeNames ? { [`:matches(ObjectTypeDefinition, ObjectTypeExtension) > .name[value=/^(${rootTypeNames})$/]`](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 });