-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathintrospectPrismicGraphql.js
43 lines (39 loc) · 1.35 KB
/
introspectPrismicGraphql.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
const fetch = require("node-fetch");
const fs = require("fs");
const restUrl = "https://thec3.cdn.prismic.io/api/v2";
const graphqlUrl = "https://thec3.prismic.io/graphql";
const resultsFileName = "prismicIntrospectionResults";
fetch(`${restUrl}`)
.then((response) => response.json())
.then((data) => {
const masterRef = data.refs.find((response) => response.id === "master");
if (!masterRef) return;
fetch(
`${graphqlUrl}?query=%7B%20__schema%20%7B%20types%20%7B%20kind%20name%20possibleTypes%20%7B%20name%20%7D%20%7D%20%7D%20%7D`,
{
headers: {
"prismic-ref": masterRef.ref,
},
}
)
.then((result) => result.json())
.then((result) => {
try {
// here we're filtering out any type information unrelated to unions or interfaces
const filteredData = result.data.__schema.types.filter((type) => type.possibleTypes !== null);
result.data.__schema.types = filteredData;
writeFile(result.data);
} catch (error) {
console.log(error);
}
});
});
const writeFile = (data) => {
fs.writeFileSync(`./${resultsFileName}.json`, JSON.stringify(data), (error) => {
if (error) {
console.error("Error writing fragmentTypes file", error);
} else {
console.log("Fragment types successfully extracted!");
}
});
};