From 95c165018edf8e80c4dc828d3d77b6fa6d799de9 Mon Sep 17 00:00:00 2001 From: Dan Ribbens Date: Tue, 22 Jun 2021 14:14:00 -0400 Subject: [PATCH] fix: respect maxDepth 0 --- docs/fields/relationship.mdx | 2 +- docs/fields/upload.mdx | 2 +- docs/getting-started/concepts.mdx | 2 +- src/fields/accessPromise.ts | 2 +- src/fields/config/types.ts | 8 ++++++++ src/fields/relationshipPopulationPromise.ts | 7 ++++--- 6 files changed, 16 insertions(+), 7 deletions(-) diff --git a/docs/fields/relationship.mdx b/docs/fields/relationship.mdx index 66331537f9..2399f06296 100644 --- a/docs/fields/relationship.mdx +++ b/docs/fields/relationship.mdx @@ -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. | | **`*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. | -| **`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. | | **`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) | diff --git a/docs/fields/upload.mdx b/docs/fields/upload.mdx index 42e26f042e..ab869d3126 100644 --- a/docs/fields/upload.mdx +++ b/docs/fields/upload.mdx @@ -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. | | **`*relationTo`** * | Provide a single collection `slug` to allow this field to accept a relation to. Note: the related collection must be configured to support Uploads. | -| **`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. | | **`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) | diff --git a/docs/getting-started/concepts.mdx b/docs/getting-started/concepts.mdx index 20354a44ea..0150904575 100644 --- a/docs/getting-started/concepts.mdx +++ b/docs/getting-started/concepts.mdx @@ -69,7 +69,7 @@ For more, visit the [Access Control documentation](/docs/access-control/overview 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` ``` diff --git a/src/fields/accessPromise.ts b/src/fields/accessPromise.ts index 6ea9acd3e0..5c197f4b6d 100644 --- a/src/fields/accessPromise.ts +++ b/src/fields/accessPromise.ts @@ -60,7 +60,7 @@ const accessPromise = async ({ relationshipPopulations.push(relationshipPopulationPromise({ data, field, - depth: field.maxDepth || depth, + depth, currentDepth, req, overrideAccess, diff --git a/src/fields/config/types.ts b/src/fields/config/types.ts index a922ea8c04..7d615d1ac9 100644 --- a/src/fields/config/types.ts +++ b/src/fields/config/types.ts @@ -261,6 +261,10 @@ export type FieldWithMany = SelectField | RelationshipField +export type FieldWithMaxDepth = + UploadField + | RelationshipField + export function fieldHasSubFields(field: Field): field is FieldWithSubFields { 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'; } +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'; diff --git a/src/fields/relationshipPopulationPromise.ts b/src/fields/relationshipPopulationPromise.ts index 450c913af5..99f2222c18 100644 --- a/src/fields/relationshipPopulationPromise.ts +++ b/src/fields/relationshipPopulationPromise.ts @@ -1,6 +1,6 @@ import { PayloadRequest } from '../express/types'; import executeAccess from '../auth/executeAccess'; -import { Field, RelationshipField, fieldSupportsMany } from './config/types'; +import { Field, RelationshipField, fieldSupportsMany, fieldHasMaxDepth } from './config/types'; import { Payload } from '..'; type PopulateArgs = { @@ -95,6 +95,7 @@ const relationshipPopulationPromise = ({ payload, }: PromiseArgs) => async (): Promise => { const resultingData = data; + const populateDepth = fieldHasMaxDepth(field) && field.maxDepth < depth ? field.maxDepth : depth; if (fieldSupportsMany(field) && field.hasMany && Array.isArray(data[field.name])) { const rowPromises = []; @@ -103,7 +104,7 @@ const relationshipPopulationPromise = ({ const rowPromise = async () => { if (relatedDoc) { await populate({ - depth, + depth: populateDepth, currentDepth, req, overrideAccess, @@ -122,7 +123,7 @@ const relationshipPopulationPromise = ({ await Promise.all(rowPromises); } else if (data[field.name]) { await populate({ - depth, + depth: populateDepth, currentDepth, req, overrideAccess,