-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerateFragmentTypes.js
More file actions
44 lines (39 loc) · 1.13 KB
/
generateFragmentTypes.js
File metadata and controls
44 lines (39 loc) · 1.13 KB
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
const fs = require('fs');
const util = require('util');
const path = require('path');
const fetch = require('node-fetch');
require('dotenv').config();
const filePath = path.join(__dirname, '/src', 'fragmentTypes.json');
const YOUR_API_HOST = 'https://api.github.com';
fetch(`${YOUR_API_HOST}/graphql`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
authorization: `Bearer ${process.env.TOKEN}`,
},
body: JSON.stringify({
query: `
{
__schema {
types {
kind
name
possibleTypes {
name
}
}
}
}
`,
}),
})
.then(body => body.json())
.then((result) => {
// 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;
fs.writeFile(filePath, JSON.stringify(result.data), (err) => {
if (err) console.error('Error writing fragmentTypes file', err);
console.log('Fragment types successfully extracted!');
});
});