@@ -54,6 +54,14 @@ export default function createSearchConnectionResolver(
54
54
resolver . resolve = async rp => {
55
55
const { args = { } , projection = { } } = rp ;
56
56
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
+
57
65
const first = parseInt ( args . first , 10 ) || 0 ;
58
66
if ( first < 0 ) {
59
67
throw new Error ( 'Argument `first` should be non-negative number.' ) ;
@@ -95,7 +103,21 @@ export default function createSearchConnectionResolver(
95
103
96
104
const hasExtraRecords = list . length > limit ;
97
105
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
+ } ) ;
99
121
const result = {
100
122
...res ,
101
123
pageInfo : {
0 commit comments