feat(graphql): allow to pass count: true to a join query (#13351)
Fixes https://github.com/payloadcms/payload/issues/13077
This commit is contained in:
@@ -296,11 +296,16 @@ query {
|
||||
sort: "createdAt"
|
||||
limit: 5
|
||||
where: { author: { equals: "66e3431a3f23e684075aaeb9" } }
|
||||
"""
|
||||
Optionally pass count: true if you want to retrieve totalDocs
|
||||
"""
|
||||
count: true -- s
|
||||
) {
|
||||
docs {
|
||||
title
|
||||
}
|
||||
hasNextPage
|
||||
totalDocs
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -379,9 +379,11 @@ export const fieldToSchemaMap: FieldToSchemaMap = {
|
||||
),
|
||||
},
|
||||
hasNextPage: { type: new GraphQLNonNull(GraphQLBoolean) },
|
||||
totalDocs: { type: GraphQLInt },
|
||||
},
|
||||
}),
|
||||
args: {
|
||||
count: { type: GraphQLBoolean },
|
||||
limit: {
|
||||
type: GraphQLInt,
|
||||
},
|
||||
@@ -402,7 +404,7 @@ export const fieldToSchemaMap: FieldToSchemaMap = {
|
||||
},
|
||||
async resolve(parent, args, context: Context) {
|
||||
const { collection } = field
|
||||
const { limit, page, sort, where } = args
|
||||
const { count = false, limit, page, sort, where } = args
|
||||
const { req } = context
|
||||
|
||||
const draft = Boolean(args.draft ?? context.req.query?.draft)
|
||||
@@ -429,7 +431,7 @@ export const fieldToSchemaMap: FieldToSchemaMap = {
|
||||
throw new Error('GraphQL with array of join.field.collection is not implemented')
|
||||
}
|
||||
|
||||
const { docs } = await req.payload.find({
|
||||
const { docs, totalDocs } = await req.payload.find({
|
||||
collection,
|
||||
depth: 0,
|
||||
draft,
|
||||
@@ -439,7 +441,7 @@ export const fieldToSchemaMap: FieldToSchemaMap = {
|
||||
locale: req.locale,
|
||||
overrideAccess: false,
|
||||
page,
|
||||
pagination: false,
|
||||
pagination: count ? true : false,
|
||||
req,
|
||||
sort,
|
||||
where: fullWhere,
|
||||
@@ -454,6 +456,7 @@ export const fieldToSchemaMap: FieldToSchemaMap = {
|
||||
return {
|
||||
docs: shouldSlice ? docs.slice(0, -1) : docs,
|
||||
hasNextPage: limit === 0 ? false : limit < docs.length,
|
||||
...(count ? { totalDocs } : {}),
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
@@ -969,6 +969,37 @@ describe('Joins Field', () => {
|
||||
expect(unlimited.data.Categories.docs[0].relatedPosts.hasNextPage).toStrictEqual(false)
|
||||
})
|
||||
|
||||
it('should return totalDocs with count: true', async () => {
|
||||
const queryWithLimit = `query {
|
||||
Categories(where: {
|
||||
name: { equals: "paginate example" }
|
||||
}) {
|
||||
docs {
|
||||
relatedPosts(
|
||||
sort: "createdAt",
|
||||
limit: 4,
|
||||
count: true
|
||||
) {
|
||||
docs {
|
||||
title
|
||||
}
|
||||
hasNextPage
|
||||
totalDocs
|
||||
}
|
||||
}
|
||||
}
|
||||
}`
|
||||
const pageWithLimit = await restClient
|
||||
.GRAPHQL_POST({ body: JSON.stringify({ query: queryWithLimit }) })
|
||||
.then((res) => res.json())
|
||||
expect(pageWithLimit.data.Categories.docs[0].relatedPosts.docs).toHaveLength(4)
|
||||
expect(pageWithLimit.data.Categories.docs[0].relatedPosts.docs[0].title).toStrictEqual(
|
||||
'test 0',
|
||||
)
|
||||
expect(pageWithLimit.data.Categories.docs[0].relatedPosts.hasNextPage).toStrictEqual(true)
|
||||
expect(pageWithLimit.data.Categories.docs[0].relatedPosts.totalDocs).toStrictEqual(15)
|
||||
})
|
||||
|
||||
it('should have simple paginate with page for joins', async () => {
|
||||
let queryWithLimit = `query {
|
||||
Categories(where: {
|
||||
|
||||
Reference in New Issue
Block a user