diff --git a/src/fields/richText/populate.ts b/src/fields/richText/populate.ts index 82382da21..9fd4f5c02 100644 --- a/src/fields/richText/populate.ts +++ b/src/fields/richText/populate.ts @@ -31,16 +31,16 @@ export const populate = async ({ }): Promise => { const dataRef = data as Record; - const doc = await req.payload.findByID({ - req, - collection: collection.config.slug, + const doc = await req.payloadDataLoader.load(JSON.stringify([ + collection.config.slug, id, - currentDepth: currentDepth + 1, - overrideAccess: typeof overrideAccess === 'undefined' ? false : overrideAccess, - disableErrors: true, depth, + currentDepth + 1, + req.locale, + req.fallbackLocale, + typeof overrideAccess === 'undefined' ? false : overrideAccess, showHiddenFields, - }); + ])); if (doc) { dataRef[key] = doc; diff --git a/test/dataloader/config.ts b/test/dataloader/config.ts index 551a84b5a..74fe92219 100644 --- a/test/dataloader/config.ts +++ b/test/dataloader/config.ts @@ -24,6 +24,42 @@ export default buildConfig({ ], }, + { + slug: 'relation-a', + labels: { + singular: 'Relation A', + plural: 'Relation As', + }, + fields: [ + { + name: 'relationship', + type: 'relationship', + relationTo: 'relation-b', + }, + { + name: 'richText', + type: 'richText', + }, + ], + }, + { + slug: 'relation-b', + labels: { + singular: 'Relation B', + plural: 'Relation Bs', + }, + fields: [ + { + name: 'relationship', + type: 'relationship', + relationTo: 'relation-a', + }, + { + name: 'richText', + type: 'richText', + }, + ], + }, ], onInit: async (payload) => { const user = await payload.create({ diff --git a/test/dataloader/int.spec.ts b/test/dataloader/int.spec.ts index 32459eb88..7600ec509 100644 --- a/test/dataloader/int.spec.ts +++ b/test/dataloader/int.spec.ts @@ -49,5 +49,88 @@ describe('dataloader', () => { const { docs } = response.Posts; expect(docs[0].title).toStrictEqual(postDoc.title); }); + + it('should avoid infinite loops', async () => { + const relationA = await payload.create({ + collection: 'relation-a', + data: { + richText: [ + { + children: [ + { + text: 'relation a', + }, + ], + }, + ], + }, + }); + + const relationB = await payload.create({ + collection: 'relation-b', + data: { + relationship: relationA.id, + richText: [ + { + children: [ + { + text: 'relation b', + }, + ], + }, + ], + }, + }); + + expect(relationA.id).toBeDefined(); + expect(relationB.id).toBeDefined(); + + await payload.update({ + collection: 'relation-a', + id: relationA.id, + data: { + relationship: relationB.id, + richText: [ + { + children: [ + { + text: 'relation a', + }, + ], + }, + { + children: [ + { + text: '', + }, + ], + type: 'relationship', + value: { + id: relationB.id, + }, + relationTo: 'relation-b', + }, + ], + }, + }); + + const relationANoDepth = await payload.findByID({ + collection: 'relation-a', + id: relationA.id, + depth: 0, + }); + + expect(relationANoDepth.relationship).toStrictEqual(relationB.id); + + const relationAWithDepth = await payload.findByID({ + collection: 'relation-a', + id: relationA.id, + depth: 4, + }); + + const innerMostRelationship = relationAWithDepth.relationship.relationship.richText[1].value.relationship.relationship; + + expect(typeof innerMostRelationship).toStrictEqual(relationB.id); + }); }); });