From 880dabdcad10dcd9f057da71a601190fbeecf92d Mon Sep 17 00:00:00 2001 From: Dan Ribbens Date: Tue, 22 Jun 2021 13:28:27 -0400 Subject: [PATCH] feat: adds maxDepth to relationships and upload fields --- demo/collections/AllFields.ts | 7 +++++++ docs/fields/relationship.mdx | 1 + docs/fields/upload.mdx | 1 + docs/getting-started/concepts.mdx | 2 +- src/fields/accessPromise.ts | 2 +- src/fields/config/schema.ts | 2 ++ src/fields/config/types.ts | 2 ++ 7 files changed, 15 insertions(+), 2 deletions(-) diff --git a/demo/collections/AllFields.ts b/demo/collections/AllFields.ts index 92a1f7348d..7f0a64707a 100644 --- a/demo/collections/AllFields.ts +++ b/demo/collections/AllFields.ts @@ -240,6 +240,13 @@ const AllFields: PayloadCollectionConfig = { name: 'relationshipMultipleCollections', relationTo: ['localized-posts', 'conditions'], }, + { + type: 'relationship', + label: 'Relationship with maxDepth', + name: 'relationshipMaxDepth', + relationTo: 'relationship-a', + maxDepth: 1, + }, { type: 'textarea', label: 'Textarea', diff --git a/docs/fields/relationship.mdx b/docs/fields/relationship.mdx index a5681e9c53..66331537f9 100644 --- a/docs/fields/relationship.mdx +++ b/docs/fields/relationship.mdx @@ -23,6 +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. | | **`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 91e16f7796..42e26f042e 100644 --- a/docs/fields/upload.mdx +++ b/docs/fields/upload.mdx @@ -27,6 +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. | | **`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 ee623df749..20354a44ea 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. **For example, let's look the following Collections:** `departments`, `users`, `posts` ``` diff --git a/src/fields/accessPromise.ts b/src/fields/accessPromise.ts index 5c197f4b6d..6ea9acd3e0 100644 --- a/src/fields/accessPromise.ts +++ b/src/fields/accessPromise.ts @@ -60,7 +60,7 @@ const accessPromise = async ({ relationshipPopulations.push(relationshipPopulationPromise({ data, field, - depth, + depth: field.maxDepth || depth, currentDepth, req, overrideAccess, diff --git a/src/fields/config/schema.ts b/src/fields/config/schema.ts index 50d17d985d..b2cd5afdba 100644 --- a/src/fields/config/schema.ts +++ b/src/fields/config/schema.ts @@ -171,6 +171,7 @@ export const upload = baseField.keys({ type: joi.string().valid('upload').required(), relationTo: joi.string().required(), name: joi.string().required(), + maxDepth: joi.number(), }); export const checkbox = baseField.keys({ @@ -187,6 +188,7 @@ export const relationship = baseField.keys({ joi.array().items(joi.string()), ), name: joi.string().required(), + maxDepth: joi.number(), }); export const blocks = baseField.keys({ diff --git a/src/fields/config/types.ts b/src/fields/config/types.ts index c969daf2ef..a922ea8c04 100644 --- a/src/fields/config/types.ts +++ b/src/fields/config/types.ts @@ -141,6 +141,7 @@ export type RowField = FieldBase & { export type UploadField = FieldBase & { type: 'upload'; relationTo: string; + maxDepth?: number; } type CodeAdmin = Admin & { @@ -164,6 +165,7 @@ export type RelationshipField = FieldBase & { type: 'relationship'; relationTo: string | string[]; hasMany?: boolean; + maxDepth?: number; } type RichTextPlugin = (editor: Editor) => Editor;