forked from kriasoft/react-starter-kit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate-schema.js
29 lines (26 loc) · 1.02 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
/* SPDX-FileCopyrightText: 2014-present Kriasoft */
/* SPDX-License-Identifier: MIT */
import envars from "envars";
import { got } from "got";
import { buildClientSchema, getIntrospectionQuery, printSchema } from "graphql";
import { format } from "prettier";
import { argv, fs, path } from "zx";
// Load the environment variables (API_ORIGIN, etc.)
envars.config({ env: argv.env ?? "local" });
const schemaURL = `${process.env.API_ORIGIN}/api`;
// Download and save GraphQL API schema
got
.post(schemaURL, { 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 ${schemaURL} to ${path.basename(filename)}`);
})
.catch((err) => {
console.error(err.stack);
process.exitCode = 1;
});