This repository was archived by the owner on Mar 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmutations-return-payload.ts
More file actions
62 lines (57 loc) · 1.72 KB
/
mutations-return-payload.ts
File metadata and controls
62 lines (57 loc) · 1.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/**
* @fileoverview Ensure that all mutations return a nullable type with the suffix "Payload"
*/
import { GraphQLESLintRule } from "@graphql-eslint/eslint-plugin";
import { GraphQLESTreeNode } from "@graphql-eslint/eslint-plugin/estree-converter";
import { ObjectTypeDefinitionNode, ObjectTypeExtensionNode } from "graphql";
const rule: GraphQLESLintRule = {
meta: {
type: "suggestion",
docs: {
// @ts-ignore
description:
"All mutations must return a nullable type with the suffix `Payload`",
category: "Operations",
url: "https://github.com/VantaInc/eslint-plugin-vanta/blob/main/docs/rules/mutations-return-payload.md",
},
},
create(context) {
const validatePayload = (
node:
| GraphQLESTreeNode<ObjectTypeExtensionNode>
| GraphQLESTreeNode<ObjectTypeDefinitionNode>
) => {
if (node.name.value !== "Mutation") {
return;
}
node.rawNode().fields?.forEach((field) => {
if (field.type.kind === "ListType") {
context.report({
node,
message: "Mutation payloads must not be list types",
});
return;
}
if (field.type.kind === "NonNullType") {
context.report({
node,
message: "Mutation payloads must be nullable",
});
return;
}
if (!field.type.name.value.endsWith("Payload")) {
context.report({
node,
message: `Mutations must return types with the suffix "Payload"`,
});
}
});
};
return {
ObjectTypeDefinition: validatePayload,
ObjectTypeExtension: validatePayload,
};
},
};
module.exports = rule;
export default rule;