UNPKG

@graphql-eslint/eslint-plugin

Version:
62 lines (61 loc) 1.95 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.rule = void 0; const graphql_1 = require("graphql"); const RULE_ID = 'require-nullable-fields-with-oneof'; exports.rule = { meta: { type: 'suggestion', docs: { category: 'Schema', description: 'Require `input` or `type` fields to be non-nullable with `@oneOf` directive.', url: `https://github.com/B2o5T/graphql-eslint/blob/master/docs/rules/${RULE_ID}.md`, examples: [ { title: 'Incorrect', code: /* GraphQL */ ` input Input @oneOf { foo: String! b: Int } `, }, { title: 'Correct', code: /* GraphQL */ ` input Input @oneOf { foo: String bar: Int } `, }, ], }, messages: { [RULE_ID]: 'Field `{{fieldName}}` must be nullable.', }, schema: [], }, create(context) { return { 'Directive[name.value=oneOf]'({ parent, }) { const isTypeOrInput = [ graphql_1.Kind.OBJECT_TYPE_DEFINITION, graphql_1.Kind.INPUT_OBJECT_TYPE_DEFINITION, ].includes(parent.kind); if (!isTypeOrInput) { return; } for (const field of parent.fields) { if (field.gqlType.kind === graphql_1.Kind.NON_NULL_TYPE) { context.report({ node: field.name, messageId: RULE_ID, data: { fieldName: field.name.value }, }); } } }, }; }, };