-
Notifications
You must be signed in to change notification settings - Fork 9
/
fetchSchema.js
executable file
·61 lines (53 loc) · 1.3 KB
/
fetchSchema.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
57
58
59
60
61
const fetch = require('node-fetch');
const {
getIntrospectionQuery,
buildClientSchema,
printSchema,
} = require('graphql');
const fs = require('fs');
const yargs = require('yargs');
async function runIntrospectionQuery() {
const body = JSON.stringify({query: getIntrospectionQuery()});
const res = await fetch(
'https://serve.onegraph.com/graphql?app_id=0b33e830-7cde-4b90-ad7e-2a39c57c0e11',
{
method: 'POST',
headers: {
'Content-Type': 'application/json',
Accept: 'application/json',
},
body,
},
);
const json = await res.json();
if (json.errors) {
throw new Error(
'Error running introspection query, errors=' +
JSON.stringify(resp.errors),
);
} else {
return json.data;
}
}
function writeFile(path, content) {
fs.writeFileSync(path, content);
}
async function main(config) {
const schema = await runIntrospectionQuery();
writeFile(config.path, JSON.stringify(schema, null, 2));
}
const argv = yargs
.usage('Fetch OneGraph schema $0 --path <path>')
.options({
path: {
describe: 'Path to save schema.json',
demandOption: true,
type: 'string',
array: false,
},
})
.help().argv;
main(argv).catch(error => {
console.error(String(error.stack || error));
process.exit(1);
});