-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathfetchGraphQLSchema.js
68 lines (57 loc) · 1.68 KB
/
fetchGraphQLSchema.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
62
63
64
65
66
67
68
require("dotenv").config()
const fs = require("fs")
const gradient = require("gradient-string")
const path = require("path")
const ProgressBar = require("progress")
const { fetch } = require("cross-undici-fetch")
const {
buildClientSchema,
getIntrospectionQuery,
printSchema,
} = require("graphql")
const supagradient = gradient(["#00CB8A", "#78E0B8"])
function fetchGraphQLSchema(url, options) {
options = options || {} // eslint-disable-line no-param-reassign
const bar = new ProgressBar("🔦 Introspecting schema [:bar]", 24)
const id = setInterval(function () {
bar.tick()
if (bar.complete) {
clearInterval(id)
}
}, 250)
return fetch(url, {
method: "POST",
headers: {
Accept: "application/json",
"Content-Type": "application/json",
apiKey: process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY,
},
body: JSON.stringify({
query: getIntrospectionQuery(),
}),
})
.then((res) => res.json())
.then((schemaJSON) => {
if (options.readable) {
return printSchema(buildClientSchema(schemaJSON.data))
}
bar.complete()
return JSON.stringify(schemaJSON, null, 2)
})
}
const filePath = path.join(__dirname, "../graphql/schema/", "schema.graphql")
console.log(
supagradient(
`🗞 Fetching GraphQL Schema from ${process.env.SUPABASE_PROJECT_REF} ...`
)
)
fetchGraphQLSchema(
`https://${process.env.NEXT_PUBLIC_SUPABASE_PROJECT_REF}.supabase.co/graphql/v1`,
{
readable: true,
}
).then((schema) => {
fs.writeFileSync(filePath, schema, "utf-8")
console.log(supagradient(`✨ Saved to ${filePath}`))
console.log('💡 Be sure to run "yarn run codegen" to generate latest types.')
})