-
Notifications
You must be signed in to change notification settings - Fork 470
/
list.ts
177 lines (164 loc) · 5.57 KB
/
list.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
import { flags } from "@oclif/command";
import { ProjectCommand } from "../../Command";
import sortBy from "lodash.sortby";
import { table } from "table";
import moment from "moment";
import { ApolloConfig, DefaultEngineConfig } from "apollo-language-server";
import chalk from "chalk";
import {
ListServices_service_implementingServices,
ListServices_service_implementingServices_FederatedImplementingServices_services,
} from "apollo-language-server/lib/graphqlTypes";
import { graphUndefinedError } from "../../utils/sharedMessages";
interface TasksOutput {
config: ApolloConfig;
implementingServices: ListServices_service_implementingServices | null;
frontendUrlRoot: string;
}
const formatImplementingService = (
implementingService: ListServices_service_implementingServices_FederatedImplementingServices_services,
effectiveDate: Date = new Date()
) => {
return {
name: implementingService.name,
url: implementingService.url || "",
updatedAt: `${moment(implementingService.updatedAt).format(
"D MMMM YYYY"
)} (${moment(implementingService.updatedAt).from(effectiveDate)})`,
};
};
function formatHumanReadable({
implementingServices,
graphName,
frontendUrlRoot,
}: {
implementingServices: ListServices_service_implementingServices | null;
graphName: string | undefined;
frontendUrlRoot: string;
}): string {
let result = "";
if (
!implementingServices ||
implementingServices.__typename === "NonFederatedImplementingService"
) {
result =
"\nThis graph is not federated, there are no services composing the graph";
} else if (implementingServices.services.length === 0) {
result = "\nThere are no services on this federated graph";
} else {
// Create a sorted list of the services.
const sortedImplementingServices =
sortBy<ListServices_service_implementingServices_FederatedImplementingServices_services>(
implementingServices.services,
[(service) => service.name.toUpperCase()]
);
console.log(
table([
["Name", "URL", "Last Updated"],
...sortedImplementingServices
.map((sortedImplementingService) =>
formatImplementingService(
sortedImplementingService,
// Force the time to a specific value if we're running tests. Otherwise the snapshots will break
// when the relative time changes.
process.env.NODE_ENV === "test"
? new Date("2019-06-13")
: undefined
)
)
.sort((s1, s2) =>
s1.name.toUpperCase() > s2.name.toUpperCase() ? 1 : -1
)
.map(Object.values)
.filter(Boolean),
])
);
const serviceListUrlEnding = `/graph/${graphName}/service-list`;
const targetUrl = `${frontendUrlRoot}${serviceListUrlEnding}`;
result += `\nView full details at: ${chalk.cyan(targetUrl)}\n`;
}
return result;
}
export default class ServiceList extends ProjectCommand {
static description =
"[DEPRECATED] List the services in a graph" +
ProjectCommand.DEPRECATION_MSG;
static flags = {
...ProjectCommand.flags,
tag: flags.string({
char: "t",
description:
"[Deprecated: please use --variant instead] The tag (AKA variant) to list implementing services for",
hidden: true,
exclusive: ["variant"],
}),
variant: flags.string({
char: "v",
description: "The variant to list implementing services for",
exclusive: ["tag"],
}),
graph: flags.string({
char: "g",
description:
"The ID of the graph in the Apollo registry for which to list implementing services. Overrides config file if set.",
}),
};
async run() {
this.printDeprecationWarning();
// @ts-ignore we're going to populate `taskOutput` later
const taskOutput: TasksOutput = {};
let graphID: string | undefined;
let graphVariant: string | undefined;
try {
await this.runTasks<TasksOutput>(({ config, flags, project }) => {
/**
* Name of the graph we are listing implementing services of. `engine` is an example of a graph.
*
* A graph can be either a monolithic schema or the result of composition a federated schema.
* This command only supports graphs that are federated into multiple implementing services.
*
*/
graphID = config.graph;
graphVariant = config.variant;
if (!graphID) {
throw graphUndefinedError;
}
return [
{
title: `Fetching list of services for graph ${chalk.cyan(
graphID + "@" + graphVariant
)}`,
task: async (ctx: TasksOutput, task) => {
const { frontendUrlRoot, service } =
await project.engine.listServices({
id: graphID!,
graphVariant: graphVariant!,
});
const { implementingServices } = service!;
const newContext: typeof ctx = {
implementingServices,
frontendUrlRoot,
config,
};
Object.assign(ctx, newContext);
Object.assign(taskOutput, ctx);
},
},
];
});
} catch (error) {
if (error.message.includes("/upgrade")) {
this.exit(1);
return;
}
throw error;
}
this.log(
formatHumanReadable({
implementingServices: taskOutput.implementingServices,
graphName: taskOutput.config.graph,
frontendUrlRoot: taskOutput.frontendUrlRoot,
})
);
}
}