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

feat: add page query parameter for joins #10998

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions packages/db-mongodb/src/utilities/buildJoinAggregation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ export const buildJoinAggregation = async ({

const {
limit: limitJoin = join.field.defaultLimit ?? 10,
page,
sort: sortJoin = join.field.defaultSort || collectionConfig.defaultSort,
where: whereJoin,
} = joins?.[join.joinPath] || {}
Expand Down Expand Up @@ -95,6 +96,12 @@ export const buildJoinAggregation = async ({
},
]

if (page) {
pipeline.push({
$skip: (page - 1) * limitJoin,
})
}

if (limitJoin > 0) {
pipeline.push({
$limit: limitJoin + 1,
Expand Down
11 changes: 11 additions & 0 deletions packages/drizzle/src/find/traverseFields.ts
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,7 @@ export const traverseFields = ({

const {
limit: limitArg = field.defaultLimit ?? 10,
page,
sort = field.defaultSort,
where,
} = joinQuery[joinSchemaPath] || {}
Expand Down Expand Up @@ -428,6 +429,16 @@ export const traverseFields = ({
})
}

if (page && limit !== 0) {
const offset = (page - 1) * limit - 1
if (offset > 0) {
chainedMethods.push({
args: [offset],
method: 'offset',
})
}
}

const db = adapter.drizzle as LibSQLDatabase

for (let key in selectFields) {
Expand Down
6 changes: 5 additions & 1 deletion packages/graphql/src/schema/buildObjectType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,9 @@ export function buildObjectType({
limit: {
type: GraphQLInt,
},
page: {
type: GraphQLInt,
},
sort: {
type: GraphQLString,
},
Expand All @@ -251,7 +254,7 @@ export function buildObjectType({
},
async resolve(parent, args, context: Context) {
const { collection } = field
const { limit, sort, where } = args
const { limit, page, sort, where } = args
const { req } = context

const fullWhere = combineQueries(where, {
Expand All @@ -265,6 +268,7 @@ export function buildObjectType({
limit,
locale: req.locale,
overrideAccess: false,
page,
req,
sort,
where: fullWhere,
Expand Down
1 change: 0 additions & 1 deletion packages/payload/src/database/sanitizeJoinQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import type { JoinQuery, PayloadRequest } from '../types/index.js'

import executeAccess from '../auth/executeAccess.js'
import { QueryError } from '../errors/QueryError.js'
import { deepCopyObjectSimple } from '../utilities/deepCopyObject.js'
import { combineQueries } from './combineQueries.js'
import { validateQueryPaths } from './queryValidation/validateQueryPaths.js'

Expand Down
1 change: 1 addition & 0 deletions packages/payload/src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ export type JoinQuery<TSlug extends CollectionSlug = string> =
[K in keyof TypedCollectionJoins[TSlug]]:
| {
limit?: number
page?: number
sort?: string
where?: Where
}
Expand Down
1 change: 1 addition & 0 deletions packages/payload/src/utilities/sanitizeJoinParams.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export const sanitizeJoinParams = (
} else {
joinQuery[schemaPath] = {
limit: isNumber(joins[schemaPath]?.limit) ? Number(joins[schemaPath].limit) : undefined,
page: isNumber(joins[schemaPath]?.page) ? Number(joins[schemaPath].page) : undefined,
sort: joins[schemaPath]?.sort ? joins[schemaPath].sort : undefined,
where: joins[schemaPath]?.where ? joins[schemaPath].where : undefined,
}
Expand Down
128 changes: 128 additions & 0 deletions test/joins/int.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -617,6 +617,46 @@ describe('Joins Field', () => {
expect(unlimited.docs[0].relatedPosts.hasNextPage).toStrictEqual(false)
})

it('should have simple paginate with page for joins', async () => {
const query = {
depth: 1,
where: {
name: { equals: 'paginate example' },
},
joins: {
relatedPosts: {
sort: 'createdAt',
limit: 2,
page: 1,
},
},
}
let pageWithLimit = await restClient.GET(`/categories`, { query }).then((res) => res.json())

query.joins.relatedPosts.limit = 0
const unlimited = await restClient.GET(`/categories`, { query }).then((res) => res.json())

expect(pageWithLimit.docs[0].relatedPosts.docs).toHaveLength(2)
expect(pageWithLimit.docs[0].relatedPosts.docs[0].id).toBe(
unlimited.docs[0].relatedPosts.docs[0].id,
)
expect(pageWithLimit.docs[0].relatedPosts.docs[1].id).toBe(
unlimited.docs[0].relatedPosts.docs[1].id,
)
query.joins.relatedPosts.limit = 2
query.joins.relatedPosts.page = 2

pageWithLimit = await restClient.GET(`/categories`, { query }).then((res) => res.json())

expect(pageWithLimit.docs[0].relatedPosts.docs).toHaveLength(2)
expect(pageWithLimit.docs[0].relatedPosts.docs[0].id).toBe(
unlimited.docs[0].relatedPosts.docs[2].id,
)
expect(pageWithLimit.docs[0].relatedPosts.docs[1].id).toBe(
unlimited.docs[0].relatedPosts.docs[3].id,
)
})

it('should respect access control for join collections', async () => {
const { docs } = await payload.find({
collection: categoriesJoinRestrictedSlug,
Expand Down Expand Up @@ -749,6 +789,94 @@ describe('Joins Field', () => {
expect(unlimited.data.Categories.docs[0].relatedPosts.hasNextPage).toStrictEqual(false)
})

it('should have simple paginate with page for joins', async () => {
let queryWithLimit = `query {
Categories(where: {
name: { equals: "paginate example" }
}) {
docs {
relatedPosts(
sort: "createdAt",
limit: 2
) {
docs {
title
}
hasNextPage
}
}
}
}`
let pageWithLimit = await restClient
.GRAPHQL_POST({ body: JSON.stringify({ query: queryWithLimit }) })
.then((res) => res.json())

const queryUnlimited = `query {
Categories(
where: {
name: { equals: "paginate example" }
}
) {
docs {
relatedPosts(
sort: "createdAt",
limit: 0
) {
docs {
title
createdAt
}
hasNextPage
}
}
}
}`

const unlimited = await restClient
.GRAPHQL_POST({ body: JSON.stringify({ query: queryUnlimited }) })
.then((res) => res.json())

expect(pageWithLimit.data.Categories.docs[0].relatedPosts.docs).toHaveLength(2)
expect(pageWithLimit.data.Categories.docs[0].relatedPosts.docs[0].id).toStrictEqual(
unlimited.data.Categories.docs[0].relatedPosts.docs[0].id,
)
expect(pageWithLimit.data.Categories.docs[0].relatedPosts.docs[1].id).toStrictEqual(
unlimited.data.Categories.docs[0].relatedPosts.docs[1].id,
)

expect(pageWithLimit.data.Categories.docs[0].relatedPosts.hasNextPage).toStrictEqual(true)

queryWithLimit = `query {
Categories(where: {
name: { equals: "paginate example" }
}) {
docs {
relatedPosts(
sort: "createdAt",
limit: 2,
page: 2,
) {
docs {
title
}
hasNextPage
}
}
}
}`

pageWithLimit = await restClient
.GRAPHQL_POST({ body: JSON.stringify({ query: queryWithLimit }) })
.then((res) => res.json())

expect(pageWithLimit.data.Categories.docs[0].relatedPosts.docs[0].id).toStrictEqual(
unlimited.data.Categories.docs[0].relatedPosts.docs[2].id,
)
expect(pageWithLimit.data.Categories.docs[0].relatedPosts.docs[1].id).toStrictEqual(
unlimited.data.Categories.docs[0].relatedPosts.docs[3].id,
)
})

it('should have simple paginate for joins inside groups', async () => {
const queryWithLimit = `query {
Categories(where: {
Expand Down