Skip to content

Commit f432cc1

Browse files
authored
feat(graphql): allow to pass count: true to a join query (#13351)
Fixes #13077
1 parent 2903486 commit f432cc1

File tree

3 files changed

+42
-3
lines changed

3 files changed

+42
-3
lines changed

docs/fields/join.mdx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,11 +296,16 @@ query {
296296
sort: "createdAt"
297297
limit: 5
298298
where: { author: { equals: "66e3431a3f23e684075aaeb9" } }
299+
"""
300+
Optionally pass count: true if you want to retrieve totalDocs
301+
"""
302+
count: true -- s
299303
) {
300304
docs {
301305
title
302306
}
303307
hasNextPage
308+
totalDocs
304309
}
305310
}
306311
}

packages/graphql/src/schema/fieldToSchemaMap.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -379,9 +379,11 @@ export const fieldToSchemaMap: FieldToSchemaMap = {
379379
),
380380
},
381381
hasNextPage: { type: new GraphQLNonNull(GraphQLBoolean) },
382+
totalDocs: { type: GraphQLInt },
382383
},
383384
}),
384385
args: {
386+
count: { type: GraphQLBoolean },
385387
limit: {
386388
type: GraphQLInt,
387389
},
@@ -402,7 +404,7 @@ export const fieldToSchemaMap: FieldToSchemaMap = {
402404
},
403405
async resolve(parent, args, context: Context) {
404406
const { collection } = field
405-
const { limit, page, sort, where } = args
407+
const { count = false, limit, page, sort, where } = args
406408
const { req } = context
407409

408410
const draft = Boolean(args.draft ?? context.req.query?.draft)
@@ -429,7 +431,7 @@ export const fieldToSchemaMap: FieldToSchemaMap = {
429431
throw new Error('GraphQL with array of join.field.collection is not implemented')
430432
}
431433

432-
const { docs } = await req.payload.find({
434+
const { docs, totalDocs } = await req.payload.find({
433435
collection,
434436
depth: 0,
435437
draft,
@@ -439,7 +441,7 @@ export const fieldToSchemaMap: FieldToSchemaMap = {
439441
locale: req.locale,
440442
overrideAccess: false,
441443
page,
442-
pagination: false,
444+
pagination: count ? true : false,
443445
req,
444446
sort,
445447
where: fullWhere,
@@ -454,6 +456,7 @@ export const fieldToSchemaMap: FieldToSchemaMap = {
454456
return {
455457
docs: shouldSlice ? docs.slice(0, -1) : docs,
456458
hasNextPage: limit === 0 ? false : limit < docs.length,
459+
...(count ? { totalDocs } : {}),
457460
}
458461
},
459462
}

test/joins/int.spec.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -969,6 +969,37 @@ describe('Joins Field', () => {
969969
expect(unlimited.data.Categories.docs[0].relatedPosts.hasNextPage).toStrictEqual(false)
970970
})
971971

972+
it('should return totalDocs with count: true', async () => {
973+
const queryWithLimit = `query {
974+
Categories(where: {
975+
name: { equals: "paginate example" }
976+
}) {
977+
docs {
978+
relatedPosts(
979+
sort: "createdAt",
980+
limit: 4,
981+
count: true
982+
) {
983+
docs {
984+
title
985+
}
986+
hasNextPage
987+
totalDocs
988+
}
989+
}
990+
}
991+
}`
992+
const pageWithLimit = await restClient
993+
.GRAPHQL_POST({ body: JSON.stringify({ query: queryWithLimit }) })
994+
.then((res) => res.json())
995+
expect(pageWithLimit.data.Categories.docs[0].relatedPosts.docs).toHaveLength(4)
996+
expect(pageWithLimit.data.Categories.docs[0].relatedPosts.docs[0].title).toStrictEqual(
997+
'test 0',
998+
)
999+
expect(pageWithLimit.data.Categories.docs[0].relatedPosts.hasNextPage).toStrictEqual(true)
1000+
expect(pageWithLimit.data.Categories.docs[0].relatedPosts.totalDocs).toStrictEqual(15)
1001+
})
1002+
9721003
it('should have simple paginate with page for joins', async () => {
9731004
let queryWithLimit = `query {
9741005
Categories(where: {

0 commit comments

Comments
 (0)