-
Notifications
You must be signed in to change notification settings - Fork 4
/
fetchODBSchema.mjs
executable file
·80 lines (63 loc) · 2.02 KB
/
fetchODBSchema.mjs
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
75
76
77
78
79
80
#!/usr/bin/env node
import { writeFile } from 'fs/promises';
import { buildClientSchema, getIntrospectionQuery, printSchema } from 'graphql';
//
// fetchODBSchema.mjs
//
// Usage: fetchODBSchema.mjs [local|dev|staging]
//
// Fetches the current version of the ODB schema from either a 'local' ODB
// instance running on localhost, or else from the 'dev' or 'staging' ODB. Places it
// in the proper place in `lucuma-schemas` for use in generating code.
//
const KEY_MESSAGE = `
Define the ODB_API_KEY environment variable to use this script. For example:
export ODB_API_KEY="111.4ed718065f21a8f01014c8d08db2c0f74fe3e860ae25bb1f03c5ae4c5e2d0a0e677e6e781a086d0a6638960506cff7db"
You can get an API key from the Explore "User Preferences" menu item in Explore.`;
const RED = '\x1b[0;31m';
const NC = '\x1b[0m';
if (!process.env.ODB_API_KEY) {
console.error(`${RED}${KEY_MESSAGE}${NC}`);
process.exit(1);
}
function usage() {
console.error(`${RED}Usage: ${process.argv[1]} [local|dev|staging]${NC}`);
process.exit(1);
}
let url;
// [2] is the first arg (after node and the script name)
switch (process.argv[2]) {
case 'local':
url = 'http://localhost:8082/odb';
break;
case 'dev':
url = 'https://lucuma-postgres-odb-dev.herokuapp.com/odb';
break;
case 'staging':
url = 'https://lucuma-postgres-odb-staging.herokuapp.com/odb';
break;
default:
usage();
break;
}
const response = await fetch(new URL(url), {
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${process.env.ODB_API_KEY}`,
},
method: 'POST',
body: JSON.stringify({ query: getIntrospectionQuery() }),
});
if (!response.ok) {
throw new Error(
`Failed to fetch introspection query: ${response.statusText}`
);
}
console.log('Fetched ODB schema.');
const data = (await response.json()).data;
const schema = printSchema(buildClientSchema(data));
await writeFile(
'lucuma-schemas/src/clue/resources/lucuma/schemas/ObservationDB.graphql',
schema
);
console.log('Wrote ODB schema to lucuma-schemas.');