fix(graphql): population of joins that target relationship fields that have relationTo as an array (#12289)

Fixes population of joins that target relationship fields that have
`relationTo` as an array, for example:
```ts
// Posts collection
{
  name: 'polymorphic',
  type: 'relationship',
  relationTo: ['categories', 'users'],
},
// Categories collection
{
  name: 'polymorphic',
  type: 'join',
  collection: 'posts',
  on: 'polymorphic',
}
```

Thanks @jaycetde for the integration test
https://github.com/payloadcms/payload/pull/12278!

---------

Co-authored-by: Jayce Pulsipher <jpulsipher@nav.com>
This commit is contained in:
Sasha
2025-05-01 21:04:42 +03:00
committed by GitHub
parent b9868c4a3b
commit c08c7071ee
5 changed files with 114 additions and 4 deletions

View File

@@ -940,6 +940,94 @@ describe('Joins Field', () => {
)
})
it('should have simple paginate with page for joins polymorphic', async () => {
let queryWithLimit = `query {
Categories(where: {
name: { equals: "paginate example" }
}) {
docs {
polymorphic(
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 {
polymorphic(
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].polymorphic.docs).toHaveLength(2)
expect(pageWithLimit.data.Categories.docs[0].polymorphic.docs[0].id).toStrictEqual(
unlimited.data.Categories.docs[0].polymorphic.docs[0].id,
)
expect(pageWithLimit.data.Categories.docs[0].polymorphic.docs[1].id).toStrictEqual(
unlimited.data.Categories.docs[0].polymorphic.docs[1].id,
)
expect(pageWithLimit.data.Categories.docs[0].polymorphic.hasNextPage).toStrictEqual(true)
queryWithLimit = `query {
Categories(where: {
name: { equals: "paginate example" }
}) {
docs {
polymorphic(
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].polymorphic.docs[0].id).toStrictEqual(
unlimited.data.Categories.docs[0].polymorphic.docs[2].id,
)
expect(pageWithLimit.data.Categories.docs[0].polymorphic.docs[1].id).toStrictEqual(
unlimited.data.Categories.docs[0].polymorphic.docs[3].id,
)
})
it('should populate joins with hasMany when on both sides documents are in draft', async () => {
const category = await payload.create({
collection: 'categories-versions',