fix: #1887, dataloader rich text population infinite loop

This commit is contained in:
James
2023-02-07 14:35:37 -05:00
parent cff6608996
commit ac2e174643
3 changed files with 126 additions and 7 deletions

View File

@@ -31,16 +31,16 @@ export const populate = async ({
}): Promise<void> => {
const dataRef = data as Record<string, unknown>;
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;

View File

@@ -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({

View File

@@ -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);
});
});
});