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.
This commit is contained in:
@@ -393,19 +393,32 @@ 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,
|
||||
// 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,
|
||||
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,
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user