Skip to content

Commit 3b0c208

Browse files
DanRibbenskendelljoseph
authored andcommitted
perf(graphql): skip count query for join field using simple pagination (#12223)
GraphQL requests with join fields result in a lot of extra count rows queries that aren't necessary. This turns off pagination and uses limit+1 and slice instead.
1 parent 4dd91ef commit 3b0c208

File tree

1 file changed

+15
-2
lines changed

1 file changed

+15
-2
lines changed

packages/graphql/src/schema/fieldToSchemaMap.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -393,19 +393,32 @@ export const fieldToSchemaMap: FieldToSchemaMap = {
393393
throw new Error('GraphQL with array of join.field.collection is not implemented')
394394
}
395395

396-
return await req.payload.find({
396+
const { docs } = await req.payload.find({
397397
collection,
398398
depth: 0,
399399
draft,
400400
fallbackLocale: req.fallbackLocale,
401-
limit,
401+
// Fetch one extra document to determine if there are more documents beyond the requested limit (used for hasNextPage calculation).
402+
limit: typeof limit === 'number' && limit > 0 ? limit + 1 : 0,
402403
locale: req.locale,
403404
overrideAccess: false,
404405
page,
406+
pagination: false,
405407
req,
406408
sort,
407409
where: fullWhere,
408410
})
411+
412+
let shouldSlice = false
413+
414+
if (typeof limit === 'number' && limit !== 0 && limit < docs.length) {
415+
shouldSlice = true
416+
}
417+
418+
return {
419+
docs: shouldSlice ? docs.slice(0, -1) : docs,
420+
hasNextPage: limit === 0 ? false : limit < docs.length,
421+
}
409422
},
410423
}
411424

0 commit comments

Comments
 (0)