@graphql-eslint/eslint-plugin
Version:
GraphQL plugin for ESLint
69 lines (66 loc) • 1.74 kB
JavaScript
Object.defineProperty(exports, "__esModule", {value: true});var _graphql = require('graphql');
var _utilsjs = require('../../utils.js');
const RULE_ID = "require-nullable-fields-with-oneof";
const rule = {
meta: {
type: "suggestion",
docs: {
category: "Schema",
description: "Require `input` or `type` fields to be non-nullable with `@oneOf` directive.",
url: `https://the-guild.dev/graphql/eslint/rules/${RULE_ID}`,
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]: '{{ nodeName }} must be nullable when "@oneOf" is in use'
},
schema: []
},
create(context) {
return {
"Directive[name.value=oneOf]"({ parent }) {
const isTypeOrInput = [
_graphql.Kind.OBJECT_TYPE_DEFINITION,
_graphql.Kind.INPUT_OBJECT_TYPE_DEFINITION
].includes(parent.kind);
if (!isTypeOrInput) {
return;
}
for (const field of parent.fields || []) {
if (field.gqlType.kind === _graphql.Kind.NON_NULL_TYPE) {
context.report({
node: field.name,
messageId: RULE_ID,
data: { nodeName: _utilsjs.getNodeName.call(void 0, field) }
});
}
}
}
};
}
};
exports.rule = rule;
;