UNPKG

@graphql-eslint/eslint-plugin

Version:
77 lines (74 loc) 2.11 kB
"use strict";Object.defineProperty(exports, "__esModule", {value: true});var _graphql = require('graphql'); var _utilsjs = 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 = _utilsjs.requireGraphQLSchema.call(void 0, 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 (_graphql.isScalarType.call(void 0, graphQLType)) { let fieldDef = node.parent; while (fieldDef.kind !== _graphql.Kind.FIELD_DEFINITION) { fieldDef = fieldDef.parent; } context.report({ node, message: `Unexpected scalar result type \`${typeName}\` for ${_utilsjs.getNodeName.call(void 0, fieldDef)}`, suggest: [ { desc: `Remove \`${typeName}\``, fix: (fixer) => fixer.remove(node) } ] }); } } }; } }; exports.rule = rule;