forked from learn-anything/learn-anything
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathloadConnectionsIntoGrafbase.ts
46 lines (42 loc) · 1.28 KB
/
loadConnectionsIntoGrafbase.ts
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
async function main() {
const currentFilePath = import.meta.path
const connectionsFilePath = `${currentFilePath.replace(
"loadConnectionsIntoGrafbase.ts",
"connections.json"
)}`
const file = Bun.file(connectionsFilePath)
const fileContent = await file.text()
const obj = JSON.parse(fileContent)
let topicsWithConnections = JSON.stringify(obj, null, 2)
// Replace quotes around property names
topicsWithConnections = topicsWithConnections.replace(/"([^"]+)":/g, "$1:")
const query = `
mutation InternalUpdateGrafbaseKv($topicsWithConnections: [updateGrafbaseKvOutput!]!) {
internalUpdateGrafbaseKv(topicsWithConnections: $topicsWithConnections)
}
`
const variables = {
topicsWithConnections: obj.map((topic: any) => ({
name: topic.name,
prettyName: topic.prettyName,
connections: topic.connections
}))
}
const res = await fetch(process.env.GRAFBASE_URL!, {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${process.env.GRAFBASE_INTERNAL_SECRET!}`
},
body: JSON.stringify({
query,
variables
})
}).catch((err) => {
console.error(err, "err")
})
// @ts-ignore
const jsonBody = await res.json()
console.log(jsonBody, "json resp")
}
await main()