This repository has been archived by the owner on Oct 14, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
batchloader.resolvers.js
executable file
·418 lines (354 loc) · 13.7 KB
/
batchloader.resolvers.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
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
/* eslint-disable no-unused-vars */
// Define GraphQL resolvers using Feathers services and BatchLoaders. (Can be re-generated.)
const { getByDot, setByDot } = require('feathers-hooks-common');
// !code: imports // !end
// !code: init // !end
let moduleExports = function batchLoaderResolvers(app, options) {
// eslint-disable-next-line
let { convertArgsToParams, convertArgsToFeathers, extractAllItems, extractFirstItem,
feathersBatchLoader: { feathersBatchLoader } } = options;
// !<DEFAULT> code: max-batch-size
let defaultPaginate = app.get('paginate');
let maxBatchSize = defaultPaginate && typeof defaultPaginate.max === 'number' ?
defaultPaginate.max : undefined;
// !end
// !<DEFAULT> code: extra_auth_props
const convertArgs = convertArgsToFeathers([]);
// !end
// !<DEFAULT> code: services
let comments = app.service('/comments');
let likes = app.service('/likes');
let posts = app.service('/posts');
let relationships = app.service('/relationships');
let users = app.service('/users');
// !end
// !<DEFAULT> code: get-result
// Given a fieldName in the parent record, return the result from a BatchLoader
// The result will be an object (or null), or an array of objects (possibly empty).
function getResult(batchLoaderName, fieldName, isArray) {
const contentByDot = `batchLoaders.${batchLoaderName}`;
// `content.app = app` is the Feathers app object.
// `content.batchLoaders = {}` is where the BatchLoaders for a request are stored.
return (parent, args, content, ast) => {
let batchLoader = getByDot(content, contentByDot);
if (!batchLoader) {
batchLoader = getBatchLoader(batchLoaderName, parent, args, content, ast);
setByDot(content, contentByDot, batchLoader);
}
const returns1 = batchLoader.load(parent[fieldName]);
return !isArray ? returns1 : returns1.then(result => result || []);
};
}
// !end
// A transient BatchLoader can be created only when (one of) its resolver has been called,
// as the BatchLoader loading function may require data from the 'args' passed to the resolver.
// Note that each resolver's 'args' are static throughout a GraphQL call.
function getBatchLoader(dataLoaderName, parent, args, content, ast) {
let feathersParams;
switch (dataLoaderName) {
/* Persistent BatchLoaders. Stored in `content.batchLoaders._persisted`. */
// !<DEFAULT> code: bl-persisted
// case '_persisted.user.one.id': // service user, returns one object, key is field id
// !end
/* Transient BatchLoaders shared among resolvers. Stored in `content.batchLoaders._shared`. */
// !code: bl-shared
// *.*: User
case '_shared.users.one.uuid': // service user, returns one object, key is field uuid
return feathersBatchLoader(dataLoaderName, '!', 'uuid',
keys => {
feathersParams = convertArgs(args, content, null,
{ query : { uuid: { $in: keys } } }
);
return users.find(feathersParams);
},
50
);
// !end
/* Transient BatchLoaders used by only one resolver. Stored in `content.batchLoaders`. */
// Comment.author: User!
// !code: bl-Comment-author
// ... Using instead _shared.users.one.id
// !end
// Comment.likes: [Like!]
// !<DEFAULT> code: bl-Comment-likes
case 'Comment.likes':
return feathersBatchLoader(dataLoaderName, '[!]', 'commentUuid',
keys => {
feathersParams = convertArgs(args, content, null, {
query: { commentUuid: { $in: keys }, $sort: undefined },
_populate: 'skip', paginate: false
});
return likes.find(feathersParams);
},
maxBatchSize // Max #keys in a BatchLoader func call.
);
// !end
// Like.author: User!
// !code: bl-Like-author
// ... Using instead _shared.users.one.id
// !end
// Like.comment: Comment!
// !<DEFAULT> code: bl-Like-comment
case 'Like.comment':
return feathersBatchLoader(dataLoaderName, '!', 'uuid',
keys => {
feathersParams = convertArgs(args, content, null, {
query: { uuid: { $in: keys }, $sort: undefined },
_populate: 'skip', paginate: false
});
return comments.find(feathersParams);
},
maxBatchSize // Max #keys in a BatchLoader func call.
);
// !end
// Post.author: User!
// !code: bl-Post-author
// ... Using instead _shared.users.one.id
// !end
// Post.comments: [Comment!]
// !<DEFAULT> code: bl-Post-comments
case 'Post.comments':
return feathersBatchLoader(dataLoaderName, '[!]', 'postUuid',
keys => {
feathersParams = convertArgs(args, content, null, {
query: { postUuid: { $in: keys }, $sort: undefined },
_populate: 'skip', paginate: false
});
return comments.find(feathersParams);
},
maxBatchSize // Max #keys in a BatchLoader func call.
);
// !end
// Relationship.follower: User!
// !code: bl-Relationship-follower
// ... Using instead _shared.users.one.id
// !end
// Relationship.followee: User!
// !code: bl-Relationship-followee
// ... Using instead _shared.users.one.id
// !end
// User.comments: [Comment!]
// !<DEFAULT> code: bl-User-comments
case 'User.comments':
return feathersBatchLoader(dataLoaderName, '[!]', 'authorUuid',
keys => {
feathersParams = convertArgs(args, content, null, {
query: { authorUuid: { $in: keys }, $sort: undefined },
_populate: 'skip', paginate: false
});
return comments.find(feathersParams);
},
maxBatchSize // Max #keys in a BatchLoader func call.
);
// !end
// User.followed_by: [Relationship!]
// !<DEFAULT> code: bl-User-followed_by
case 'User.followed_by':
return feathersBatchLoader(dataLoaderName, '[!]', 'followeeUuid',
keys => {
feathersParams = convertArgs(args, content, null, {
query: { followeeUuid: { $in: keys }, $sort: undefined },
_populate: 'skip', paginate: false
});
return relationships.find(feathersParams);
},
maxBatchSize // Max #keys in a BatchLoader func call.
);
// !end
// User.following: [Relationship!]
// !<DEFAULT> code: bl-User-following
case 'User.following':
return feathersBatchLoader(dataLoaderName, '[!]', 'followerUuid',
keys => {
feathersParams = convertArgs(args, content, null, {
query: { followerUuid: { $in: keys }, $sort: undefined },
_populate: 'skip', paginate: false
});
return relationships.find(feathersParams);
},
maxBatchSize // Max #keys in a BatchLoader func call.
);
// !end
// User.likes: [Like!]
// !<DEFAULT> code: bl-User-likes
case 'User.likes':
return feathersBatchLoader(dataLoaderName, '[!]', 'authorUuid',
keys => {
feathersParams = convertArgs(args, content, null, {
query: { authorUuid: { $in: keys }, $sort: undefined },
_populate: 'skip', paginate: false
});
return likes.find(feathersParams);
},
maxBatchSize // Max #keys in a BatchLoader func call.
);
// !end
// User.posts(query: JSON, params: JSON, key: JSON): [Post!]
// !code: bl-User-posts
case 'User.posts':
return feathersBatchLoader(dataLoaderName, '[!]', 'authorUuid',
keys => {
feathersParams = convertArgs(args, content, null,
{ query: { authorUuid: { $in: keys }, $sort: { uuid: 1 } }, populate: false }
);
return posts.find(feathersParams);
}
);
// !end
/* Throw on unknown BatchLoader name. */
default:
// !<DEFAULT> code: bl-default
throw new Error(`GraphQL query requires BatchLoader named '${dataLoaderName}' but no definition exists for it.`);
// !end
}
}
let returns = {
Comment: {
// author: User!
// !code: resolver-Comment-author
author: getResult('_shared.users.one.uuid', 'authorUuid'),
// !end
// likes: [Like!]
// !<DEFAULT> code: resolver-Comment-likes
likes: getResult('Comment.likes', 'uuid', true),
// !end
},
Like: {
// author: User!
// !code: resolver-Like-author
author: getResult('_shared.users.one.uuid', 'authorUuid'),
// !end
// comment: Comment!
// !<DEFAULT> code: resolver-Like-comment
comment: getResult('Like.comment', 'commentUuid'),
// !end
},
Post: {
// author: User!
// !code: resolver-Post-author
author: getResult('_shared.users.one.uuid', 'authorUuid'),
// !end
// comments: [Comment!]
// !<DEFAULT> code: resolver-Post-comments
comments: getResult('Post.comments', 'uuid', true),
// !end
},
Relationship: {
// follower: User!
// !code: resolver-Relationship-follower
follower: getResult('_shared.users.one.uuid', 'followerUuid'),
// !end
// followee: User!
// !code: resolver-Relationship-followee
followee: getResult('_shared.users.one.uuid', 'followeeUuid'),
// !end
},
User: {
// comments: [Comment!]
// !<DEFAULT> code: resolver-User-comments
comments: getResult('User.comments', 'uuid', true),
// !end
// followed_by: [Relationship!]
// !<DEFAULT> code: resolver-User-followed_by
followed_by: getResult('User.followed_by', 'uuid', true),
// !end
// following: [Relationship!]
// !<DEFAULT> code: resolver-User-following
following: getResult('User.following', 'uuid', true),
// !end
// fullName: String!
// !code: resolver-User-fullName-non
fullName: ({ firstName, lastName }, args, context, ast) => `${firstName} ${lastName}`,
// !end
// likes: [Like!]
// !<DEFAULT> code: resolver-User-likes
likes: getResult('User.likes', 'uuid', true),
// !end
// posts(query: JSON, params: JSON, key: JSON): [Post!]
// !<DEFAULT> code: resolver-User-posts
posts: getResult('User.posts', 'uuid', true),
// !end
},
// !code: resolver_field_more // !end
Query: {
// !<DEFAULT> code: query-Comment
// getComment(query: JSON, params: JSON, key: JSON): Comment
getComment(parent, args, content, ast) {
const feathersParams = convertArgs(args, content, ast);
return comments.get(args.key, feathersParams).then(extractFirstItem);
},
// findComment(query: JSON, params: JSON): [Comment!]
findComment(parent, args, content, ast) {
const feathersParams = convertArgs(args, content, ast, { query: { $sort: { uuid: 1 } } });
return comments.find(feathersParams).then(paginate(content)).then(extractAllItems);
},
// !end
// !<DEFAULT> code: query-Like
// getLike(query: JSON, params: JSON, key: JSON): Like
getLike(parent, args, content, ast) {
const feathersParams = convertArgs(args, content, ast);
return likes.get(args.key, feathersParams).then(extractFirstItem);
},
// findLike(query: JSON, params: JSON): [Like!]
findLike(parent, args, content, ast) {
const feathersParams = convertArgs(args, content, ast, { query: { $sort: { uuid: 1 } } });
return likes.find(feathersParams).then(paginate(content)).then(extractAllItems);
},
// !end
// !<DEFAULT> code: query-Post
// getPost(query: JSON, params: JSON, key: JSON): Post
getPost(parent, args, content, ast) {
const feathersParams = convertArgs(args, content, ast);
return posts.get(args.key, feathersParams).then(extractFirstItem);
},
// findPost(query: JSON, params: JSON): [Post!]
findPost(parent, args, content, ast) {
const feathersParams = convertArgs(args, content, ast, { query: { $sort: { uuid: 1 } } });
return posts.find(feathersParams).then(paginate(content)).then(extractAllItems);
},
// !end
// !<DEFAULT> code: query-Relationship
// getRelationship(query: JSON, params: JSON, key: JSON): Relationship
getRelationship(parent, args, content, ast) {
const feathersParams = convertArgs(args, content, ast);
return relationships.get(args.key, feathersParams).then(extractFirstItem);
},
// findRelationship(query: JSON, params: JSON): [Relationship!]
findRelationship(parent, args, content, ast) {
const feathersParams = convertArgs(args, content, ast, { query: { $sort: { uuid: 1 } } });
return relationships.find(feathersParams).then(paginate(content)).then(extractAllItems);
},
// !end
// !<DEFAULT> code: query-User
// getUser(query: JSON, params: JSON, key: JSON): User
getUser(parent, args, content, ast) {
const feathersParams = convertArgs(args, content, ast);
return users.get(args.key, feathersParams).then(extractFirstItem);
},
// findUser(query: JSON, params: JSON): [User!]
findUser(parent, args, content, ast) {
const feathersParams = convertArgs(args, content, ast, { query: { $sort: { uuid: 1 } } });
return users.find(feathersParams).then(paginate(content)).then(extractAllItems);
},
// !end
// !code: resolver_query_more // !end
},
};
// !code: func_return // !end
return returns;
};
// !code: more // !end
// !code: exports // !end
module.exports = moduleExports;
function paginate(content) {
return result => {
content.pagination = !result.data ? undefined : {
total: result.total,
limit: result.limit,
skip: result.skip,
};
return result;
};
}
// !code: funcs // !end
// !code: end // !end