fix: respect maxDepth 0

This commit is contained in:
Dan Ribbens
2021-06-22 14:14:00 -04:00
parent 880dabdcad
commit 95c165018e
6 changed files with 16 additions and 7 deletions

View File

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

View File

@@ -27,7 +27,7 @@ keywords: upload, images media, fields, config, configuration, documentation, Co
| ---------------- | ----------- | | ---------------- | ----------- |
| **`name`** * | To be used as the property name when stored and retrieved from the database. | | **`name`** * | To be used as the property name when stored and retrieved from the database. |
| **`*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> | | **`*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> |
| **`maxDepth`** | Sets a number limit on iterations of related documents to populate when queried. | | **`maxDepth`** | Sets a number limit on iterations of related documents to populate when queried. [Depth](/docs/getting-started/concepts#depth) |
| **`label`** | Used as a field label in the Admin panel and to name the generated GraphQL type. | | **`label`** | Used as a field label in the Admin panel and to name the generated GraphQL type. |
| **`unique`** | Enforce that each entry in the Collection has a unique value for this field. | | **`unique`** | Enforce that each entry in the Collection has a unique value for this field. |
| **`validate`** | Provide a custom validation function that will be executed on both the Admin panel and the backend. [More](/docs/fields/overview#validation) | | **`validate`** | Provide a custom validation function that will be executed on both the Admin panel and the backend. [More](/docs/fields/overview#validation) |

View File

@@ -69,7 +69,7 @@ For more, visit the [Access Control documentation](/docs/access-control/overview
</Banner> </Banner>
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.* 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.*
It is also possible to limit the depth for a specific field using the `maxDepth` property in your configuration. It is also possible to limit the depth for specific `relation` and `upload` fields using the `maxDepth` property in your configuration.
**For example, let's look the following Collections:** `departments`, `users`, `posts` **For example, let's look the following Collections:** `departments`, `users`, `posts`
``` ```

View File

@@ -60,7 +60,7 @@ const accessPromise = async ({
relationshipPopulations.push(relationshipPopulationPromise({ relationshipPopulations.push(relationshipPopulationPromise({
data, data,
field, field,
depth: field.maxDepth || depth, depth,
currentDepth, currentDepth,
req, req,
overrideAccess, overrideAccess,

View File

@@ -261,6 +261,10 @@ export type FieldWithMany =
SelectField SelectField
| RelationshipField | RelationshipField
export type FieldWithMaxDepth =
UploadField
| RelationshipField
export function fieldHasSubFields(field: Field): field is FieldWithSubFields { export function fieldHasSubFields(field: Field): field is FieldWithSubFields {
return (field.type === 'group' || field.type === 'array' || field.type === 'row'); return (field.type === 'group' || field.type === 'array' || field.type === 'row');
} }
@@ -289,4 +293,8 @@ export function fieldSupportsMany(field: Field): field is FieldWithMany {
return field.type === 'select' || field.type === 'relationship'; return field.type === 'select' || field.type === 'relationship';
} }
export function fieldHasMaxDepth(field: Field): field is FieldWithMaxDepth {
return (field.type === 'upload' || field.type === 'relationship') && typeof field.maxDepth === 'number';
}
export type HookName = 'beforeChange' | 'beforeValidate' | 'afterChange' | 'afterRead'; export type HookName = 'beforeChange' | 'beforeValidate' | 'afterChange' | 'afterRead';

View File

@@ -1,6 +1,6 @@
import { PayloadRequest } from '../express/types'; import { PayloadRequest } from '../express/types';
import executeAccess from '../auth/executeAccess'; import executeAccess from '../auth/executeAccess';
import { Field, RelationshipField, fieldSupportsMany } from './config/types'; import { Field, RelationshipField, fieldSupportsMany, fieldHasMaxDepth } from './config/types';
import { Payload } from '..'; import { Payload } from '..';
type PopulateArgs = { type PopulateArgs = {
@@ -95,6 +95,7 @@ const relationshipPopulationPromise = ({
payload, payload,
}: PromiseArgs) => async (): Promise<void> => { }: PromiseArgs) => async (): Promise<void> => {
const resultingData = data; const resultingData = data;
const populateDepth = fieldHasMaxDepth(field) && field.maxDepth < depth ? field.maxDepth : depth;
if (fieldSupportsMany(field) && field.hasMany && Array.isArray(data[field.name])) { if (fieldSupportsMany(field) && field.hasMany && Array.isArray(data[field.name])) {
const rowPromises = []; const rowPromises = [];
@@ -103,7 +104,7 @@ const relationshipPopulationPromise = ({
const rowPromise = async () => { const rowPromise = async () => {
if (relatedDoc) { if (relatedDoc) {
await populate({ await populate({
depth, depth: populateDepth,
currentDepth, currentDepth,
req, req,
overrideAccess, overrideAccess,
@@ -122,7 +123,7 @@ const relationshipPopulationPromise = ({
await Promise.all(rowPromises); await Promise.all(rowPromises);
} else if (data[field.name]) { } else if (data[field.name]) {
await populate({ await populate({
depth, depth: populateDepth,
currentDepth, currentDepth,
req, req,
overrideAccess, overrideAccess,