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

Prevent executing connection resolver when only total count is selected #1025

Merged
merged 5 commits into from
Sep 22, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 13 additions & 3 deletions packages/plugin-prisma/src/field-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,13 @@ fieldBuilderProto.prismaConnection = function prismaConnection<
withUsageCheck: !!this.builder.options.prisma?.onUnusedQuery,
});

const selection = info.fieldNodes[0];
const totalCountOnly =
selection.selectionSet?.selections.length === 1 &&
selection.selectionSet.selections.every(
(s) => 'name' in s && s.name.value === 'totalCount',
);
hayes marked this conversation as resolved.
Show resolved Hide resolved

return resolvePrismaCursorConnection(
{
parent,
Expand All @@ -157,13 +164,16 @@ fieldBuilderProto.prismaConnection = function prismaConnection<
totalCount: totalCount && (() => totalCount(parent, args as never, context, info)),
},
formatCursor,
(q) =>
checkIfQueryIsUsed(
(q) => {
if (totalCountOnly) return [];

return checkIfQueryIsUsed(
this.builder,
query,
info,
resolve(q as never, parent, args as never, context, info) as never,
),
);
},
);
},
},
Expand Down
48 changes: 38 additions & 10 deletions packages/plugin-prisma/tests/counts.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,16 +143,6 @@ describe('prisma counts', () => {
"model": "User",
"runInTransaction": false,
},
{
"action": "findMany",
"args": {
"skip": 0,
"take": 2,
},
"dataPath": [],
"model": "User",
"runInTransaction": false,
},
{
"action": "count",
"args": undefined,
Expand Down Expand Up @@ -237,6 +227,44 @@ describe('prisma counts', () => {
`);
});

it('queries only totalCount on connection', async () => {
const query = gql`
query {
userConnection {
totalCount
}
}
`;

const result = await execute({
schema,
document: query,
contextValue: { user: { id: 1 } },
});

expect(result).toMatchInlineSnapshot(`
{
"data": {
"userConnection": {
"totalCount": 100,
},
},
}
`);

expect(queries).toMatchInlineSnapshot(`
[
{
"action": "count",
"args": undefined,
"dataPath": [],
"model": "User",
"runInTransaction": false,
},
]
`);
});

it('connection totalCount in fragment', async () => {
const query = gql`
query {
Expand Down