Skip to content

Commit 61633a6

Browse files
committed
fix(SearchConnection): Add checks for sort arg presence and its values are unique
1 parent 9ebe980 commit 61633a6

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

src/resolvers/searchConnection.js

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,14 @@ export default function createSearchConnectionResolver(
5454
resolver.resolve = async rp => {
5555
const { args = {}, projection = {} } = rp;
5656

57+
if (!args.sort || !Array.isArray(args.sort) || args.sort.length === 0) {
58+
throw new Error(
59+
'Argument `sort` is required for the Relay Connection. According to ' +
60+
'the fields in `sort` will be constructed `cursor`s for every edge. ' +
61+
'Values of fields which used in `sort` should be unique in compound.'
62+
);
63+
}
64+
5765
const first = parseInt(args.first, 10) || 0;
5866
if (first < 0) {
5967
throw new Error('Argument `first` should be non-negative number.');
@@ -95,7 +103,21 @@ export default function createSearchConnectionResolver(
95103

96104
const hasExtraRecords = list.length > limit;
97105
if (hasExtraRecords) list = list.slice(0, limit);
98-
const edges = list.map(node => ({ node, cursor: dataToCursor(node.sort) }));
106+
const cursorMap = new Map();
107+
const edges = list.map(node => {
108+
const cursor = dataToCursor(node.sort);
109+
if (cursorMap.has(cursor)) {
110+
throw new Error(
111+
'Argument `sort` should be more complex. `cursor` are constructed ' +
112+
'according to the sort fields. Detected that two records have ' +
113+
`the same cursors '${cursor}' with data '${unbase64(cursor)}'. ` +
114+
'You should add more `sort` fields, which provide unique data ' +
115+
'for all cursors in the result set (eg. `id` field).'
116+
);
117+
}
118+
cursorMap.set(cursor, node);
119+
return { node, cursor };
120+
});
99121
const result = {
100122
...res,
101123
pageInfo: {

0 commit comments

Comments
 (0)