UNPKG

@graphql-eslint/eslint-plugin

Version:
101 lines (100 loc) 3.13 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_scalar_result_type_on_mutation_exports = {}; __export(no_scalar_result_type_on_mutation_exports, { rule: () => rule }); module.exports = __toCommonJS(no_scalar_result_type_on_mutation_exports); var import_graphql = require("graphql"); var import_utils = require("../utils.js"); const RULE_ID = "no-scalar-result-type-on-mutation"; const rule = { meta: { type: "suggestion", hasSuggestions: true, docs: { category: "Schema", description: "Avoid scalar result type on mutation type to make sure to return a valid state.", url: `https://the-guild.dev/graphql/eslint/rules/${RULE_ID}`, requiresSchema: true, examples: [ { title: "Incorrect", code: ( /* GraphQL */ ` type Mutation { createUser: Boolean } ` ) }, { title: "Correct", code: ( /* GraphQL */ ` type Mutation { createUser: User! } ` ) } ] }, schema: [] }, create(context) { const schema = (0, import_utils.requireGraphQLSchemaFromContext)(RULE_ID, context); const mutationType = schema.getMutationType(); if (!mutationType) { return {}; } const selector = [ `:matches(ObjectTypeDefinition, ObjectTypeExtension)[name.value=${mutationType.name}]`, "> FieldDefinition > .gqlType Name" ].join(" "); return { [selector](node) { const typeName = node.value; const graphQLType = schema.getType(typeName); if ((0, import_graphql.isScalarType)(graphQLType)) { let fieldDef = node.parent; while (fieldDef.kind !== import_graphql.Kind.FIELD_DEFINITION) { fieldDef = fieldDef.parent; } context.report({ node, message: `Unexpected scalar result type \`${typeName}\` for ${(0, import_utils.getNodeName)(fieldDef)}`, suggest: [ { desc: `Remove \`${typeName}\``, fix: (fixer) => fixer.remove(node) } ] }); } } }; } }; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { rule });