/
Schema.js
328 lines (279 loc) 路 8.64 KB
/
Schema.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
'use strict';
/**
* GraphQL.js service
*
* @description: A set of functions similar to controller's actions to avoid code duplication.
*/
const { gql, makeExecutableSchema } = require('apollo-server-koa');
const _ = require('lodash');
const graphql = require('graphql');
const Query = require('./Query.js');
const Mutation = require('./Mutation.js');
const Types = require('./Types.js');
const Resolvers = require('./Resolvers.js');
const schemaBuilder = {
/**
* Receive an Object and return a string which is following the GraphQL specs.
*
* @return String
*/
formatGQL: function(fields, description = {}, model = {}, type = 'field') {
const typeFields = JSON.stringify(fields, null, 2).replace(/['",]+/g, '');
const lines = typeFields.split('\n');
// Try to add description for field.
if (type === 'field') {
return lines
.map(line => {
if (['{', '}'].includes(line)) {
return '';
}
const split = line.split(':');
const attribute = _.trim(split[0]);
const info =
(_.isString(description[attribute])
? description[attribute]
: _.get(description[attribute], 'description')) ||
_.get(model, `attributes.${attribute}.description`);
const deprecated =
_.get(description[attribute], 'deprecated') ||
_.get(model, `attributes.${attribute}.deprecated`);
// Snakecase an attribute when we find a dash.
if (attribute.indexOf('-') !== -1) {
line = ` ${_.snakeCase(attribute)}: ${_.trim(split[1])}`;
}
if (info) {
line = ` """\n ${info}\n """\n${line}`;
}
if (deprecated) {
line = `${line} @deprecated(reason: "${deprecated}")`;
}
return line;
})
.join('\n');
} else if (type === 'query' || type === 'mutation') {
return lines
.map((line, index) => {
if (['{', '}'].includes(line)) {
return '';
}
const split = Object.keys(fields)[index - 1].split('(');
const attribute = _.trim(split[0]);
const info = _.get(description[attribute], 'description');
const deprecated = _.get(description[attribute], 'deprecated');
// Snakecase an attribute when we find a dash.
if (attribute.indexOf('-') !== -1) {
line = ` ${_.snakeCase(attribute)}(${_.trim(split[1])}`;
}
if (info) {
line = ` """\n ${info}\n """\n${line}`;
}
if (deprecated) {
line = `${line} @deprecated(reason: "${deprecated}")`;
}
return line;
})
.join('\n');
}
return lines
.map((line, index) => {
if ([0, lines.length - 1].includes(index)) {
return '';
}
return line;
})
.join('\n');
},
/**
* Retrieve description from variable and return a string which follow the GraphQL specs.
*
* @return String
*/
getDescription: (description, model = {}) => {
const format = '"""\n';
const str =
_.get(description, '_description') || _.isString(description)
? description
: undefined || _.get(model, 'info.description');
if (str) {
return `${format}${str}\n${format}`;
}
return '';
},
/**
* Generate GraphQL schema.
*
* @return Schema
*/
generateSchema: function() {
// Generate type definition and query/mutation for models.
let shadowCRUD = { definition: '', query: '', mutation: '', resolver: '' };
// build defaults schemas if shadowCRUD is enabled
if (strapi.plugins.graphql.config.shadowCRUD !== false) {
const modelCruds = Resolvers.buildShadowCRUD(
_.omit(strapi.models, ['core_store'])
);
shadowCRUD = Object.keys(strapi.plugins).reduce((acc, plugin) => {
const {
definition,
query,
mutation,
resolver,
} = Resolvers.buildShadowCRUD(strapi.plugins[plugin].models, plugin);
// We cannot put this in the merge because it's a string.
acc.definition += definition || '';
return _.merge(acc, {
query,
resolver,
mutation,
});
}, modelCruds);
}
let groups = Object.keys(strapi.groups)
.map(key =>
Resolvers.buildModel(strapi.groups[key], key, {
plugin: null,
isGroup: true,
})
)
.reduce(
(acc, group) => {
return {
definition: acc.definition + group.definition,
resolver: _.merge(acc.resolver, group.resolver),
};
},
{ definition: '', resolver: {} }
);
// Extract custom definition, query or resolver.
const {
definition,
query,
mutation,
resolver = {},
} = strapi.plugins.graphql.config._schema.graphql;
// Polymorphic.
const {
polymorphicDef,
polymorphicResolver,
} = Types.addPolymorphicUnionType(definition, shadowCRUD.definition);
// Build resolvers.
const resolvers =
_.omitBy(
_.merge(
shadowCRUD.resolver,
groups.resolver,
resolver,
polymorphicResolver
),
_.isEmpty
) || {};
// Transform object to only contain function.
Object.keys(resolvers).reduce((acc, type) => {
return Object.keys(acc[type]).reduce((acc, resolverName) => {
const resolverObj = acc[type][resolverName];
// Disabled this query.
if (resolverObj === false) {
delete acc[type][resolverName];
return acc;
}
if (_.isFunction(resolverObj)) {
return acc;
}
let plugin;
if (_.has(resolverObj, ['plugin'])) {
plugin = resolverObj.plugin;
} else if (_.has(resolverObj, ['resolver', 'plugin'])) {
plugin = resolverObj.resolver.plugin;
}
switch (type) {
case 'Mutation': {
let name, action;
if (
_.has(resolverObj, ['resolver']) &&
_.isString(resolverObj.resolver)
) {
[name, action] = resolverObj.resolver.split('.');
} else if (
_.has(resolverObj, ['resolver', 'handler']) &&
_.isString(resolverObj.resolver.handler)
) {
[name, action] = resolverObj.resolver.handler.split('.');
} else {
name = null;
action = resolverName;
}
const mutationResolver = Mutation.composeMutationResolver({
_schema: strapi.plugins.graphql.config._schema.graphql,
plugin,
name: _.toLower(name),
action,
});
acc[type][resolverName] = mutationResolver;
break;
}
case 'Query':
default:
acc[type][resolverName] = Query.composeQueryResolver({
_schema: strapi.plugins.graphql.config._schema.graphql,
plugin,
name: resolverName,
isSingular: 'force', // Avoid singular/pluralize and force query name.
});
break;
}
return acc;
}, acc);
}, resolvers);
// Return empty schema when there is no model.
if (_.isEmpty(shadowCRUD.definition) && _.isEmpty(definition)) {
return {};
}
// Concatenate.
let typeDefs = `
${definition}
${shadowCRUD.definition}
${groups.definition}
type Query {${shadowCRUD.query &&
this.formatGQL(
shadowCRUD.query,
resolver.Query,
null,
'query'
)}${query}}
type Mutation {${shadowCRUD.mutation &&
this.formatGQL(
shadowCRUD.mutation,
resolver.Mutation,
null,
'mutation'
)}${mutation}}
${Types.addCustomScalar(resolvers)}
${Types.addInput()}
${polymorphicDef}
`;
// // Build schema.
if (!strapi.config.currentEnvironment.server.production) {
// Write schema.
const schema = makeExecutableSchema({
typeDefs,
resolvers,
});
this.writeGenerateSchema(graphql.printSchema(schema));
}
// Remove custom scalar (like Upload);
typeDefs = Types.removeCustomScalar(typeDefs, resolvers);
return {
typeDefs: gql(typeDefs),
resolvers,
};
},
/**
* Save into a file the readable GraphQL schema.
*
* @return void
*/
writeGenerateSchema: schema => {
return strapi.fs.writeAppFile('exports/graphql/schema.graphql', schema);
},
};
module.exports = schemaBuilder;