-
Notifications
You must be signed in to change notification settings - Fork 12
/
index.js
175 lines (165 loc) · 5.59 KB
/
index.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
// based on Relay's Connection spec at
// https://facebook.github.io/relay/graphql/connections.htm#sec-Pagination-algorithm
/**
* Slices the nodes list according to the `before` and `after` graphql query params.
* @param {Object} allNodesAccessor an accessor to the nodes. Will depend on the concrete implementor.
* @param {Object} graphqlParams must contain `before` and `after` query params.
* @param {Object} operatorFunctions must contain removeNodesBeforeAndIncluding and removeNodesAfterAndIncluding functions.
* @param {Object} orderArgs must contain orderColumn and ascOrDesc. Include only if the implementor requires these params.
*/
const applyCursorsToNodes = (
allNodesAccessor,
{ before, after }, {
removeNodesBeforeAndIncluding,
removeNodesAfterAndIncluding,
}, {
orderColumn, ascOrDesc, isAggregateFn, formatColumnFn,
},
) => {
let nodesAccessor = allNodesAccessor;
if (after) {
nodesAccessor = removeNodesBeforeAndIncluding(nodesAccessor, after, {
orderColumn, ascOrDesc, isAggregateFn, formatColumnFn,
});
}
if (before) {
nodesAccessor = removeNodesAfterAndIncluding(nodesAccessor, before, {
orderColumn, ascOrDesc, isAggregateFn, formatColumnFn,
});
}
return nodesAccessor;
};
/**
* Slices a node list according to `before`, `after`, `first` and `last` graphql query params.
* @param {Object} allNodesAccessor an accessor to the nodes. Will depend on the concrete implementor.
* @param {Object} operatorFunctions must contain `getNodesLength`, `removeNodesFromEnd`, `removeNodesFromBeginning`,`removeNodesBeforeAndIncluding` and `removeNodesAfterAndIncluding` functions.
* @param {Object} graphqlParams must contain `first`, `last`, `before` and `after` query params.
* @param {Object} orderArgs must contain orderColumn and ascOrDesc. Include only if the implementor requires these params.
*/
const nodesToReturn = async (
allNodesAccessor,
{
removeNodesBeforeAndIncluding,
removeNodesAfterAndIncluding,
hasLengthGreaterThan,
removeNodesFromEnd,
removeNodesFromBeginning,
orderNodesBy,
},
{
before, after, first, last,
}, {
orderColumn, ascOrDesc, isAggregateFn, formatColumnFn,
},
) => {
const orderedNodesAccessor = orderNodesBy(allNodesAccessor, {
orderColumn, ascOrDesc, isAggregateFn, formatColumnFn,
});
const nodesAccessor = applyCursorsToNodes(
orderedNodesAccessor,
{ before, after },
{
removeNodesBeforeAndIncluding,
removeNodesAfterAndIncluding,
}, {
orderColumn, ascOrDesc, isAggregateFn, formatColumnFn,
},
);
let hasNextPage = !!before;
let hasPreviousPage = !!after;
let nodes = [];
if (first) {
if (first < 0) throw new Error('`first` argument must not be less than 0');
nodes = await removeNodesFromEnd(nodesAccessor, first + 1, { orderColumn, ascOrDesc });
if (nodes.length > first) {
hasNextPage = true;
nodes = nodes.slice(0, first);
}
}
if (last) {
if (last < 0) throw new Error('`last` argument must not be less than 0');
nodes = await removeNodesFromBeginning(nodesAccessor, last + 1, { orderColumn, ascOrDesc });
if (nodes.length > last) {
hasPreviousPage = true;
nodes = nodes.slice(1);
}
}
return { nodes, hasNextPage, hasPreviousPage };
};
/**
* Returns a function that must be called to generate a Relay's Connection based page.
* @param {Object} operatorFunctions must contain `getNodesLength`, `removeNodesFromEnd`, `removeNodesFromBeginning`,`removeNodesBeforeAndIncluding` and `removeNodesAfterAndIncluding` functions.
*/
const apolloCursorPaginationBuilder = ({
removeNodesBeforeAndIncluding,
removeNodesAfterAndIncluding,
getNodesLength,
hasLengthGreaterThan,
removeNodesFromEnd,
removeNodesFromBeginning,
convertNodesToEdges,
orderNodesBy,
}) => async (
allNodesAccessor,
{
before, after, first, last, orderBy = 'id', orderDirection = 'asc',
},
opts = {},
) => {
const {
isAggregateFn, formatColumnFn, skipTotalCount = false, modifyEdgeFn,
} = opts;
let {
orderColumn, ascOrDesc,
} = opts;
if (orderColumn) {
console.warn('"orderColumn" and "ascOrDesc" are being deprecated in favor of "orderBy" and "orderDirection" respectively');
} else {
orderColumn = orderBy;
ascOrDesc = orderDirection;
}
if (formatColumnFn && formatColumnFn(orderColumn) === orderColumn) {
console.warn(`orderBy ${orderColumn} should not equal its formatted counterpart: ${formatColumnFn(orderColumn)}.`);
console.warn('This may cause issues with cursors being generated properly.');
}
const { nodes, hasPreviousPage, hasNextPage } = await nodesToReturn(
allNodesAccessor,
{
removeNodesBeforeAndIncluding,
removeNodesAfterAndIncluding,
getNodesLength,
hasLengthGreaterThan,
removeNodesFromEnd,
removeNodesFromBeginning,
orderNodesBy,
}, {
before, after, first, last,
}, {
orderColumn, ascOrDesc, isAggregateFn, formatColumnFn,
},
);
const totalCount = !skipTotalCount && await getNodesLength(allNodesAccessor, {
getNodesLength,
});
let edges = convertNodesToEdges(nodes, {
before, after, first, last,
}, {
orderColumn, ascOrDesc, isAggregateFn, formatColumnFn,
});
if (modifyEdgeFn) {
edges = edges.map(edge => modifyEdgeFn(edge));
}
const startCursor = edges[0] && edges[0].cursor;
const endCursor = edges[edges.length - 1] && edges[edges.length - 1].cursor;
return {
pageInfo: {
hasPreviousPage,
hasNextPage,
startCursor,
endCursor,
},
totalCount,
edges,
};
};
module.exports = apolloCursorPaginationBuilder;