-
Notifications
You must be signed in to change notification settings - Fork 470
/
check.ts
242 lines (220 loc) · 6.74 KB
/
check.ts
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
import { flags } from "@oclif/command";
import { print } from "graphql";
import { gitInfo } from "../../git";
import { ClientCommand } from "../../Command";
import { CompactRenderer } from "../../utils";
import URI from "vscode-uri";
import { relative } from "path";
import { graphqlTypes } from "apollo-language-server";
import chalk from "chalk";
import envCi from "env-ci";
import { graphUndefinedError } from "../../utils/sharedMessages";
const { ValidationErrorType } = graphqlTypes;
type ValidationResult =
graphqlTypes.ValidateOperations_service_validateOperations_validationResults;
interface Operation {
body: string;
name: string;
relativePath: string;
locationOffset: LocationOffset;
}
interface LocationOffset {
column: number;
line: number;
}
export default class ClientCheck extends ClientCommand {
static description = "Check a client project against a pushed service";
static flags = {
...ClientCommand.flags,
};
async run() {
const { isCi } = envCi();
const { validationResults, operations } = await this.runTasks<{
operations: Operation[];
validationResults: ValidationResult[];
}>(
({ project, config }) => [
{
title: "Checking client compatibility with service",
task: async (ctx) => {
if (!config.graph) {
throw graphUndefinedError;
}
ctx.gitContext = await gitInfo(this.log);
ctx.operations = Object.entries(
this.project.mergedOperationsAndFragmentsForService
).map(([name, doc]) => ({
body: print(doc),
name,
relativePath: relative(
config.configURI ? config.configURI.fsPath : "",
URI.parse(doc.definitions[0].loc!.source.name).fsPath
),
locationOffset: doc.definitions[0].loc!.source.locationOffset,
}));
ctx.validationResults = await project.engine.validateOperations({
id: config.graph,
tag: config.variant,
operations: ctx.operations.map(({ body, name }) => ({
body,
name,
})),
gitContext: ctx.gitContext,
});
},
},
],
() => ({
renderer: isCi ? CompactRenderer : "default",
})
);
// Group the validation results by operation name
const messagesByOperationName = this.getMessagesByOperationName(
validationResults,
operations
);
// For each operation, log its loc and iterate over all associated validationResults
this.log();
Object.values(messagesByOperationName).forEach(
this.logMessagesForOperation
);
if (validationResults.length === 0) {
return this.log(
chalk.green("\nAll operations are valid against service\n")
);
}
this.printStats(validationResults, operations);
// exit with failing status if there are any failures or invalid documents
const hasFailures = validationResults.some(
({ type }: ValidationResult) =>
type === ValidationErrorType.FAILURE ||
type === ValidationErrorType.INVALID
);
if (hasFailures) {
this.exit();
}
return;
}
getMessagesByOperationName(
validationResults: ValidationResult[],
operations: Operation[]
) {
return validationResults.reduce<{
[operationName: string]: {
operation: Operation;
validationResults: ValidationResult[];
};
}>((byOperation, validationResult) => {
// Match a validation result to a known operation (by name)
const matchingOperation = operations.find(
({ name }) => name === validationResult.operation.name
);
// If we find a match, add the validationResult to its respective array
if (matchingOperation) {
byOperation[matchingOperation.name] = {
operation: matchingOperation,
validationResults: byOperation[matchingOperation.name]
? [
...byOperation[matchingOperation.name].validationResults,
validationResult,
]
: [validationResult],
};
}
return byOperation;
}, {});
}
logMessagesForOperation = ({
validationResults,
operation,
}: {
validationResults: ValidationResult[];
operation: Operation;
}) => {
const { name, locationOffset, relativePath } = operation;
this.log(
`${name}: ${chalk.cyan(`${relativePath}:${locationOffset.line}`)}\n`
);
const byErrorType = validationResults.reduce(
(byError, validation) => {
validation;
byError[validation.type].push(validation);
return byError;
},
{
[ValidationErrorType.INVALID]: [],
[ValidationErrorType.FAILURE]: [],
[ValidationErrorType.WARNING]: [],
// XXX TS doesn't recognize ValidationErrorType as a type unless prefixed with graphqlTypes
} as { [key in graphqlTypes.ValidationErrorType]: ValidationResult[] }
);
Object.values(byErrorType).map((validations) => {
if (validations.length > 0) {
validations.forEach((validation) => {
this.log(this.formatValidation(validation));
});
this.log();
}
});
};
formatValidation({ type, description }: ValidationResult) {
let color = (x: string) => x;
switch (type) {
case ValidationErrorType.FAILURE:
color = chalk.red;
break;
case ValidationErrorType.INVALID:
color = chalk.gray;
break;
case ValidationErrorType.WARNING:
color = chalk.yellow;
break;
}
return ` ${color(type)} ${description}`;
}
printStats = (
validationResults: ValidationResult[],
operations: Operation[]
) => {
const counts = validationResults.reduce(
(counts, { type }) => {
switch (type) {
case ValidationErrorType.INVALID:
counts.invalid++;
break;
case ValidationErrorType.FAILURE:
counts.failure++;
break;
case ValidationErrorType.WARNING:
counts.warning++;
}
return counts;
},
{
invalid: 0,
failure: 0,
warning: 0,
}
);
this.log(`${operations.length} total operations validated`);
if (counts.invalid > 0) {
this.log(
chalk.cyan(
`${counts.invalid} invalid document${counts.invalid > 1 ? "s" : ""}`
)
);
}
if (counts.failure > 0) {
this.log(
chalk.red(`${counts.failure} failure${counts.failure > 1 ? "s" : ""}`)
);
}
if (counts.warning > 0) {
this.log(
chalk.yellow(
`${counts.warning} warning${counts.warning > 1 ? "s" : ""}`
)
);
}
};
}