forked from apollographql/apollo-tooling
-
Notifications
You must be signed in to change notification settings - Fork 0
/
check.ts
178 lines (162 loc) · 5.14 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
import { Command, flags } from "@oclif/command";
import chalk from "chalk";
import { table, styledJSON } from "heroku-cli-util";
import * as Listr from "listr";
import {
GraphQLError,
parse,
introspectionQuery,
execute as graphql
} from "graphql";
import { toPromise, execute } from "apollo-link";
import { VALIDATE_SCHEMA } from "../../operations/validateSchema";
import { engineLink, getIdFromKey } from "../../engine";
import { fetchSchema } from "../../fetch-schema";
import { gitInfo } from "../../git";
import { ChangeType } from "../../printer/ast";
import { engineFlags } from "../../engine-cli";
import { loadConfigStep } from "../../load-config";
// how its brought down from schema
interface Change {
type: ChangeType;
code: string;
description: string;
}
export default class SchemaCheck extends Command {
static description =
"Check a schema against the version registered in Apollo Engine.";
static flags = {
help: flags.help({
char: "h",
description: "Show command help"
}),
config: flags.string({
description: "Path to your Apollo config file"
}),
...engineFlags,
header: flags.string({
multiple: true,
parse: header => {
const [key, value] = header.split(":");
return JSON.stringify({ [key.trim()]: value.trim() });
},
description: "Additional headers to send to server for introspectionQuery"
}),
endpoint: flags.string({
description: "The URL of the server to fetch the schema from"
}),
json: flags.boolean({
description: "Output result as JSON"
})
};
async run() {
const { flags } = this.parse(SchemaCheck);
const tasks = new Listr([
loadConfigStep(flags, true),
{
title: "Fetching local schema",
task: async ctx => {
if (Object.values(ctx.config.schemas).length > 1) {
this.error("More than one schema found.");
}
if (Object.values(ctx.config.schemas).length == 0) {
this.error("No schemas found.");
}
ctx.currentSchema = Object.values(ctx.config.schemas)[0];
if (!ctx.currentSchema.engineKey) {
this.error(
"No API key was specified. Set an Apollo Engine API key using the `--key` flag or the `ENGINE_API_KEY` environment variable."
);
}
ctx.schema = await fetchSchema(ctx.currentSchema.endpoint);
}
},
{
title: "Checking schema for changes",
task: async ctx => {
const gitContext = await gitInfo();
const variables = {
id: getIdFromKey(ctx.currentSchema.engineKey),
schema: (await graphql(ctx.schema, parse(introspectionQuery))).data!
.__schema,
// XXX hardcoded for now
tag: "current",
gitContext
};
ctx.changes = await toPromise(
execute(engineLink, {
query: VALIDATE_SCHEMA,
variables,
context: {
headers: { ["x-api-key"]: ctx.currentSchema.engineKey },
...(ctx.config.engineEndpoint && {
uri: ctx.config.engineEndpoint
})
}
})
)
.then(({ data, errors }) => {
// XXX better end user error message
if (errors)
throw new Error(
errors.map(({ message }) => message).join("\n")
);
if (!data!.service)
throw new Error(`No schema found for ${variables.id}`);
return data!.service.schema.checkSchema.changes;
})
.catch(e => {
if (e.result && e.result.errors) {
this.error(
e.result.errors
.map(({ message }: GraphQLError) => message)
.join("\n")
);
} else {
this.error(e.message);
}
});
}
}
]);
return tasks.run().then(async ({ changes }) => {
const failures = changes.filter(
({ type }: Change) => type === ChangeType.FAILURE
);
const exit = failures.length > 0 ? 1 : 0;
if (flags.json) {
await styledJSON({ changes });
// exit with failing status if we have failures
this.exit(exit);
}
if (changes.length === 0) {
return this.log("\nNo changes present between schemas\n");
}
this.log("\n");
table(changes.map(format), {
columns: [
{ key: "type", label: "Change" },
{ key: "code", label: "Code" },
{ key: "description", label: "Description" }
]
});
this.log("\n");
// exit with failing status if we have failures
this.exit(exit);
});
}
}
export const format = (change: Change) => {
let color = (x: string): string => x;
if (change.type === ChangeType.FAILURE) {
color = chalk.red;
}
if (change.type === ChangeType.WARNING) {
color = chalk.yellow;
}
return {
type: color(change.type),
code: color(change.code),
description: color(change.description)
};
};