@@ -42,6 +42,39 @@ const defaultPageInfo: PageInfo = {
4242
4343const ensureKey = ( x : any ) : string | null => ( typeof x === 'string' ? x : null ) ;
4444
45+ const extractPaginationArgs = ( args : Variables ) => {
46+ const result = {
47+ first : undefined as number | undefined ,
48+ last : undefined as number | undefined ,
49+ after : null as string | null ,
50+ before : null as string | null ,
51+ } ;
52+
53+ // Check for nested args in common patterns
54+ const nestedPatterns = [ 'paging' , 'pagination' , 'page' ] ;
55+ for ( const pattern of nestedPatterns ) {
56+ const nested = args [ pattern ] ;
57+ if ( nested && typeof nested === 'object' ) {
58+ if ( 'first' in nested && typeof nested . first === 'number' )
59+ result . first = nested . first ;
60+ if ( 'last' in nested && typeof nested . last === 'number' )
61+ result . last = nested . last ;
62+ if ( 'after' in nested && typeof nested . after === 'string' )
63+ result . after = nested . after ;
64+ if ( 'before' in nested && typeof nested . before === 'string' )
65+ result . before = nested . before ;
66+ }
67+ }
68+
69+ // Check for direct args
70+ if ( typeof args . first === 'number' ) result . first = args . first ;
71+ if ( typeof args . last === 'number' ) result . last = args . last ;
72+ if ( typeof args . after === 'string' ) result . after = args . after ;
73+ if ( typeof args . before === 'string' ) result . before = args . before ;
74+
75+ return result ;
76+ } ;
77+
4578const concatEdges = (
4679 cache : Cache ,
4780 leftEdges : NullArray < string > ,
@@ -89,24 +122,65 @@ const concatNodes = (
89122 return newNodes ;
90123} ;
91124
92- const compareArgs = (
125+ const isPaginationArg = ( key : string , value : any ) : boolean => {
126+ // Direct pagination args
127+ if (
128+ key === 'first' ||
129+ key === 'last' ||
130+ key === 'after' ||
131+ key === 'before'
132+ ) {
133+ return true ;
134+ }
135+
136+ // Nested pagination args - check common patterns
137+ const nestedPatterns = [ 'paging' , 'pagination' , 'page' ] ;
138+ if (
139+ nestedPatterns . includes ( key ) &&
140+ typeof value === 'object' &&
141+ value !== null &&
142+ ! Array . isArray ( value )
143+ ) {
144+ const nested = value as Variables ;
145+ return (
146+ nested . first !== undefined ||
147+ nested . last !== undefined ||
148+ nested . after !== undefined ||
149+ nested . before !== undefined
150+ ) ;
151+ }
152+
153+ return false ;
154+ } ;
155+
156+ const areNonPaginationArgsEqual = (
93157 fieldArgs : Variables ,
94158 connectionArgs : Variables
95159) : boolean => {
160+ // Create versions of args without pagination args for comparison
161+ const filteredFieldArgs : Variables = { } ;
162+ const filteredConnectionArgs : Variables = { } ;
163+
164+ for ( const key in fieldArgs ) {
165+ if ( ! isPaginationArg ( key , fieldArgs [ key ] ) ) {
166+ filteredFieldArgs [ key ] = fieldArgs [ key ] ;
167+ }
168+ }
169+
96170 for ( const key in connectionArgs ) {
97- if (
98- key === 'first' ||
99- key === 'last' ||
100- key === 'after' ||
101- key === 'before'
102- ) {
103- continue ;
104- } else if ( ! ( key in fieldArgs ) ) {
171+ if ( ! isPaginationArg ( key , connectionArgs [ key ] ) ) {
172+ filteredConnectionArgs [ key ] = connectionArgs [ key ] ;
173+ }
174+ }
175+
176+ // Compare non-pagination args
177+ for ( const key in filteredConnectionArgs ) {
178+ if ( ! ( key in filteredFieldArgs ) ) {
105179 return false ;
106180 }
107181
108- const argA = fieldArgs [ key ] ;
109- const argB = connectionArgs [ key ] ;
182+ const argA = filteredFieldArgs [ key ] ;
183+ const argB = filteredConnectionArgs [ key ] ;
110184
111185 if (
112186 typeof argA !== typeof argB || typeof argA !== 'object'
@@ -117,17 +191,8 @@ const compareArgs = (
117191 }
118192 }
119193
120- for ( const key in fieldArgs ) {
121- if (
122- key === 'first' ||
123- key === 'last' ||
124- key === 'after' ||
125- key === 'before'
126- ) {
127- continue ;
128- }
129-
130- if ( ! ( key in connectionArgs ) ) return false ;
194+ for ( const key in filteredFieldArgs ) {
195+ if ( ! ( key in filteredConnectionArgs ) ) return false ;
131196 }
132197
133198 return true ;
@@ -235,7 +300,7 @@ export const relayPagination = (
235300
236301 for ( let i = 0 ; i < size ; i ++ ) {
237302 const { fieldKey, arguments : args } = fieldInfos [ i ] ;
238- if ( args === null || ! compareArgs ( fieldArgs , args ) ) {
303+ if ( args === null || ! areNonPaginationArgsEqual ( fieldArgs , args ) ) {
239304 continue ;
240305 }
241306
@@ -247,33 +312,35 @@ export const relayPagination = (
247312 continue ;
248313 }
249314
315+ const paginationArgs = extractPaginationArgs ( args ) ;
316+
250317 if (
251318 mergeMode === 'inwards' &&
252- typeof args . last === 'number' &&
253- typeof args . first === 'number'
319+ typeof paginationArgs . last === 'number' &&
320+ typeof paginationArgs . first === 'number'
254321 ) {
255- const firstEdges = page . edges . slice ( 0 , args . first + 1 ) ;
256- const lastEdges = page . edges . slice ( - args . last ) ;
257- const firstNodes = page . nodes . slice ( 0 , args . first + 1 ) ;
258- const lastNodes = page . nodes . slice ( - args . last ) ;
322+ const firstEdges = page . edges . slice ( 0 , paginationArgs . first + 1 ) ;
323+ const lastEdges = page . edges . slice ( - paginationArgs . last ) ;
324+ const firstNodes = page . nodes . slice ( 0 , paginationArgs . first + 1 ) ;
325+ const lastNodes = page . nodes . slice ( - paginationArgs . last ) ;
259326
260327 startEdges = concatEdges ( cache , startEdges , firstEdges ) ;
261328 endEdges = concatEdges ( cache , lastEdges , endEdges ) ;
262329 startNodes = concatNodes ( startNodes , firstNodes ) ;
263330 endNodes = concatNodes ( lastNodes , endNodes ) ;
264331
265332 pageInfo = page . pageInfo ;
266- } else if ( args . after ) {
333+ } else if ( paginationArgs . after ) {
267334 startEdges = concatEdges ( cache , startEdges , page . edges ) ;
268335 startNodes = concatNodes ( startNodes , page . nodes ) ;
269336 pageInfo . endCursor = page . pageInfo . endCursor ;
270337 pageInfo . hasNextPage = page . pageInfo . hasNextPage ;
271- } else if ( args . before ) {
338+ } else if ( paginationArgs . before ) {
272339 endEdges = concatEdges ( cache , page . edges , endEdges ) ;
273340 endNodes = concatNodes ( page . nodes , endNodes ) ;
274341 pageInfo . startCursor = page . pageInfo . startCursor ;
275342 pageInfo . hasPreviousPage = page . pageInfo . hasPreviousPage ;
276- } else if ( typeof args . last === 'number' ) {
343+ } else if ( typeof paginationArgs . last === 'number' ) {
277344 endEdges = concatEdges ( cache , page . edges , endEdges ) ;
278345 endNodes = concatNodes ( page . nodes , endNodes ) ;
279346 pageInfo = page . pageInfo ;
0 commit comments