From 0b648093c389b8d3c2da792ab63c355a0574d1eb Mon Sep 17 00:00:00 2001 From: Dan Ribbens Date: Fri, 25 Apr 2025 11:46:37 -0400 Subject: [PATCH 1/2] perf(graphql): skip count query for join field using simple pagination --- packages/graphql/src/schema/fieldToSchemaMap.ts | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/packages/graphql/src/schema/fieldToSchemaMap.ts b/packages/graphql/src/schema/fieldToSchemaMap.ts index fefd5983936..526b9d8544e 100644 --- a/packages/graphql/src/schema/fieldToSchemaMap.ts +++ b/packages/graphql/src/schema/fieldToSchemaMap.ts @@ -393,19 +393,31 @@ export const fieldToSchemaMap: FieldToSchemaMap = { throw new Error('GraphQL with array of join.field.collection is not implemented') } - return await req.payload.find({ + const { docs } = await req.payload.find({ collection, depth: 0, draft, fallbackLocale: req.fallbackLocale, - limit, + limit: typeof limit === 'number' && limit > 0 ? limit + 1 : 0, locale: req.locale, overrideAccess: false, page, + pagination: false, req, sort, where: fullWhere, }) + + let shouldSlice = false + + if (typeof limit === 'number' && limit !== 0 && limit < docs.length) { + shouldSlice = true + } + + return { + docs: shouldSlice ? docs.slice(0, -1) : docs, + hasNextPage: limit === 0 ? false : limit < docs.length, + } }, } From a8d3301123fa5d80d5f1ef88845e056d201cde2e Mon Sep 17 00:00:00 2001 From: Dan Ribbens Date: Mon, 28 Apr 2025 22:23:33 -0400 Subject: [PATCH 2/2] chore: add comment Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- packages/graphql/src/schema/fieldToSchemaMap.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/graphql/src/schema/fieldToSchemaMap.ts b/packages/graphql/src/schema/fieldToSchemaMap.ts index 526b9d8544e..70ffc75abdc 100644 --- a/packages/graphql/src/schema/fieldToSchemaMap.ts +++ b/packages/graphql/src/schema/fieldToSchemaMap.ts @@ -398,6 +398,7 @@ export const fieldToSchemaMap: FieldToSchemaMap = { depth: 0, draft, fallbackLocale: req.fallbackLocale, + // Fetch one extra document to determine if there are more documents beyond the requested limit (used for hasNextPage calculation). limit: typeof limit === 'number' && limit > 0 ? limit + 1 : 0, locale: req.locale, overrideAccess: false,