Skip to content

Commit

Permalink
feat: make fields configurable for determining whether or not to fetc…
Browse files Browse the repository at this point in the history
…h edges (#614)

* docs: fix link to types file

* feat: make fields configurable for determining whether or not to fetch edges

* Add changeset
  • Loading branch information
mikeroelens authored May 29, 2024
1 parent 0750527 commit ce9b19c
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changeset/moody-apricots-wonder.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@slonik/dataloaders": minor
---

feat: make fields configurable for determining whether or not to fetch edges
Original file line number Diff line number Diff line change
Expand Up @@ -456,7 +456,7 @@ describe('createConnectionLoaderClass', () => {
expect(results[1].count).toEqual(2);
});

it('gets the edges without fetching edges', async () => {
it('gets the edges without fetching count', async () => {
const loader = new PersonConnectionLoader(pool);
const results = await Promise.all([
loader.load({
Expand All @@ -474,6 +474,38 @@ describe('createConnectionLoaderClass', () => {
expect(results[1].count).toEqual(0);
expect(results[1].edges.length).toEqual(2);
});

it('fetches edges for fields provided in resolverFieldsThatRequireFetchingEdges config variable', async () => {
const loaderClass = createConnectionLoaderClass({
query: sql.type(
z
.object({
id: z.number(),
name: z.string(),
uid: z.string(),
})
.strict(),
)`
SELECT
id,
uid,
name
FROM person
`,
resolverFieldsThatRequireFetchingEdges: ['data'],
});

const loader = new loaderClass(pool);
const result = await loader.load({
info: getInfo(['data']),
});

expect(countTaggedQueries('@count-query')).toEqual(0);
expect(countTaggedQueries('@edges-query')).toEqual(1);

expect(result.count).toEqual(0);
expect(result.edges.length).toEqual(10);
});
});

describe('createConnectionLoaderClass (with validation)', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,13 @@ const TABLE_ALIAS = 't1';
export const createConnectionLoaderClass = <T extends ZodTypeAny>(config: {
columnNameTransformer?: (column: string) => string;
query: QuerySqlToken<T>;
resolverFieldsThatRequireFetchingEdges?: string[];
}) => {
const { columnNameTransformer = snakeCase, query } = config;

const fieldsThatRequireFetchingEdges =
config.resolverFieldsThatRequireFetchingEdges ?? ['pageInfo', 'edges'];

const columnIdentifiers = getColumnIdentifiers<z.infer<T>>(
TABLE_ALIAS,
columnNameTransformer,
Expand Down Expand Up @@ -105,10 +109,11 @@ export const createConnectionLoaderClass = <T extends ZodTypeAny>(config: {
countQueries.push(null);
}

if (
requestedFields.has('pageInfo') ||
requestedFields.has('edges')
) {
const shouldFetchEdges = fieldsThatRequireFetchingEdges.some(
(field) => requestedFields.has(field),
);

if (shouldFetchEdges) {
const orderByExpressions: Array<[SqlToken, OrderDirection]> =
orderBy ? orderBy(columnIdentifiers) : [];

Expand Down

0 comments on commit ce9b19c

Please sign in to comment.