UNPKG

@graphql-eslint/eslint-plugin

Version:
103 lines (98 loc) 3.42 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 require_field_of_type_query_in_mutation_result_exports = {}; __export(require_field_of_type_query_in_mutation_result_exports, { rule: () => rule }); module.exports = __toCommonJS(require_field_of_type_query_in_mutation_result_exports); var import_graphql = require("graphql"); var import_utils = require("../utils.js"); const RULE_ID = "require-field-of-type-query-in-mutation-result"; const rule = { meta: { type: "suggestion", docs: { category: "Schema", description: "Allow the client in one round-trip not only to call mutation but also to get a wagon of data to update their application.\n> Currently, no errors are reported for result type `union`, `interface` and `scalar`.", url: `https://the-guild.dev/graphql/eslint/rules/${RULE_ID}`, requiresSchema: true, examples: [ { title: "Incorrect", code: ( /* GraphQL */ ` type User { ... } type Mutation { createUser: User! } ` ) }, { title: "Correct", code: ( /* GraphQL */ ` type User { ... } type Query { ... } type CreateUserPayload { user: User! query: Query! } type Mutation { createUser: CreateUserPayload! } ` ) } ] }, schema: [] }, create(context) { const schema = (0, import_utils.requireGraphQLSchemaFromContext)(RULE_ID, context); const mutationType = schema.getMutationType(); const queryType = schema.getQueryType(); if (!mutationType || !queryType) { return {}; } const selector = `:matches(ObjectTypeDefinition, ObjectTypeExtension)[name.value=${mutationType.name}] > FieldDefinition > .gqlType Name`; return { [selector](node) { const typeName = node.value; const graphQLType = schema.getType(typeName); if ((0, import_graphql.isObjectType)(graphQLType)) { const { fields } = graphQLType.astNode; const hasQueryType = fields == null ? void 0 : fields.some((field) => (0, import_utils.getTypeName)(field) === queryType.name); if (!hasQueryType) { context.report({ node, message: `Mutation result type "${graphQLType.name}" must contain field of type "${queryType.name}"` }); } } } }; } }; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { rule });