@vanta-inc/eslint-plugin-vanta
Version:
Custom ESLint rules for Vanta
72 lines (71 loc) • 2.92 kB
JavaScript
;
/**
* @fileoverview Ensure that all mutations take a single non-nullable input type with the suffix "Input" or have no arguments
*/
Object.defineProperty(exports, "__esModule", { value: true });
var rule = {
meta: {
type: "suggestion",
docs: {
// @ts-ignore
description: "All mutations must take a single non-nullable input type with the suffix `Input` or have no arguments",
category: "Operations",
url: "https://github.com/VantaInc/eslint-plugin-vanta/blob/main/docs/rules/mutations-input-type.md",
},
},
create: function (context) {
var validatePayload = function (node) {
var _a;
if (node.name.value !== "Mutation") {
return;
}
(_a = node.rawNode().fields) === null || _a === void 0 ? void 0 : _a.forEach(function (field) {
var _a, _b, _c;
if (((_b = (_a = field.arguments) === null || _a === void 0 ? void 0 : _a.length) !== null && _b !== void 0 ? _b : 0) > 1) {
context.report({
node: node,
message: "Mutations must take zero or one argument",
});
return;
}
(_c = field.arguments) === null || _c === void 0 ? void 0 : _c.forEach(function (arg) {
if (arg.name.value !== "input") {
context.report({
node: node,
message: "Mutations must take a single argument named input",
});
return;
}
if (arg.type.kind === "ListType") {
context.report({
node: node,
message: "Mutation input must not be list type",
});
return;
}
if (arg.type.kind !== "NonNullType") {
context.report({
node: node,
message: "Mutation input must be non-nullable",
});
return;
}
if (arg.type.type.kind !== "NamedType" ||
!arg.type.type.name.value.endsWith("Input")) {
context.report({
node: node,
message: "Mutation input must be a type with suffix `Input`",
});
return;
}
});
});
};
return {
ObjectTypeDefinition: validatePayload,
ObjectTypeExtension: validatePayload,
};
},
};
module.exports = rule;
exports.default = rule;