-
-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Expand file tree
/
Copy pathparseClassTypes.js
More file actions
543 lines (518 loc) · 19.5 KB
/
parseClassTypes.js
File metadata and controls
543 lines (518 loc) · 19.5 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
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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
/* eslint-disable indent */
import {
GraphQLID,
GraphQLObjectType,
GraphQLString,
GraphQLList,
GraphQLInputObjectType,
GraphQLNonNull,
GraphQLBoolean,
GraphQLEnumType,
} from 'graphql';
import { globalIdField, connectionArgs, connectionDefinitions } from 'graphql-relay';
import getFieldNames from 'graphql-list-fields';
import * as defaultGraphQLTypes from './defaultGraphQLTypes';
import * as objectsQueries from '../helpers/objectsQueries';
import { ParseGraphQLClassConfig } from '../../Controllers/ParseGraphQLController';
import { transformClassNameToGraphQL } from '../transformers/className';
import { transformInputTypeToGraphQL } from '../transformers/inputType';
import { transformOutputTypeToGraphQL } from '../transformers/outputType';
import { transformConstraintTypeToGraphQL } from '../transformers/constraintType';
import { extractKeysAndInclude, getParseClassMutationConfig } from '../parseGraphQLUtils';
const getParseClassTypeConfig = function (parseClassConfig: ?ParseGraphQLClassConfig) {
return (parseClassConfig && parseClassConfig.type) || {};
};
const getInputFieldsAndConstraints = function (
parseClass,
parseClassConfig: ?ParseGraphQLClassConfig
) {
const classFields = Object.keys(parseClass.fields).concat('id');
const {
inputFields: allowedInputFields,
outputFields: allowedOutputFields,
constraintFields: allowedConstraintFields,
sortFields: allowedSortFields,
} = getParseClassTypeConfig(parseClassConfig);
let classOutputFields;
let classCreateFields;
let classUpdateFields;
let classConstraintFields;
let classSortFields;
// All allowed customs fields
const classCustomFields = classFields.filter(field => {
return !Object.keys(defaultGraphQLTypes.PARSE_OBJECT_FIELDS).includes(field) && field !== 'id';
});
if (allowedInputFields && allowedInputFields.create) {
classCreateFields = classCustomFields.filter(field => {
return allowedInputFields.create.includes(field);
});
} else {
classCreateFields = classCustomFields;
}
if (allowedInputFields && allowedInputFields.update) {
classUpdateFields = classCustomFields.filter(field => {
return allowedInputFields.update.includes(field);
});
} else {
classUpdateFields = classCustomFields;
}
if (allowedOutputFields) {
classOutputFields = classCustomFields.filter(field => {
return allowedOutputFields.includes(field);
});
} else {
classOutputFields = classCustomFields;
}
// Filters the "password" field from class _User
if (parseClass.className === '_User') {
classOutputFields = classOutputFields.filter(outputField => outputField !== 'password');
}
if (allowedConstraintFields) {
classConstraintFields = classCustomFields.filter(field => {
return allowedConstraintFields.includes(field);
});
} else {
classConstraintFields = classFields;
}
if (allowedSortFields) {
classSortFields = allowedSortFields;
if (!classSortFields.length) {
// must have at least 1 order field
// otherwise the FindArgs Input Type will throw.
classSortFields.push({
field: 'id',
asc: true,
desc: true,
});
}
} else {
classSortFields = classFields.map(field => {
return { field, asc: true, desc: true };
});
}
return {
classCreateFields,
classUpdateFields,
classConstraintFields,
classOutputFields,
classSortFields,
};
};
const load = (parseGraphQLSchema, parseClass, parseClassConfig: ?ParseGraphQLClassConfig) => {
const className = parseClass.className;
const graphQLClassName = transformClassNameToGraphQL(className);
const {
classCreateFields,
classUpdateFields,
classOutputFields,
classConstraintFields,
classSortFields,
} = getInputFieldsAndConstraints(parseClass, parseClassConfig);
const {
create: isCreateEnabled = true,
update: isUpdateEnabled = true,
} = getParseClassMutationConfig(parseClassConfig);
const classGraphQLCreateTypeName = `Create${graphQLClassName}FieldsInput`;
let classGraphQLCreateType = new GraphQLInputObjectType({
name: classGraphQLCreateTypeName,
description: `The ${classGraphQLCreateTypeName} input type is used in operations that involve creation of objects in the ${graphQLClassName} class.`,
fields: () =>
classCreateFields.reduce(
(fields, field) => {
const type = transformInputTypeToGraphQL(
parseClass.fields[field].type,
parseClass.fields[field].targetClass,
parseGraphQLSchema.parseClassTypes
);
if (type) {
return {
...fields,
[field]: {
description: `This is the object ${field}.`,
type: parseClass.fields[field].required ? new GraphQLNonNull(type) : type,
},
};
} else {
return fields;
}
},
{
ACL: { type: defaultGraphQLTypes.ACL_INPUT },
}
),
});
classGraphQLCreateType = parseGraphQLSchema.addGraphQLType(classGraphQLCreateType);
const classGraphQLUpdateTypeName = `Update${graphQLClassName}FieldsInput`;
let classGraphQLUpdateType = new GraphQLInputObjectType({
name: classGraphQLUpdateTypeName,
description: `The ${classGraphQLUpdateTypeName} input type is used in operations that involve creation of objects in the ${graphQLClassName} class.`,
fields: () =>
classUpdateFields.reduce(
(fields, field) => {
const type = transformInputTypeToGraphQL(
parseClass.fields[field].type,
parseClass.fields[field].targetClass,
parseGraphQLSchema.parseClassTypes
);
if (type) {
return {
...fields,
[field]: {
description: `This is the object ${field}.`,
type,
},
};
} else {
return fields;
}
},
{
ACL: { type: defaultGraphQLTypes.ACL_INPUT },
}
),
});
classGraphQLUpdateType = parseGraphQLSchema.addGraphQLType(classGraphQLUpdateType);
const classGraphQLPointerTypeName = `${graphQLClassName}PointerInput`;
let classGraphQLPointerType = new GraphQLInputObjectType({
name: classGraphQLPointerTypeName,
description: `Allow to link OR add and link an object of the ${graphQLClassName} class.`,
fields: () => {
const fields = {
link: {
description: `Link an existing object from ${graphQLClassName} class. You can use either the global or the object id.`,
type: GraphQLID,
},
};
if (isCreateEnabled) {
fields['createAndLink'] = {
description: `Create and link an object from ${graphQLClassName} class.`,
type: classGraphQLCreateType,
};
}
return fields;
},
});
classGraphQLPointerType =
parseGraphQLSchema.addGraphQLType(classGraphQLPointerType) || defaultGraphQLTypes.OBJECT;
const classGraphQLRelationTypeName = `${graphQLClassName}RelationInput`;
let classGraphQLRelationType = new GraphQLInputObjectType({
name: classGraphQLRelationTypeName,
description: `Allow to add, remove, createAndAdd objects of the ${graphQLClassName} class into a relation field.`,
fields: () => {
const fields = {
add: {
description: `Add existing objects from the ${graphQLClassName} class into the relation. You can use either the global or the object ids.`,
type: new GraphQLList(defaultGraphQLTypes.OBJECT_ID),
},
remove: {
description: `Remove existing objects from the ${graphQLClassName} class out of the relation. You can use either the global or the object ids.`,
type: new GraphQLList(defaultGraphQLTypes.OBJECT_ID),
},
};
if (isCreateEnabled) {
fields['createAndAdd'] = {
description: `Create and add objects of the ${graphQLClassName} class into the relation.`,
type: new GraphQLList(new GraphQLNonNull(classGraphQLCreateType)),
};
}
return fields;
},
});
classGraphQLRelationType =
parseGraphQLSchema.addGraphQLType(classGraphQLRelationType) || defaultGraphQLTypes.OBJECT;
const classGraphQLConstraintsTypeName = `${graphQLClassName}WhereInput`;
let classGraphQLConstraintsType = new GraphQLInputObjectType({
name: classGraphQLConstraintsTypeName,
description: `The ${classGraphQLConstraintsTypeName} input type is used in operations that involve filtering objects of ${graphQLClassName} class.`,
fields: () => ({
...classConstraintFields.reduce((fields, field) => {
if (['OR', 'AND', 'NOR'].includes(field)) {
parseGraphQLSchema.log.warn(
`Field ${field} could not be added to the auto schema ${classGraphQLConstraintsTypeName} because it collided with an existing one.`
);
return fields;
}
const parseField = field === 'id' ? 'objectId' : field;
const type = transformConstraintTypeToGraphQL(
parseClass.fields[parseField].type,
parseClass.fields[parseField].targetClass,
parseGraphQLSchema.parseClassTypes,
field
);
if (type) {
return {
...fields,
[field]: {
description: `This is the object ${field}.`,
type,
},
};
} else {
return fields;
}
}, {}),
OR: {
description: 'This is the OR operator to compound constraints.',
type: new GraphQLList(new GraphQLNonNull(classGraphQLConstraintsType)),
},
AND: {
description: 'This is the AND operator to compound constraints.',
type: new GraphQLList(new GraphQLNonNull(classGraphQLConstraintsType)),
},
NOR: {
description: 'This is the NOR operator to compound constraints.',
type: new GraphQLList(new GraphQLNonNull(classGraphQLConstraintsType)),
},
}),
});
classGraphQLConstraintsType =
parseGraphQLSchema.addGraphQLType(classGraphQLConstraintsType) || defaultGraphQLTypes.OBJECT;
const classGraphQLRelationConstraintsTypeName = `${graphQLClassName}RelationWhereInput`;
let classGraphQLRelationConstraintsType = new GraphQLInputObjectType({
name: classGraphQLRelationConstraintsTypeName,
description: `The ${classGraphQLRelationConstraintsTypeName} input type is used in operations that involve filtering objects of ${graphQLClassName} class.`,
fields: () => ({
have: {
description: 'Run a relational/pointer query where at least one child object can match.',
type: classGraphQLConstraintsType,
},
haveNot: {
description:
'Run an inverted relational/pointer query where at least one child object can match.',
type: classGraphQLConstraintsType,
},
exists: {
description: 'Check if the relation/pointer contains objects.',
type: GraphQLBoolean,
},
}),
});
classGraphQLRelationConstraintsType =
parseGraphQLSchema.addGraphQLType(classGraphQLRelationConstraintsType) ||
defaultGraphQLTypes.OBJECT;
const classGraphQLOrderTypeName = `${graphQLClassName}Order`;
let classGraphQLOrderType = new GraphQLEnumType({
name: classGraphQLOrderTypeName,
description: `The ${classGraphQLOrderTypeName} input type is used when sorting objects of the ${graphQLClassName} class.`,
values: classSortFields.reduce((sortFields, fieldConfig) => {
const { field, asc, desc } = fieldConfig;
const updatedSortFields = {
...sortFields,
};
const value = field === 'id' ? 'objectId' : field;
if (asc) {
updatedSortFields[`${field}_ASC`] = { value };
}
if (desc) {
updatedSortFields[`${field}_DESC`] = { value: `-${value}` };
}
return updatedSortFields;
}, {}),
});
classGraphQLOrderType = parseGraphQLSchema.addGraphQLType(classGraphQLOrderType);
const classGraphQLFindArgs = {
where: {
description: 'These are the conditions that the objects need to match in order to be found.',
type: classGraphQLConstraintsType,
},
order: {
description: 'The fields to be used when sorting the data fetched.',
type: classGraphQLOrderType
? new GraphQLList(new GraphQLNonNull(classGraphQLOrderType))
: GraphQLString,
},
skip: defaultGraphQLTypes.SKIP_ATT,
...connectionArgs,
options: defaultGraphQLTypes.READ_OPTIONS_ATT,
};
const classGraphQLOutputTypeName = `${graphQLClassName}`;
const interfaces = [defaultGraphQLTypes.PARSE_OBJECT, parseGraphQLSchema.relayNodeInterface];
const parseObjectFields = {
id: globalIdField(className, obj => obj.objectId),
...defaultGraphQLTypes.PARSE_OBJECT_FIELDS,
...(className === '_User'
? {
authDataResponse: {
description: `auth provider response when triggered on signUp/logIn.`,
type: defaultGraphQLTypes.OBJECT,
},
}
: {}),
};
const outputFields = () => {
return classOutputFields.reduce((fields, field) => {
const type = transformOutputTypeToGraphQL(
parseClass.fields[field].type,
parseClass.fields[field].targetClass,
parseGraphQLSchema.parseClassTypes
);
if (parseClass.fields[field].type === 'Relation') {
const targetParseClassTypes =
parseGraphQLSchema.parseClassTypes[parseClass.fields[field].targetClass];
const args = targetParseClassTypes ? targetParseClassTypes.classGraphQLFindArgs : undefined;
return {
...fields,
[field]: {
description: `This is the object ${field}.`,
args,
type: parseClass.fields[field].required ? new GraphQLNonNull(type) : type,
async resolve(source, args, context, queryInfo) {
try {
const { where, order, skip, first, after, last, before, options } = args;
const { readPreference, includeReadPreference, subqueryReadPreference } =
options || {};
const { config, auth, info } = context;
const selectedFields = getFieldNames(queryInfo);
const { keys, include } = extractKeysAndInclude(
selectedFields
.filter(field => field.startsWith('edges.node.'))
// GraphQL relation connections expose data under `edges.node.*`. Those
// segments do not correspond to actual Parse fields, so strip them to
// ensure the root relation key remains in the keys list (e.g. convert
// `users.edges.node.username` -> `users.username`). This preserves the
// synthetic relation placeholders that Parse injects while still
// respecting field projections.
.map(field => field.replace('edges.node.', '').replace(/\.edges\.node/g, ''))
.filter(field => field.indexOf('edges.node') < 0)
);
const parseOrder = order && order.join(',');
return objectsQueries.findObjects(
source[field].className,
{
$relatedTo: {
object: {
__type: 'Pointer',
className: className,
objectId: source.objectId,
},
key: field,
},
...(where || {}),
},
parseOrder,
skip,
first,
after,
last,
before,
keys,
include,
false,
readPreference,
includeReadPreference,
subqueryReadPreference,
config,
auth,
info,
selectedFields,
parseGraphQLSchema.parseClasses
);
} catch (e) {
parseGraphQLSchema.handleError(e);
}
},
},
};
} else if (parseClass.fields[field].type === 'Polygon') {
return {
...fields,
[field]: {
description: `This is the object ${field}.`,
type: parseClass.fields[field].required ? new GraphQLNonNull(type) : type,
async resolve(source) {
if (source[field] && source[field].coordinates) {
return source[field].coordinates.map(coordinate => ({
latitude: coordinate[0],
longitude: coordinate[1],
}));
} else {
return null;
}
},
},
};
} else if (parseClass.fields[field].type === 'Array') {
return {
...fields,
[field]: {
description: `Use Inline Fragment on Array to get results: https://graphql.org/learn/queries/#inline-fragments`,
type: parseClass.fields[field].required ? new GraphQLNonNull(type) : type,
async resolve(source) {
if (!source[field]) { return null; }
return source[field].map(async elem => {
if (elem.className && elem.objectId && elem.__type === 'Object') {
return elem;
} else {
return { value: elem };
}
});
},
},
};
} else if (type) {
return {
...fields,
[field]: {
description: `This is the object ${field}.`,
type: parseClass.fields[field].required ? new GraphQLNonNull(type) : type,
},
};
} else {
return fields;
}
}, parseObjectFields);
};
let classGraphQLOutputType = new GraphQLObjectType({
name: classGraphQLOutputTypeName,
description: `The ${classGraphQLOutputTypeName} object type is used in operations that involve outputting objects of ${graphQLClassName} class.`,
interfaces,
fields: outputFields,
});
classGraphQLOutputType = parseGraphQLSchema.addGraphQLType(classGraphQLOutputType);
const { connectionType, edgeType } = connectionDefinitions({
name: graphQLClassName,
connectionFields: {
count: defaultGraphQLTypes.COUNT_ATT,
},
nodeType: classGraphQLOutputType || defaultGraphQLTypes.OBJECT,
});
let classGraphQLFindResultType = undefined;
if (
parseGraphQLSchema.addGraphQLType(edgeType) &&
parseGraphQLSchema.addGraphQLType(connectionType, false, false, true)
) {
classGraphQLFindResultType = connectionType;
}
parseGraphQLSchema.parseClassTypes[className] = {
classGraphQLPointerType,
classGraphQLRelationType,
classGraphQLCreateType,
classGraphQLUpdateType,
classGraphQLConstraintsType,
classGraphQLRelationConstraintsType,
classGraphQLFindArgs,
classGraphQLOutputType,
classGraphQLFindResultType,
config: {
parseClassConfig,
isCreateEnabled,
isUpdateEnabled,
},
};
if (className === '_User') {
const viewerType = new GraphQLObjectType({
name: 'Viewer',
description: `The Viewer object type is used in operations that involve outputting the current user data.`,
fields: () => ({
sessionToken: defaultGraphQLTypes.SESSION_TOKEN_ATT,
user: {
description: 'This is the current user.',
type: new GraphQLNonNull(classGraphQLOutputType),
},
}),
});
parseGraphQLSchema.addGraphQLType(viewerType, true, true);
parseGraphQLSchema.viewerType = viewerType;
}
};
export { extractKeysAndInclude, load };