@graphql-eslint/eslint-plugin
Version:
GraphQL plugin for ESLint
93 lines (92 loc) • 2.77 kB
JavaScript
;
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_nullable_fields_with_oneof_exports = {};
__export(require_nullable_fields_with_oneof_exports, {
rule: () => rule
});
module.exports = __toCommonJS(require_nullable_fields_with_oneof_exports);
var import_graphql = require("graphql");
var import_utils = 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 = [
import_graphql.Kind.OBJECT_TYPE_DEFINITION,
import_graphql.Kind.INPUT_OBJECT_TYPE_DEFINITION
].includes(parent.kind);
if (!isTypeOrInput) {
return;
}
for (const field of parent.fields || []) {
if (field.gqlType.kind === import_graphql.Kind.NON_NULL_TYPE) {
context.report({
node: field.name,
messageId: RULE_ID,
data: { nodeName: (0, import_utils.getNodeName)(field) }
});
}
}
}
};
}
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
rule
});