From b101feca7a83063db6f2a48f70f35df184f42d29 Mon Sep 17 00:00:00 2001 From: Sasha <64744993+r1tsuu@users.noreply.github.com> Date: Fri, 13 Dec 2024 16:23:43 +0200 Subject: [PATCH] feat: expose `pagination: false` to REST / GraphQL (#9952) Exposes `pagination: false` to REST / GraphQL to improve performance on large collections by avoiding count query. This will also be nice for our SDK https://github.com/payloadcms/payload/pull/9463 to have the same properties. --- .../graphql/src/resolvers/collections/find.ts | 2 ++ .../src/resolvers/collections/findVersions.ts | 2 ++ .../src/resolvers/globals/findVersions.ts | 2 ++ .../graphql/src/schema/initCollections.ts | 2 ++ packages/graphql/src/schema/initGlobals.ts | 1 + .../next/src/routes/rest/collections/find.ts | 25 +++++++++++-------- .../routes/rest/collections/findVersions.ts | 4 ++- .../src/routes/rest/globals/findVersions.ts | 4 ++- 8 files changed, 29 insertions(+), 13 deletions(-) diff --git a/packages/graphql/src/resolvers/collections/find.ts b/packages/graphql/src/resolvers/collections/find.ts index 0229b2a4f6f..3285e16c76b 100644 --- a/packages/graphql/src/resolvers/collections/find.ts +++ b/packages/graphql/src/resolvers/collections/find.ts @@ -13,6 +13,7 @@ export type Resolver = ( limit?: number locale?: string page?: number + pagination?: boolean sort?: string where?: Where }, @@ -53,6 +54,7 @@ export function findResolver(collection: Collection): Resolver { draft: args.draft, limit: args.limit, page: args.page, + pagination: args.pagination, req, sort: args.sort, where: args.where, diff --git a/packages/graphql/src/resolvers/collections/findVersions.ts b/packages/graphql/src/resolvers/collections/findVersions.ts index 85ec8fd088c..c747bbdfdcd 100644 --- a/packages/graphql/src/resolvers/collections/findVersions.ts +++ b/packages/graphql/src/resolvers/collections/findVersions.ts @@ -12,6 +12,7 @@ export type Resolver = ( limit?: number locale?: string page?: number + pagination?: boolean sort?: string where: Where }, @@ -50,6 +51,7 @@ export function findVersionsResolver(collection: Collection): Resolver { depth: 0, limit: args.limit, page: args.page, + pagination: args.pagination, req: isolateObjectProperty(req, 'transactionID'), sort: args.sort, where: args.where, diff --git a/packages/graphql/src/resolvers/globals/findVersions.ts b/packages/graphql/src/resolvers/globals/findVersions.ts index 500fe16c7db..1401a4f1d44 100644 --- a/packages/graphql/src/resolvers/globals/findVersions.ts +++ b/packages/graphql/src/resolvers/globals/findVersions.ts @@ -11,6 +11,7 @@ export type Resolver = ( limit?: number locale?: string page?: number + pagination?: boolean sort?: string where: Where }, @@ -26,6 +27,7 @@ export function findVersions(globalConfig: SanitizedGlobalConfig): Resolver { globalConfig, limit: args.limit, page: args.page, + pagination: args.pagination, req: isolateObjectProperty(context.req, 'transactionID'), sort: args.sort, where: args.where, diff --git a/packages/graphql/src/schema/initCollections.ts b/packages/graphql/src/schema/initCollections.ts index 394f621b801..947a97e6dd7 100644 --- a/packages/graphql/src/schema/initCollections.ts +++ b/packages/graphql/src/schema/initCollections.ts @@ -196,6 +196,7 @@ export function initCollections({ config, graphqlResult }: InitCollectionsGraphQ : {}), limit: { type: GraphQLInt }, page: { type: GraphQLInt }, + pagination: { type: GraphQLBoolean }, sort: { type: GraphQLString }, }, resolve: findResolver(collection), @@ -351,6 +352,7 @@ export function initCollections({ config, graphqlResult }: InitCollectionsGraphQ : {}), limit: { type: GraphQLInt }, page: { type: GraphQLInt }, + pagination: { type: GraphQLBoolean }, sort: { type: GraphQLString }, }, resolve: findVersionsResolver(collection), diff --git a/packages/graphql/src/schema/initGlobals.ts b/packages/graphql/src/schema/initGlobals.ts index d11bc7a3fb4..46a7e3d8615 100644 --- a/packages/graphql/src/schema/initGlobals.ts +++ b/packages/graphql/src/schema/initGlobals.ts @@ -166,6 +166,7 @@ export function initGlobals({ config, graphqlResult }: InitGlobalsGraphQLArgs): : {}), limit: { type: GraphQLInt }, page: { type: GraphQLInt }, + pagination: { type: GraphQLBoolean }, sort: { type: GraphQLString }, }, resolve: findVersions(global), diff --git a/packages/next/src/routes/rest/collections/find.ts b/packages/next/src/routes/rest/collections/find.ts index e0ba92a0f9b..eba702d9432 100644 --- a/packages/next/src/routes/rest/collections/find.ts +++ b/packages/next/src/routes/rest/collections/find.ts @@ -13,17 +13,19 @@ import type { CollectionRouteHandler } from '../types.js' import { headersWithCors } from '../../../utilities/headersWithCors.js' export const find: CollectionRouteHandler = async ({ collection, req }) => { - const { depth, draft, joins, limit, page, populate, select, sort, where } = req.query as { - depth?: string - draft?: string - joins?: JoinQuery - limit?: string - page?: string - populate?: Record - select?: Record - sort?: string - where?: Where - } + const { depth, draft, joins, limit, page, pagination, populate, select, sort, where } = + req.query as { + depth?: string + draft?: string + joins?: JoinQuery + limit?: string + page?: string + pagination?: string + populate?: Record + select?: Record + sort?: string + where?: Where + } const result = await findOperation({ collection, @@ -32,6 +34,7 @@ export const find: CollectionRouteHandler = async ({ collection, req }) => { joins: sanitizeJoinParams(joins), limit: isNumber(limit) ? Number(limit) : undefined, page: isNumber(page) ? Number(page) : undefined, + pagination: pagination === 'false' ? false : undefined, populate: sanitizePopulateParam(populate), req, select: sanitizeSelectParam(select), diff --git a/packages/next/src/routes/rest/collections/findVersions.ts b/packages/next/src/routes/rest/collections/findVersions.ts index d029ce72785..f9dacbb2b98 100644 --- a/packages/next/src/routes/rest/collections/findVersions.ts +++ b/packages/next/src/routes/rest/collections/findVersions.ts @@ -9,10 +9,11 @@ import type { CollectionRouteHandler } from '../types.js' import { headersWithCors } from '../../../utilities/headersWithCors.js' export const findVersions: CollectionRouteHandler = async ({ collection, req }) => { - const { depth, limit, page, populate, select, sort, where } = req.query as { + const { depth, limit, page, pagination, populate, select, sort, where } = req.query as { depth?: string limit?: string page?: string + pagination?: string populate?: Record select?: Record sort?: string @@ -24,6 +25,7 @@ export const findVersions: CollectionRouteHandler = async ({ collection, req }) depth: isNumber(depth) ? Number(depth) : undefined, limit: isNumber(limit) ? Number(limit) : undefined, page: isNumber(page) ? Number(page) : undefined, + pagination: pagination === 'false' ? false : undefined, populate: sanitizePopulateParam(populate), req, select: sanitizeSelectParam(select), diff --git a/packages/next/src/routes/rest/globals/findVersions.ts b/packages/next/src/routes/rest/globals/findVersions.ts index 5cf888b7dd1..2f49a2699be 100644 --- a/packages/next/src/routes/rest/globals/findVersions.ts +++ b/packages/next/src/routes/rest/globals/findVersions.ts @@ -9,10 +9,11 @@ import type { GlobalRouteHandler } from '../types.js' import { headersWithCors } from '../../../utilities/headersWithCors.js' export const findVersions: GlobalRouteHandler = async ({ globalConfig, req }) => { - const { depth, limit, page, populate, select, sort, where } = req.query as { + const { depth, limit, page, pagination, populate, select, sort, where } = req.query as { depth?: string limit?: string page?: string + pagination?: string populate?: Record select?: Record sort?: string @@ -24,6 +25,7 @@ export const findVersions: GlobalRouteHandler = async ({ globalConfig, req }) => globalConfig, limit: isNumber(limit) ? Number(limit) : undefined, page: isNumber(page) ? Number(page) : undefined, + pagination: pagination === 'false' ? false : undefined, populate: sanitizePopulateParam(populate), req, select: sanitizeSelectParam(select),