-
Notifications
You must be signed in to change notification settings - Fork 470
/
push.ts
218 lines (205 loc) · 7.4 KB
/
push.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
import { ClientCommand } from "../../Command";
import { table } from "table";
import { relative } from "path";
import URI from "vscode-uri";
import { getOperationManifestFromProject } from "../../utils/getOperationManifestFromProject";
import {
operationHash,
defaultOperationRegistrySignature,
} from "apollo-graphql";
import { pluralize } from "../../utils";
import chalk from "chalk";
import {
GraphQLClientProject,
ApolloConfig,
graphqlTypes,
} from "apollo-language-server";
import { graphUndefinedError } from "../../utils/sharedMessages";
export default class ClientPush extends ClientCommand {
static description =
"Register operations with Apollo, adding them to the safelist";
static flags = {
...ClientCommand.flags,
};
async run() {
const invalidOperationsErrorMessage = "encountered invalid operations";
let result = "";
try {
await this.runTasks(({ flags, project, config }) => {
const clientBundleInfo = `${chalk.cyan(
(config.client && config.client.name) || flags
)}${chalk.cyan(
(config.client &&
config.client.version &&
`@${config.client.version}`) ||
""
)}`;
return [
{
title: `Extracting operation from client, ${clientBundleInfo}`,
task: async (ctx, task) => {
const operationManifest = getOperationManifestFromProject(
this.project
);
ctx.operationManifest = operationManifest;
task.title = `Extracted ${pluralize(
operationManifest.length,
"operation"
)} from client, ${clientBundleInfo}`;
},
},
{
title: `Checked operations against ${chalk.cyan(
config.graph + "@" + config.variant
)}`,
task: async () => {},
},
{
title: "Pushing operations to operation registry",
task: async (_, task) => {
if (!config.graph) {
throw graphUndefinedError;
}
const operationManifest = getOperationManifestFromProject(
this.project
);
const signatureToOperation = generateSignatureToOperationMap(
this.project,
config
);
const { name, referenceID, version } = config.client!;
if (!name) {
throw new Error("Client name is required to push");
}
const variables = {
clientIdentity: {
name: name,
identifier: referenceID || name,
version,
},
id: config.graph,
operations: operationManifest,
manifestVersion: 2,
graphVariant: config.variant,
};
const { operations: _op, ...restVariables } = variables;
this.debug("Variables sent to Apollo");
this.debug(restVariables);
this.debug("Operations sent to Apollo");
this.debug(operationManifest);
let response: graphqlTypes.RegisterOperations_service_registerOperationsWithResponse;
const { invalidOperations, newOperations, registrationSuccess } =
(response = await project.engine.registerOperations(variables));
this.debug("Results received from Apollo");
this.debug(response);
if (!registrationSuccess) {
if (invalidOperations) {
invalidOperations.forEach((operation) => {
const { operationName, file } =
signatureToOperation[operation.signature];
result += `\nError in: ${chalk.cyan(file)}\n`;
result += table(
[
["Status", "Operation", "Errors"],
[
chalk.red("Error"),
operationName,
(operation.errors
? operation.errors.map(({ message }) => message)
: []
).join("\n"),
],
],
{
columns: {
2: {
width: 50,
wrapWord: true,
},
},
}
);
});
task.title = `Failed to push operations, due to ${pluralize(
invalidOperations.length,
"invalid operation"
)}`;
throw new Error(invalidOperationsErrorMessage);
} else {
task.title = `Failed to register operations`;
throw new Error(
[
"Registration failed and did not receive invalid operations.",
"This should not occur, so please open a GitHub issue on:",
"https://github.com/apollographql/apollo-tooling/",
].join("\n")
);
}
} else {
if (newOperations && newOperations.length) {
task.title = `Successfully pushed ${pluralize(
newOperations.length,
"operation"
)} to the operation registry`;
result += table([
["Status", "Operation Name"],
...newOperations.map((operation) => {
const { operationName, file } =
signatureToOperation[operation.signature];
return [chalk.green("ADDED"), operationName];
}),
]);
} else {
task.title = `All operations were already found in the operation registry`;
}
}
},
},
];
});
} catch (e) {
// Print results when we have an expected error message
if (e.message === invalidOperationsErrorMessage) {
this.log(result);
this.exit(1);
}
throw e;
}
this.log(result);
}
}
function generateSignatureToOperationMap(
project: GraphQLClientProject,
config: ApolloConfig
) {
return Object.fromEntries(
Object.entries(project.mergedOperationsAndFragmentsForService).map(
([operationName, document]) => {
const operationDefinition = document.definitions.find(
({ kind }) => kind === "OperationDefinition"
);
const relativePath =
operationDefinition &&
operationDefinition.loc &&
relative(
config.configURI ? config.configURI.fsPath : "",
URI.parse(operationDefinition.loc.source.name).fsPath
);
const line =
operationDefinition &&
operationDefinition.loc &&
operationDefinition.loc.source.locationOffset.line;
return [
operationHash(
defaultOperationRegistrySignature(document, operationName)
),
{
operationName,
document,
file: line ? `${relativePath}:${line}` : relativePath || "",
},
];
}
)
);
}