Skip to content

Commit 0d03239

Browse files
Fanaennodkz
authored andcommitted
fix(resolver): it now yields the correct dataset for last+before args
Target the issue #7 In backward situations with cursors, the resolver uses count to compute the "skip" value to forward to the findMany resolver. If the count value is the global one, and not the one with the rawQuery with the '$lt' statement, the value is incorrect. Hence the invalid dataset.
1 parent dbc8f8d commit 0d03239

File tree

2 files changed

+28
-3
lines changed

2 files changed

+28
-3
lines changed

src/__tests__/connectionResolver-test.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -598,6 +598,19 @@ describe('connectionResolver', () => {
598598
expect(conn.edges[0].node.id).toBe(result.edges[2].node.id);
599599
});
600600
});
601+
602+
it('should return previous edges when querying with last and before', async () => {
603+
const result = await connectionResolver.resolve({
604+
args: {
605+
sort: sortOptions.ID_ASC.value,
606+
before: dataToCursor({ id: 4 }),
607+
last: 2,
608+
},
609+
});
610+
expect(result.edges).toHaveLength(2);
611+
expect(result.edges[0].node.id).toBe(2);
612+
expect(result.edges[1].node.id).toBe(3);
613+
});
601614
});
602615

603616
describe('how works filter argument with resolve', () => {

src/connectionResolver.js

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -187,18 +187,30 @@ export function prepareConnectionResolver<TSource, TContext>(
187187
findManyParams.projection = { ...projection };
188188
}
189189

190+
// Apply the rawQuery to the count to get accurate results with last and before
191+
const sortConfig: ?ConnectionSortOpts = findSortConfig(opts.sort, args.sort);
192+
if (sortConfig) {
193+
prepareRawQuery(resolveParams, sortConfig);
194+
}
195+
190196
if (!first && last) {
191-
first = await countPromise;
197+
// Get the number of edges targeted by the findMany resolver (not the whole count)
198+
const filteredCountParams: $Shape<ResolveParams<TSource, TContext>> = {
199+
...resolveParams,
200+
args: {
201+
filter: { ...resolveParams.args.filter },
202+
},
203+
};
204+
205+
first = await countResolve(filteredCountParams);
192206
first = parseInt(first, 10) || 0;
193207
}
194208

195209
let limit = last || first || 20;
196210
let skip = last > 0 ? first - last : 0;
197211

198212
let prepareCursorData;
199-
const sortConfig: ?ConnectionSortOpts = findSortConfig(opts.sort, args.sort);
200213
if (sortConfig) {
201-
prepareRawQuery(resolveParams, sortConfig);
202214
findManyParams.rawQuery = resolveParams.rawQuery;
203215
sortConfig.cursorFields.forEach(fieldName => {
204216
findManyParams.projection[fieldName] = true;

0 commit comments

Comments
 (0)