UNPKG

@graphql-eslint/eslint-plugin

Version:
67 lines (61 loc) 2.33 kB
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }var _utilsjs = require('../../utils.js'); const RULE_ID = "require-type-pattern-with-oneof"; const rule = { meta: { type: "suggestion", docs: { category: "Schema", description: "Enforce types with `@oneOf` directive have `error` and `ok` fields.", url: `https://the-guild.dev/graphql/eslint/rules/${RULE_ID}`, examples: [ { title: "Correct", code: ( /* GraphQL */ ` type Mutation { doSomething: DoSomethingMutationResult! } interface Error { message: String! } type DoSomethingMutationResult @oneOf { ok: DoSomethingSuccess error: Error } type DoSomethingSuccess { # ... } ` ) } ] }, messages: { [RULE_ID]: '{{ nodeName }} is defined as output with "@oneOf" and must be defined with "{{ fieldName }}" field' }, schema: [] }, create(context) { return { "Directive[name.value=oneOf][parent.kind=ObjectTypeDefinition]"({ parent }) { const requiredFields = ["error", "ok"]; for (const fieldName of requiredFields) { if (!_optionalChain([parent, 'access', _ => _.fields, 'optionalAccess', _2 => _2.some, 'call', _3 => _3((field) => field.name.value === fieldName)])) { context.report({ node: parent.name, messageId: RULE_ID, data: { nodeName: _utilsjs.displayNodeName.call(void 0, parent), fieldName } }); } } } }; } }; exports.rule = rule;