diff --git a/test/database/config.ts b/test/database/config.ts index 5e17d3025..76230f516 100644 --- a/test/database/config.ts +++ b/test/database/config.ts @@ -460,6 +460,7 @@ export default buildConfigWithDefaults({ { slug: 'virtual-relations', admin: { useAsTitle: 'postTitle' }, + access: { read: () => true }, fields: [ { name: 'postTitle', diff --git a/test/database/int.spec.ts b/test/database/int.spec.ts index c903c03b2..1ff38715d 100644 --- a/test/database/int.spec.ts +++ b/test/database/int.spec.ts @@ -2151,8 +2151,6 @@ describe('database', () => { expect(descDocs[0]?.id).toBe(doc_2.id) }) - it.todo('should allow to sort by a virtual field with reference') - it('should allow virtual field 2x deep', async () => { const category = await payload.create({ collection: 'categories', @@ -2213,6 +2211,77 @@ describe('database', () => { }) expect(globalData.postTitle).toBe('post') }) + + it('should allow to sort by a virtual field with a refence, Local / GraphQL', async () => { + const post_1 = await payload.create({ collection: 'posts', data: { title: 'A' } }) + const post_2 = await payload.create({ collection: 'posts', data: { title: 'B' } }) + const doc_1 = await payload.create({ + collection: 'virtual-relations', + data: { post: post_1 }, + }) + const doc_2 = await payload.create({ + collection: 'virtual-relations', + data: { post: post_2 }, + }) + + const queryDesc = `query { + VirtualRelations( + where: {OR: [{ id: { equals: ${JSON.stringify(doc_1.id)} } }, { id: { equals: ${JSON.stringify(doc_2.id)} } }], + }, sort: "-postTitle") { + docs { + id + } + } + }` + + const { + data: { + VirtualRelations: { docs: graphqlDesc }, + }, + } = await restClient + .GRAPHQL_POST({ body: JSON.stringify({ query: queryDesc }) }) + .then((res) => res.json()) + + const { docs: localDesc } = await payload.find({ + collection: 'virtual-relations', + sort: '-postTitle', + where: { id: { in: [doc_1.id, doc_2.id] } }, + }) + + expect(graphqlDesc[0].id).toBe(doc_2.id) + expect(graphqlDesc[1].id).toBe(doc_1.id) + expect(localDesc[0].id).toBe(doc_2.id) + expect(localDesc[1].id).toBe(doc_1.id) + + const queryAsc = `query { + VirtualRelations( + where: {OR: [{ id: { equals: ${JSON.stringify(doc_1.id)} } }, { id: { equals: ${JSON.stringify(doc_2.id)} } }], + }, sort: "postTitle") { + docs { + id + } + } + }` + + const { + data: { + VirtualRelations: { docs: graphqlAsc }, + }, + } = await restClient + .GRAPHQL_POST({ body: JSON.stringify({ query: queryAsc }) }) + .then((res) => res.json()) + + const { docs: localAsc } = await payload.find({ + collection: 'virtual-relations', + sort: 'postTitle', + where: { id: { in: [doc_1.id, doc_2.id] } }, + }) + + expect(graphqlAsc[1].id).toBe(doc_2.id) + expect(graphqlAsc[0].id).toBe(doc_1.id) + expect(localAsc[1].id).toBe(doc_2.id) + expect(localAsc[0].id).toBe(doc_1.id) + }) }) it('should convert numbers to text', async () => {