Skip to content

Commit 938472b

Browse files
authored
fix: populate is ignored for nested relationships (#11227)
### What? As described in #11209, previously, the `populate` argument was ignored for nested relationships. ### Why? `populate` should work for nested relationships, no matter where they are in the tree. ### How? Preserves the `populate` argument in the payload data loader. Fixes #11209
1 parent 64d0217 commit 938472b

File tree

5 files changed

+118
-20
lines changed

5 files changed

+118
-20
lines changed

packages/payload/src/collections/dataloader.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import type { BatchLoadFn } from 'dataloader'
33

44
import DataLoader from 'dataloader'
55

6-
import type { PayloadRequest, SelectType } from '../types/index.js'
6+
import type { PayloadRequest, PopulateType, SelectType } from '../types/index.js'
77
import type { TypeWithID } from './config/types.js'
88

99
import { isValidID } from '../utilities/isValidID.js'
@@ -57,6 +57,7 @@ const batchAndLoadDocs =
5757
showHiddenFields,
5858
draft,
5959
select,
60+
populate,
6061
] = JSON.parse(key)
6162

6263
const batchKeyArray = [
@@ -70,6 +71,7 @@ const batchAndLoadDocs =
7071
showHiddenFields,
7172
draft,
7273
select,
74+
populate,
7375
]
7476

7577
const batchKey = JSON.stringify(batchKeyArray)
@@ -107,6 +109,7 @@ const batchAndLoadDocs =
107109
showHiddenFields,
108110
draft,
109111
select,
112+
populate,
110113
] = JSON.parse(batchKey)
111114

112115
req.transactionID = transactionID
@@ -121,6 +124,7 @@ const batchAndLoadDocs =
121124
locale,
122125
overrideAccess: Boolean(overrideAccess),
123126
pagination: false,
127+
populate,
124128
req,
125129
select,
126130
showHiddenFields: Boolean(showHiddenFields),
@@ -144,6 +148,7 @@ const batchAndLoadDocs =
144148
fallbackLocale,
145149
locale,
146150
overrideAccess,
151+
populate,
147152
select,
148153
showHiddenFields,
149154
transactionID: req.transactionID,
@@ -173,6 +178,7 @@ type CreateCacheKeyArgs = {
173178
fallbackLocale: string
174179
locale: string
175180
overrideAccess: boolean
181+
populate?: PopulateType
176182
select?: SelectType
177183
showHiddenFields: boolean
178184
transactionID: number | Promise<number | string> | string
@@ -186,6 +192,7 @@ export const createDataloaderCacheKey = ({
186192
fallbackLocale,
187193
locale,
188194
overrideAccess,
195+
populate,
189196
select,
190197
showHiddenFields,
191198
transactionID,
@@ -202,4 +209,5 @@ export const createDataloaderCacheKey = ({
202209
showHiddenFields,
203210
draft,
204211
select,
212+
populate,
205213
])

packages/payload/src/fields/hooks/afterRead/relationshipPopulationPromise.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ const populate = async ({
7272
fallbackLocale,
7373
locale,
7474
overrideAccess,
75+
populate: populateArg,
7576
select:
7677
populateArg?.[relatedCollection.config.slug] ??
7778
relatedCollection.config.defaultPopulate,

test/select/collections/Pages/index.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ export const Pages: CollectionConfig<'pages'> = {
2121
},
2222
access: { read: () => true },
2323
fields: [
24+
{
25+
name: 'relatedPage',
26+
type: 'relationship',
27+
relationTo: 'pages',
28+
},
2429
{
2530
name: 'content',
2631
type: 'blocks',

test/select/int.spec.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2230,6 +2230,33 @@ describe('Select', () => {
22302230
expect(richTextLexicalRel.value).toMatchObject(expectedHomePageOverride)
22312231
expect(richTextSlateRel.value).toMatchObject(expectedHomePageOverride)
22322232
})
2233+
2234+
it('should apply populate on depth 2', async () => {
2235+
const page_1 = await payload.create({
2236+
collection: 'pages',
2237+
data: { relatedPage: null, blocks: [{ blockType: 'some' }], slug: 'page-1' },
2238+
})
2239+
const page_2 = await payload.create({
2240+
collection: 'pages',
2241+
data: { relatedPage: page_1.id, slug: 'page-2' },
2242+
})
2243+
const page_3 = await payload.create({
2244+
collection: 'pages',
2245+
data: { relatedPage: page_2.id, slug: 'page-3' },
2246+
})
2247+
const result = await payload.findByID({
2248+
collection: 'pages',
2249+
id: page_3.id,
2250+
depth: 3,
2251+
populate: { pages: { slug: true, relatedPage: true } },
2252+
})
2253+
expect(result.relatedPage.id).toBe(page_2.id)
2254+
expect(result.relatedPage.relatedPage as Page).toStrictEqual({
2255+
id: page_1.id,
2256+
slug: page_1.slug,
2257+
relatedPage: null,
2258+
})
2259+
})
22332260
})
22342261
})
22352262

test/select/payload-types.ts

Lines changed: 76 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,65 @@
66
* and re-run `payload generate:types` to regenerate this file.
77
*/
88

9+
/**
10+
* Supported timezones in IANA format.
11+
*
12+
* This interface was referenced by `Config`'s JSON-Schema
13+
* via the `definition` "supportedTimezones".
14+
*/
15+
export type SupportedTimezones =
16+
| 'Pacific/Midway'
17+
| 'Pacific/Niue'
18+
| 'Pacific/Honolulu'
19+
| 'Pacific/Rarotonga'
20+
| 'America/Anchorage'
21+
| 'Pacific/Gambier'
22+
| 'America/Los_Angeles'
23+
| 'America/Tijuana'
24+
| 'America/Denver'
25+
| 'America/Phoenix'
26+
| 'America/Chicago'
27+
| 'America/Guatemala'
28+
| 'America/New_York'
29+
| 'America/Bogota'
30+
| 'America/Caracas'
31+
| 'America/Santiago'
32+
| 'America/Buenos_Aires'
33+
| 'America/Sao_Paulo'
34+
| 'Atlantic/South_Georgia'
35+
| 'Atlantic/Azores'
36+
| 'Atlantic/Cape_Verde'
37+
| 'Europe/London'
38+
| 'Europe/Berlin'
39+
| 'Africa/Lagos'
40+
| 'Europe/Athens'
41+
| 'Africa/Cairo'
42+
| 'Europe/Moscow'
43+
| 'Asia/Riyadh'
44+
| 'Asia/Dubai'
45+
| 'Asia/Baku'
46+
| 'Asia/Karachi'
47+
| 'Asia/Tashkent'
48+
| 'Asia/Calcutta'
49+
| 'Asia/Dhaka'
50+
| 'Asia/Almaty'
51+
| 'Asia/Jakarta'
52+
| 'Asia/Bangkok'
53+
| 'Asia/Shanghai'
54+
| 'Asia/Singapore'
55+
| 'Asia/Tokyo'
56+
| 'Asia/Seoul'
57+
| 'Australia/Sydney'
58+
| 'Pacific/Guam'
59+
| 'Pacific/Noumea'
60+
| 'Pacific/Auckland'
61+
| 'Pacific/Fiji';
62+
963
export interface Config {
1064
auth: {
1165
users: UserAuthOperations;
1266
};
67+
blocks: {};
1368
collections: {
1469
posts: Post;
1570
'localized-posts': LocalizedPost;
@@ -122,7 +177,7 @@ export interface Post {
122177
unnamedTabNumber?: number | null;
123178
hasOne?: (string | null) | Rel;
124179
hasMany?: (string | Rel)[] | null;
125-
hasManyUpload?: (string | Rel)[] | null;
180+
hasManyUpload?: (string | Upload)[] | null;
126181
hasOnePoly?: {
127182
relationTo: 'rels';
128183
value: string | Rel;
@@ -145,6 +200,24 @@ export interface Rel {
145200
updatedAt: string;
146201
createdAt: string;
147202
}
203+
/**
204+
* This interface was referenced by `Config`'s JSON-Schema
205+
* via the `definition` "upload".
206+
*/
207+
export interface Upload {
208+
id: string;
209+
updatedAt: string;
210+
createdAt: string;
211+
url?: string | null;
212+
thumbnailURL?: string | null;
213+
filename?: string | null;
214+
mimeType?: string | null;
215+
filesize?: number | null;
216+
width?: number | null;
217+
height?: number | null;
218+
focalX?: number | null;
219+
focalY?: number | null;
220+
}
148221
/**
149222
* This interface was referenced by `Config`'s JSON-Schema
150223
* via the `definition` "localized-posts".
@@ -290,6 +363,7 @@ export interface DeepPost {
290363
*/
291364
export interface Page {
292365
id: string;
366+
relatedPage?: (string | null) | Page;
293367
content?:
294368
| {
295369
title: string;
@@ -369,24 +443,6 @@ export interface Point {
369443
updatedAt: string;
370444
createdAt: string;
371445
}
372-
/**
373-
* This interface was referenced by `Config`'s JSON-Schema
374-
* via the `definition` "upload".
375-
*/
376-
export interface Upload {
377-
id: string;
378-
updatedAt: string;
379-
createdAt: string;
380-
url?: string | null;
381-
thumbnailURL?: string | null;
382-
filename?: string | null;
383-
mimeType?: string | null;
384-
filesize?: number | null;
385-
width?: number | null;
386-
height?: number | null;
387-
focalX?: number | null;
388-
focalY?: number | null;
389-
}
390446
/**
391447
* This interface was referenced by `Config`'s JSON-Schema
392448
* via the `definition` "users".
@@ -706,6 +762,7 @@ export interface DeepPostsSelect<T extends boolean = true> {
706762
* via the `definition` "pages_select".
707763
*/
708764
export interface PagesSelect<T extends boolean = true> {
765+
relatedPage?: T;
709766
content?:
710767
| T
711768
| {

0 commit comments

Comments
 (0)