WARNING: THIS SITE IS A MIRROR OF GITHUB.COM / IT CANNOT LOGIN OR REGISTER ACCOUNTS / THE CONTENTS ARE PROVIDED AS-IS / THIS SITE ASSUMES NO RESPONSIBILITY FOR ANY DISPLAYED CONTENT OR LINKS / IF YOU FOUND SOMETHING MAY NOT GOOD FOR EVERYONE, CONTACT ADMIN AT ilovescratch@foxmail.com
Skip to content

Commit 95c1650

Browse files
committed
fix: respect maxDepth 0
1 parent 880dabd commit 95c1650

File tree

6 files changed

+16
-7
lines changed

6 files changed

+16
-7
lines changed

docs/fields/relationship.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ keywords: relationship, fields, config, configuration, documentation, Content Ma
2323
| **`name`** * | To be used as the property name when stored and retrieved from the database. |
2424
| **`*relationTo`** * | Provide one or many collection `slug`s to be able to assign relationships to. |
2525
| **`hasMany`** | Boolean when, if set to `true`, allows this field to have many relations instead of only one. |
26-
| **`maxDepth`** | Sets a number limit on iterations of related documents to populate when queried. |
26+
| **`maxDepth`** | Sets a number limit on iterations of related documents to populate when queried. [Depth](/docs/getting-started/concepts#depth) |
2727
| **`label`** | Used as a field label in the Admin panel and to name the generated GraphQL type. |
2828
| **`unique`** | Enforce that each entry in the Collection has a unique value for this field. |
2929
| **`validate`** | Provide a custom validation function that will be executed on both the Admin panel and the backend. [More](/docs/fields/overview#validation) |

docs/fields/upload.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ keywords: upload, images media, fields, config, configuration, documentation, Co
2727
| ---------------- | ----------- |
2828
| **`name`** * | To be used as the property name when stored and retrieved from the database. |
2929
| **`*relationTo`** * | Provide a single collection `slug` to allow this field to accept a relation to. <strong>Note: the related collection must be configured to support Uploads.</strong> |
30-
| **`maxDepth`** | Sets a number limit on iterations of related documents to populate when queried. |
30+
| **`maxDepth`** | Sets a number limit on iterations of related documents to populate when queried. [Depth](/docs/getting-started/concepts#depth) |
3131
| **`label`** | Used as a field label in the Admin panel and to name the generated GraphQL type. |
3232
| **`unique`** | Enforce that each entry in the Collection has a unique value for this field. |
3333
| **`validate`** | Provide a custom validation function that will be executed on both the Admin panel and the backend. [More](/docs/fields/overview#validation) |

docs/getting-started/concepts.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ For more, visit the [Access Control documentation](/docs/access-control/overview
6969
</Banner>
7070

7171
You can specify population `depth` via query parameter in the REST API and by an option in the local API. *Depth has no effect in the GraphQL API, because there, depth is based on the shape of your queries.*
72-
It is also possible to limit the depth for a specific field using the `maxDepth` property in your configuration.
72+
It is also possible to limit the depth for specific `relation` and `upload` fields using the `maxDepth` property in your configuration.
7373
**For example, let's look the following Collections:** `departments`, `users`, `posts`
7474

7575
```

src/fields/accessPromise.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ const accessPromise = async ({
6060
relationshipPopulations.push(relationshipPopulationPromise({
6161
data,
6262
field,
63-
depth: field.maxDepth || depth,
63+
depth,
6464
currentDepth,
6565
req,
6666
overrideAccess,

src/fields/config/types.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,10 @@ export type FieldWithMany =
261261
SelectField
262262
| RelationshipField
263263

264+
export type FieldWithMaxDepth =
265+
UploadField
266+
| RelationshipField
267+
264268
export function fieldHasSubFields(field: Field): field is FieldWithSubFields {
265269
return (field.type === 'group' || field.type === 'array' || field.type === 'row');
266270
}
@@ -289,4 +293,8 @@ export function fieldSupportsMany(field: Field): field is FieldWithMany {
289293
return field.type === 'select' || field.type === 'relationship';
290294
}
291295

296+
export function fieldHasMaxDepth(field: Field): field is FieldWithMaxDepth {
297+
return (field.type === 'upload' || field.type === 'relationship') && typeof field.maxDepth === 'number';
298+
}
299+
292300
export type HookName = 'beforeChange' | 'beforeValidate' | 'afterChange' | 'afterRead';

src/fields/relationshipPopulationPromise.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { PayloadRequest } from '../express/types';
22
import executeAccess from '../auth/executeAccess';
3-
import { Field, RelationshipField, fieldSupportsMany } from './config/types';
3+
import { Field, RelationshipField, fieldSupportsMany, fieldHasMaxDepth } from './config/types';
44
import { Payload } from '..';
55

66
type PopulateArgs = {
@@ -95,6 +95,7 @@ const relationshipPopulationPromise = ({
9595
payload,
9696
}: PromiseArgs) => async (): Promise<void> => {
9797
const resultingData = data;
98+
const populateDepth = fieldHasMaxDepth(field) && field.maxDepth < depth ? field.maxDepth : depth;
9899

99100
if (fieldSupportsMany(field) && field.hasMany && Array.isArray(data[field.name])) {
100101
const rowPromises = [];
@@ -103,7 +104,7 @@ const relationshipPopulationPromise = ({
103104
const rowPromise = async () => {
104105
if (relatedDoc) {
105106
await populate({
106-
depth,
107+
depth: populateDepth,
107108
currentDepth,
108109
req,
109110
overrideAccess,
@@ -122,7 +123,7 @@ const relationshipPopulationPromise = ({
122123
await Promise.all(rowPromises);
123124
} else if (data[field.name]) {
124125
await populate({
125-
depth,
126+
depth: populateDepth,
126127
currentDepth,
127128
req,
128129
overrideAccess,

0 commit comments

Comments
 (0)