Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Port slonik-dataloaders #599

Merged
merged 25 commits into from
May 13, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
add offset
  • Loading branch information
gajus committed May 13, 2024
commit 5ec9156b220f5c85292ba1c0e1feedae9bcbb53c
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,17 @@ describe('createConnectionLoaderClass', () => {
expect(getNodeIds(result.edges)).toEqual([1, 2, 3, 4]);
});

it('loads records with limit and offset', async () => {
const loader = new BarConnectionLoader(pool, {});
const result = await loader.load({
limit: 4,
offset: 4,
orderBy: ({ uid }) => [[uid, 'ASC']],
});

expect(getNodeIds(result.edges)).toEqual([5, 6, 7, 8]);
});

it('paginates through the records forwards', async () => {
const loader = new BarConnectionLoader(pool, {});
const firstResult = await loader.load({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ type DataLoaderKey<TResult> = {
cursor?: string | null;
info?: Pick<GraphQLResolveInfo, 'fieldNodes' | 'fragments'>;
limit?: number | null;
offset?: number | null;
orderBy?: (
identifiers: ColumnIdentifiers<TResult>,
) => Array<[SqlToken, OrderDirection]>;
Expand Down Expand Up @@ -65,6 +66,7 @@ export const createConnectionLoaderClass = <T extends ZodTypeAny>(config: {
cursor,
info,
limit,
offset,
orderBy,
reverse = false,
where,
Expand Down Expand Up @@ -183,6 +185,7 @@ export const createConnectionLoaderClass = <T extends ZodTypeAny>(config: {
WHERE ${whereExpression}
${orderByClause}
LIMIT ${limit ? limit + 1 : null}
OFFSET ${offset || 0}
)`,
);
}
Expand Down Expand Up @@ -293,6 +296,7 @@ export const createConnectionLoaderClass = <T extends ZodTypeAny>(config: {
cursor,
info,
limit,
offset,
orderBy,
reverse = false,
where,
Expand All @@ -301,7 +305,7 @@ export const createConnectionLoaderClass = <T extends ZodTypeAny>(config: {
? getRequestedFields(info)
: new Set(['pageInfo', 'edges']);

return `${cursor}|${reverse}|${limit}|${JSON.stringify(
return `${cursor}|${reverse}|${limit}|${offset}|${JSON.stringify(
orderBy?.(columnIdentifiers),
)}|${JSON.stringify(
where?.(columnIdentifiers),
Expand Down