-
Notifications
You must be signed in to change notification settings - Fork 242
/
do_compose.js
56 lines (49 loc) 路 1.59 KB
/
do_compose.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
/** @typedef {{typeDefs: string, name: string, url?: string;}} ServiceDefinition */
/**
* This `composition` is defined as a global by the runtime we define in Rust.
* We declare this as a `var` here only to allow the TSDoc type annotation to be
* applied to it. Running `var` multiple times has no effect.
* @type {{
* composeAndValidate: import('../../federation-js').composeAndValidate,
* parseGraphqlDocument: import('graphql').parse
* }} */
var composition;
/**
* @type {ServiceDefinition[]}
*/
var serviceList = serviceList;
if (!serviceList || !Array.isArray(serviceList)) {
throw new Error("Error in JS-Rust-land: serviceList missing or incorrect.");
}
serviceList.some((service) => {
if (
typeof service.name !== "string" || !service.name ||
typeof service.url !== "string" && service.url ||
typeof service.typeDefs !== "string" && service.typeDefs
) {
throw new Error("Missing required data structure on service.");
}
});
serviceList = serviceList.map(({ typeDefs, ...rest }) => ({
typeDefs: parseTypedefs(typeDefs),
...rest,
}));
function parseTypedefs(source) {
try {
return composition.parseGraphqlDocument(source)
} catch (err) {
// Return the error in a way that we know how to handle it.
done({ Err: [err] });
}
}
try {
/**
* @type {{ errors: Error[], supergraphSdl?: undefined } | { errors?: undefined, supergraphSdl: string; }}
*/
const composed = composition.composeAndValidate(serviceList);
done(
composed.errors ? { Err: composed.errors } : { Ok: composed.supergraphSdl },
);
} catch (err) {
done({ Err: [err] });
}