-
Notifications
You must be signed in to change notification settings - Fork 6
/
buildGraphQLTypeData.js
57 lines (47 loc) · 1.63 KB
/
buildGraphQLTypeData.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
/* eslint-disable no-underscore-dangle, no-console */
const fs = require('fs');
function getFilteredTypesData(schema, filter) {
return {
...schema.data,
__schema: {
...schema.data.__schema,
types: schema.data.__schema.types.filter(filter),
},
};
}
// here we're filtering out any type information unrelated to unions or interfaces
function getFragmentTypesData(schema) {
return getFilteredTypesData(schema, (type) => type.possibleTypes !== null);
}
function getEnumTypesData(schema) {
const filteredTypes = getFilteredTypesData(
schema,
(type) => type.kind === 'ENUM' && !type.name.startsWith('__'),
).__schema.types;
return filteredTypes.reduce((acc, type) => ({ ...acc, [type.name]: type }), {});
}
function getPossibleTypesData(schema) {
const possibleTypes = {};
schema.data.__schema.types.forEach(supertype => {
if (supertype.possibleTypes) {
possibleTypes[supertype.name] = supertype.possibleTypes.map(subtype => subtype.name);
}
});
return possibleTypes;
}
function writeDataToFile(data, path) {
fs.writeFile(
path, JSON.stringify(data, null, 2), (err) => {
if (err) {
console.error(`Error writing ${path}`, err);
} else {
console.log(`${path} successfully extracted!`);
}
},
);
}
const schema = JSON.parse(fs.readFileSync('./schema.json'));
// no longer needed in @apollo/client 3
// writeDataToFile(getFragmentTypesData(schema), './app/javascript/fragmentTypes.json');
writeDataToFile(getPossibleTypesData(schema), './app/javascript/possibleTypes.json');
writeDataToFile(getEnumTypesData(schema), './app/javascript/enumTypes.json');