-
Notifications
You must be signed in to change notification settings - Fork 37
/
ConnectionResolver.js
47 lines (40 loc) · 1.14 KB
/
ConnectionResolver.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
import Models from 'Models';
import BaseResolver from './Resolver';
export default class BaseConnectionResolver extends BaseResolver {
MAX_LIMIT = 30;
resolve = async () => {
const {
limit,
before,
after,
orderBy,
exclude,
orderDirection = 'DESC',
} = this.args;
const Op = Models.Sequelize.Op;
const where = {};
if (exclude?.length) where.id = { [Op.notIn]: exclude };
const query = (await this.query?.call(this)) || {};
query.where = query.where ? { [Op.and]: [query.where, where] } : where;
const { results, cursors } = await this.model.paginate({
limit: this.MAX_LIMIT ? Math.min(limit || 1, this.MAX_LIMIT) : limit,
before,
after,
order: orderBy,
desc: orderDirection === 'DESC',
...query,
});
return {
edges: results.map((node) => ({
node,
cursor: Buffer.from(JSON.stringify([node.id])).toString('base64'),
})),
pageInfo: {
startCursor: cursors.before,
endCursor: cursors.after,
hasNextPage: cursors.hasNext,
hasPreviousPage: cursors.hasPrevious,
},
};
};
}