fix: virtual relationship fields with select (#12266)
Continuation of https://github.com/payloadcms/payload/pull/12265. Currently, using `select` on new relationship virtual fields: ``` const doc = await payload.findByID({ collection: 'virtual-relations', depth: 0, id, select: { postTitle: true }, }) ``` doesn't work, because in order to calculate `post.title`, the `post` field must be selected as well. This PR adds logic that sanitizes the incoming `select` to include those relationships into `select` (that are related to selected virtual fields) --------- Co-authored-by: Dan Ribbens <dan.ribbens@gmail.com>
This commit is contained in:
@@ -1997,6 +1997,23 @@ describe('database', () => {
|
||||
expect(draft.docs[0]?.postTitle).toBe('my-title')
|
||||
})
|
||||
|
||||
it('should not break when using select', async () => {
|
||||
const post = await payload.create({ collection: 'posts', data: { title: 'my-title-10' } })
|
||||
const { id } = await payload.create({
|
||||
collection: 'virtual-relations',
|
||||
depth: 0,
|
||||
data: { post: post.id },
|
||||
})
|
||||
|
||||
const doc = await payload.findByID({
|
||||
collection: 'virtual-relations',
|
||||
depth: 0,
|
||||
id,
|
||||
select: { postTitle: true },
|
||||
})
|
||||
expect(doc.postTitle).toBe('my-title-10')
|
||||
})
|
||||
|
||||
it('should allow virtual field as reference to ID', async () => {
|
||||
const post = await payload.create({ collection: 'posts', data: { title: 'my-title' } })
|
||||
const { id } = await payload.create({
|
||||
@@ -2129,6 +2146,26 @@ describe('database', () => {
|
||||
expect(doc.postCategoryTitle).toBe('1-category')
|
||||
})
|
||||
|
||||
it('should not break when using select 2x deep', async () => {
|
||||
const category = await payload.create({
|
||||
collection: 'categories',
|
||||
data: { title: '3-category' },
|
||||
})
|
||||
const post = await payload.create({
|
||||
collection: 'posts',
|
||||
data: { title: '3-post', category: category.id },
|
||||
})
|
||||
const doc = await payload.create({ collection: 'virtual-relations', data: { post: post.id } })
|
||||
|
||||
const docWithSelect = await payload.findByID({
|
||||
collection: 'virtual-relations',
|
||||
depth: 0,
|
||||
id: doc.id,
|
||||
select: { postCategoryTitle: true },
|
||||
})
|
||||
expect(docWithSelect.postCategoryTitle).toBe('3-category')
|
||||
})
|
||||
|
||||
it('should allow to query by virtual field 2x deep', async () => {
|
||||
const category = await payload.create({
|
||||
collection: 'categories',
|
||||
|
||||
Reference in New Issue
Block a user