-
Notifications
You must be signed in to change notification settings - Fork 9
/
build-types.js
143 lines (137 loc) · 4.79 KB
/
build-types.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
const { getTypesMap } = require('./api');
const {
filterExcludedTypes,
getFieldType,
getTypeMap,
getTypeName,
getTypeKind,
getEntityType,
getEntityTypes,
isListType,
} = require('./helpers');
const getTypeDefs = (typeNames, typeMap, schema, entityTypeMap, inlineImages) => {
const typeDefs = {};
const foundTypes = [];
for (let typeName of typeNames) {
if (typeMap?.[typeName]) {
foundTypes.push(typeName);
} else {
console.warn('Could not find type: ', typeName);
}
}
for (let i = 0; i < foundTypes.length; i += 1) {
const name = foundTypes[i];
const type = typeMap?.[name];
switch (type.kind) {
case 'OBJECT':
typeDefs[type.name] = schema.buildObjectType({
name: `Strapi${type.name}`,
...entityTypeMap[type.name] && { interfaces: ['Node'] },
fields: type.fields.filter(filterExcludedTypes).reduce((acc, field) => {
const fieldTypeName = getTypeName(field.type);
// Add relationship resolver referenced collections.
const entityType = getEntityType(fieldTypeName);
if (entityType) {
if (entityTypeMap?.[entityType]) {
const typeName = `Strapi${entityType}`;
return Object.assign(acc, {
[field.name]: {
type: isListType(fieldTypeName) ? `[${typeName}]` : typeName,
resolve: (source, _, context) => {
const nodeId = source?.[field.name]?.nodeId;
if (nodeId) {
return context.nodeModel.getNodeById({
id: nodeId,
type: typeName,
});
}
const nodeIds = source?.[field.name]?.nodeIds;
if (nodeIds) {
return context.nodeModel.getNodesByIds({
ids: nodeIds,
type: typeName,
});
}
return null;
},
},
});
}
return acc;
} else {
const fieldTypeKind = getTypeKind(field.type);
switch (fieldTypeKind) {
case 'OBJECT':
case 'LIST': {
if (!typeDefs?.[fieldTypeName]) {
foundTypes.push(fieldTypeName);
}
break;
}
}
}
return Object.assign(acc, { [field.name]: getFieldType(field.type) });
}, {
strapiId: {
type: 'Int',
resolve: source => source?.strapiId || null,
},
...type.name === 'UploadFile' && {
file: {
type: 'File',
resolve: (source, _, context) => {
const fileId = source?.file;
if (fileId) {
return context.nodeModel.getNodeById({
id: fileId,
type: 'File',
});
}
return null;
},
},
},
...(inlineImages?.[type.name] || []).reduce((acc, field) => {
return {
...acc,
[`${field}_images`]: {
type: '[StrapiUploadFile]',
resolve: async (source, _, context) => {
const files = source?.[`${field}_images`] || [];
return files.map(file => ({
...file?.nodeId && context.nodeModel.getNodeById({ id: file.nodeId, type: 'StrapiUploadFile' }),
...file,
}));
},
},
}
}, {}),
}),
});
break;
case 'UNION':
typeDefs[type.name] = schema.buildUnionType({
name: `Strapi${type.name}`,
resolveType: value => `Strapi${value.__typename}`,
types: type.possibleTypes.map(unionType => {
const unionTypeName = getTypeName(unionType);
if (!typeDefs?.[unionTypeName]) {
foundTypes.push(unionTypeName);
}
return `Strapi${unionType.name}`;
}, {}),
});
default:
break;
}
};
return typeDefs;
}
module.exports = async (pluginOptions, schema) => {
const entityTypes = getEntityTypes(pluginOptions);
const entityTypeMap = getTypeMap(entityTypes);
const typeMap = await getTypesMap(pluginOptions);
const inlineImages = pluginOptions?.inlineImages?.typesToParse;
const result = getTypeDefs(entityTypes, typeMap, schema, entityTypeMap, inlineImages);
return Object.values(result);
};