forked from kriasoft/react-starter-kit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate-schema.js
36 lines (33 loc) · 1.21 KB
/
update-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
/* SPDX-FileCopyrightText: 2014-present Kriasoft <[email protected]> */
/* SPDX-License-Identifier: MIT */
const fs = require("fs");
const path = require("path");
const got = require("got");
const { format } = require("prettier");
const {
getIntrospectionQuery,
buildClientSchema,
printSchema,
} = require("graphql");
// Get GraphQL API URL for the the selected environment.
// Example: `yarn update-schema --env=test`
require("@babel/register")({ extensions: [".ts"], cache: false });
const { env } = require("minimist")(process.argv.slice(2));
const { api } = require("../config")[env || "local"];
const url = `${api.origin}${api.prefix}${api.path}`;
// Download and save GraphQL API schema
got
.post(url, { json: { query: getIntrospectionQuery() } })
.json()
.then((res) => {
const schema = buildClientSchema(res.data);
const filename = path.resolve(__dirname, "../schema.graphql");
let output = printSchema(schema, { commentDescriptions: true });
output = format(output, { parser: "graphql" });
fs.writeFileSync(filename, output, { encoding: "utf-8" });
console.log(`Saved ${url} to ${path.basename(filename)}`);
})
.catch((err) => {
console.error(err.stack);
process.exit(1);
});