-
Notifications
You must be signed in to change notification settings - Fork 153
/
gql-inspector-checker.js
79 lines (78 loc) · 3.35 KB
/
gql-inspector-checker.js
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
module.exports = (props) => {
const { changes, newSchema, oldSchema } = props;
return changes.map((change) => {
const deprecateNotationRegex = /Deprecated since (\d{2}\.\d{2}.\d{1})/;
const addNotationRegex = /Added in (\d{2}\.\d{2}.\d{1})/;
if (
[
"FIELD_DEPRECATION_REASON_ADDED",
"FIELD_DEPRECATION_REASON_CHANGED",
].includes(change.type) &&
change.criticality.level !== "BREAKING"
) {
const newReason =
change.meta?.addedDeprecationReason ?? change.meta?.newDeprecationReason;
if (newReason && !newReason.match(deprecateNotationRegex)) {
change.criticality.level = "BREAKING";
change.criticality.reason =
'Deprecation reason must include a version number in the format "Deprecated since XX.XX.X."';
change.message =
'Deprecation reason must include a version number in the format "Deprecated since XX.XX.X.", ' +
change.message;
}
} else if (
["FIELD_ADDED", "INPUT_FIELD_ADDED"].includes(change.type) &&
change.criticality.level !== "BREAKING"
) {
const [typeName, fieldName] = change.path.split(".");
const description = newSchema.getTypeMap()[typeName].getFields()[
fieldName
].astNode.description?.value;
if (!description || (description && !description.match(addNotationRegex))) {
change.criticality.level = "BREAKING";
change.criticality.reason =
'New fields must include a description with a version number in the format "Added in XX.XX.X."';
change.message =
'New fields must include a description with a version number in the format "Added in XX.XX.X.", ' +
change.message;
}
} else if (
change.type === "TYPE_ADDED" &&
change.criticality.level !== "BREAKING"
) {
const typeName = change.path.split(".")[0];
const description =
newSchema.getTypeMap()[typeName].astNode.description?.value;
if (!description || (description && !description.match(addNotationRegex))) {
change.criticality.level = "BREAKING";
change.criticality.reason =
'New types must include a description with a version number in the format "Added in XX.XX.X."';
change.message =
'New types must include a description with a version number in the format "Added in XX.XX.X.", ' +
change.message;
}
} else if (
["FIELD_ARGUMENT_ADDED", "FIELD_ARGUMENT_DESCRIPTION_CHANGED"].includes(
change.type
)
) {
const [type, fieldName, argumentName] = change.path.split(".");
const field = newSchema.getTypeMap()[type].getFields()[fieldName];
const description = field.args.find(
(arg) => arg.name === argumentName
)?.description;
if (!description || (description && !description.match(addNotationRegex))) {
change.criticality.level = "BREAKING";
change.criticality.reason =
'New arguments must include a description with a version number in the format "Added in XX.XX.X."';
change.message =
'New arguments must include a description with a version number in the format "Added in XX.XX.X.", ' +
change.message;
} else {
change.criticality.level = "DANGEROUS";
}
}
return change;
});
};
// TODO: update the rule to check for the version number in the description.