-
Notifications
You must be signed in to change notification settings - Fork 4
/
compose.ts
74 lines (60 loc) · 2.24 KB
/
compose.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
import { existsSync, readdirSync, readFileSync, writeFileSync } from 'node:fs';
import { basename, join } from 'node:path';
import { DocumentNode, parse, print, Source, visit } from 'graphql';
import { composeServices as apolloComposeServices } from '@apollo/composition';
import { getSubgraphs as getDGS } from './__tests__/fixtures/dgs/index.js';
import { getSubgraphs as getHuge } from './__tests__/fixtures/huge-schema/index.js';
import { composeServices as guildComposeServices } from './src/index.js';
const args = process.argv.slice(2);
const isApollo = args.includes('apollo');
const composeServices = isApollo ? apolloComposeServices : guildComposeServices;
function fromDirectory(directoryName: string) {
const filepaths = readdirSync(directoryName);
return filepaths
.filter(f => f.endsWith('.graphql'))
.map(f => {
const originalNameSourceFile = join(directoryName, f.replace('.graphql', '.log'));
let name = basename(f).replace('.graphql', '').replace('_', '-');
if (existsSync(originalNameSourceFile)) {
name = readFileSync(originalNameSourceFile, 'utf-8');
}
const typeDefs = visit(parse(new Source(readFileSync(join(directoryName, f), 'utf-8'), f)), {
enter(node) {
if ('description' in node) {
return {
...node,
description: undefined,
};
}
},
});
writeFileSync(join(directoryName, f), print(typeDefs));
return {
name,
typeDefs,
};
});
}
let services: Array<{
typeDefs: DocumentNode;
name: string;
}> = [];
// services = await getDGS();
// services = await getHuge();
services = fromDirectory('./temp');
if (typeof gc === 'function') {
gc();
}
debugger;
console.time('Total');
console.log('Composing', services.length, 'services');
const result = composeServices(services);
console.timeEnd('Total');
debugger;
const memoryAfter = process.memoryUsage().heapUsed;
console.log('Memory:', memoryAfter / 1024 / 1024, 'MB');
const hasErrors = 'errors' in result && result.errors && result.errors.length;
console.log(hasErrors ? '❌ Failed' : '✅ Succeeded');
if (hasErrors) {
console.log(result.errors.map(e => (e.extensions.code ?? '') + ' ' + e.message).join('\n\n'));
}