/
fetch-schema.js
executable file
·52 lines (43 loc) · 1.63 KB
/
fetch-schema.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
#!/usr/bin/env node
// @flow
// TODO: DRY monorepo-utils/bin/monorepo-babel-node-runner.js and relay-compiler
// This is here to make this `bin` available directly from our monorepo without transpiling it.
require('@babel/register')({
ignore: [/node_modules\/(?!@adeira)/],
rootMode: 'upward-optional',
});
const fs = require('fs');
const path = require('path');
const program = require('commander');
const { invariant } = require('@adeira/js');
const fetch = require('@adeira/fetch').default;
const logger = require('@adeira/logger').default;
const SignedSource = require('@adeira/signed-source').default;
const { lexicographicSortSchema } = require('graphql');
const { buildClientSchema, getIntrospectionQuery, printSchema } = require('graphql/utilities');
program
.option('--resource <url>')
.option('--filename <path>', undefined, 'schema.graphql')
.parse(process.argv);
invariant(program.resource, 'Option --resource is required.');
const filename = path.join(process.cwd(), program.filename);
(async () => {
const response = await fetch(program.resource, {
method: 'POST',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
'X-Client': '@adeira/relay schema fetcher',
},
body: JSON.stringify({
query: getIntrospectionQuery(),
}),
});
const schemaJSON = await response.json();
const clientSchema = printSchema(lexicographicSortSchema(buildClientSchema(schemaJSON.data)));
const schema = SignedSource.signFile(`# ${SignedSource.getSigningToken()}
${clientSchema}
`);
fs.writeFileSync(filename, schema);
logger.log('GraphQL schema saved to: %s', filename);
})();