Skip to content

Commit

Permalink
docs: improves queries docs (#7122)
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobsfletch authored Jul 11, 2024
1 parent 840e075 commit fac5425
Show file tree
Hide file tree
Showing 13 changed files with 218 additions and 241 deletions.
4 changes: 1 addition & 3 deletions docs/configuration/collections.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,10 @@ desc: Structure your Collections for your needs by defining fields, adding slugs
keywords: collections, config, configuration, documentation, Content Management System, cms, headless, javascript, node, react, nextjs
---

A Collection is a group of records, called Documents, that share a common schema. You can define as many Collections as your application needs. Each Collection saves to the [Database](../database/overview) based on the [Fields](../fields/overview) that you define.
A Collection is a group of records, called Documents, that share a common schema. You can define as many Collections as your application needs. Each Collection saves to the [Database](../database/overview) based on the [Fields](../fields/overview) that you define, and automatically generates the [Local API](../local-api/overview), [REST API](../rest-api/overview), and [GraphQL API](../graphql/overview) so you can query and mutate your Documents.

Collections are the primary way to structure recurring data in your application, such as users, products, pages, posts, and other types of content that you might want to manage. Each Collection can have its own unique [Access Control](../access-control/overview), [Hooks](../hooks/overview), [Admin Options](#admin-options), and more.

All Collections automatically accessible through the [Local API](../local-api/overview). If your application is using the [Admin Panel](../admin/overview), you will also get a fully-featured [REST API](../rest-api/overview) and [GraphQL API](../graphql/overview) without any additional configuration.

To define a Collection Config, use the `collection` property in your [Payload Config](./overview):

```ts
Expand Down
4 changes: 1 addition & 3 deletions docs/configuration/globals.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,10 @@ desc: Set up your Global config for your needs by defining fields, adding slugs
keywords: globals, config, configuration, documentation, Content Management System, cms, headless, javascript, node, react, nextjs
---

Globals are in many ways similar to [Collections](../configuration/collections), except they correspond to only a single Document. You can define as many Globals as your application needs. Each Global saves to the [Database](../database/overview) based on the [Fields](../fields/overview) that you define.
Globals are in many ways similar to [Collections](../configuration/collections), except they correspond to only a single Document. You can define as many Globals as your application needs. Each Global saves to the [Database](../database/overview) based on the [Fields](../fields/overview) that you define, and automatically generates the [Local API](../local-api/overview), [REST API](../rest-api/overview), and [GraphQL API](../graphql/overview) so you can query and mutate your Documents.

Globals are the primary way to structure singletons in Payload, such as a header navigation, site-wide banner alerts, or app-wide localized strings. Each Global can have its own unique [Access Control](../access-control/overview), [Hooks](../hooks/overview), [Admin Options](#admin-options), and more.

All Globals are automatically accessible through the [Local API](../local-api/overview). If your application is using the [Admin Panel](../admin/overview), you will also get a fully-featured [REST API](../rest-api/overview) and [GraphQL API](../graphql/overview) without any additional configuration.

To define a Global Config, use the `globals` property in your [Payload Config](./overview):

```ts
Expand Down
2 changes: 1 addition & 1 deletion docs/configuration/overview.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ The following options are available:
| **`graphQL`** | Manage GraphQL-specific functionality, including custom queries and mutations, query complexity limits, etc. [More details](../graphql/overview#graphql-options). |
| **`cookiePrefix`** | A string that will be prefixed to all cookies that Payload sets. |
| **`csrf`** | A whitelist array of URLs to allow Payload cookies to be accepted from as a form of CSRF protection. [More details](../authentication/overview#csrf-protection). |
| **`defaultDepth`** | If a user does not specify `depth` while requesting a resource, this depth will be used. [More details](../getting-started/concepts#depth). |
| **`defaultDepth`** | If a user does not specify `depth` while requesting a resource, this depth will be used. [More details](../queries/depth). |
| **`defaultMaxTextLength`** | The maximum allowed string length to be permitted application-wide. Helps to prevent malicious public document creation. |
| **`maxDepth`** | The maximum allowed depth to be permitted application-wide. This setting helps prevent against malicious queries. Defaults to `10`. |
| **`indexSortableFields`** | Automatically index all sortable top-level fields in the database to improve sort performance and add database compatibility for Azure Cosmos and similar. |
Expand Down
2 changes: 1 addition & 1 deletion docs/fields/relationship.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ _\* An asterisk denotes that a property is required._
<Banner type="success">
<strong>Tip:</strong>
<br />
The [Depth](/docs/getting-started/concepts#depth) parameter can be used to automatically populate
The [Depth](../queries/depth) parameter can be used to automatically populate
related documents that are returned by the API.
</Banner>

Expand Down
2 changes: 1 addition & 1 deletion docs/fields/upload.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,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. [More](/docs/fields/overview#field-names) |
| **`*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> |
| **`filterOptions`** | A query to filter which options appear in the UI and validate against. [More](#filtering-upload-options). |
| **`maxDepth`** | Sets a number limit on iterations of related documents to populate when queried. [Depth](/docs/getting-started/concepts#depth) |
| **`maxDepth`** | Sets a number limit on iterations of related documents to populate when queried. [Depth](../queries/depth) |
| **`label`** | Text used as a field label in the Admin Panel or an object with keys for each language. |
| **`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) |
Expand Down
120 changes: 1 addition & 119 deletions docs/getting-started/concepts.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -129,125 +129,7 @@ If you don't use GraphQL, you can delete those files! But if you do, you'll find

## Depth

<Banner type="info">
"Depth" gives you control over how many levels down related documents should be automatically
populated when retrieved.
</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._
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 at the following Collections:** `departments`, `users`, `posts`

```
// type: 'relationship' fields are equal to 1 depth level
{
slug: 'posts',
fields: [
{
name: 'title',
type: 'text',
},
{
name: 'author',
label: 'Post Author',
type: 'relationship',
relationTo: 'users',
}
]
}
{
slug: 'users',
fields: [
{
name: 'email',
type: 'email',
},
{
name: 'department'
type: 'relationship',
relationTo: 'departments'
}
]
}
{
slug: 'departments',
fields: [
{
name: 'name'
type: 'text',
}
]
}
```

If you were to query the Posts endpoint at, say, `http://localhost:3000/api/posts?depth=1`, you will retrieve Posts with populations one level deep. This depth parameter can be thought of as N, where N is the number of levels you want to populate. To populate one level further, you would simply specify N+1 as the depth. A returned result may look like the following:

```
// ?depth=1
{
id: '5ae8f9bde69e394e717c8832',
title: 'This post sucks',
author: {
id: '5f7dd05cd50d4005f8bcab17',
email: '[email protected]',
department: '5e3ca05cd50d4005f8bdab15'
}
}
```

Notice how the `user.author` is fully populated, but `user.author.department` is left as a document ID? That's because the User collection counted as the first level of `depth` and got populated—but then prevented any further populations from taking place.

To populate `user.author.department` in it's entirety you could specify `?depth=2` or _higher_.

```
// ?depth=2
{
id: '5ae8f9bde69e394e717c8832',
title: 'This post sucks',
author: {
id: '5f7dd05cd50d4005f8bcab17',
email: '[email protected]',
department: {
id: '5e3ca05cd50d4005f8bdab15',
name: 'Marvel'
}
}
}
```

### Field-level max depth

Fields like relationships or uploads can have a `maxDepth` property that limits the depth of the population for that field. Here are some examples:

Depth: 10
Current depth when field is accessed: 1
`maxDepth`: undefined

In this case, the field would be populated to 9 levels of population.

Depth: 10
Current depth when field is accessed: 0
`maxDepth`: 2

In this case, the field would be populated to 2 levels of population, despite there being a remaining depth of 8.

Depth: 10
Current depth when field is accessed: 2
`maxDepth`: 1

In this case, the field would not be populated, as the current depth (2) has exceeded the `maxDepth` for this field (1).

<Banner type="warning">
<strong>Note:</strong>
<br />
When access control on collections prevents relationship fields from populating, the API response
will contain the relationship id instead of the full document.
</Banner>
Documents can be related to other Documents through a concept called "relationships". When you query a Document, you can specify these relationships are populated. [More details](../queries/depth).

## Plugins

Expand Down
4 changes: 2 additions & 2 deletions docs/live-preview/client.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ And return the following values:
</Banner>

<Banner type="info">
It is important that the `depth` argument matches exactly with the depth of your initial page request. The depth property is used to populated relationships and uploads beyond their IDs. See [Depth](../getting-started/concepts#depth) for more information.
It is important that the `depth` argument matches exactly with the depth of your initial page request. The depth property is used to populated relationships and uploads beyond their IDs. See [Depth](../queries/depth) for more information.
</Banner>

## Frameworks
Expand Down Expand Up @@ -273,7 +273,7 @@ If you are using relationships or uploads in your front-end application, and you

### Relationships and/or uploads disappear after editing a document

It is possible that either you are setting an improper [`depth`](../getting-started/concepts#depth) in your initial request and/or your `useLivePreview` hook, or they're mismatched. Ensure that the `depth` parameter is set to the correct value, and that it matches exactly in both places. For example:
It is possible that either you are setting an improper [`depth`](../queries/depth) in your initial request and/or your `useLivePreview` hook, or they're mismatched. Ensure that the `depth` parameter is set to the correct value, and that it matches exactly in both places. For example:

```tsx
// Your initial request
Expand Down
2 changes: 1 addition & 1 deletion docs/local-api/overview.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ You can specify more options within the Local API vs. REST or GraphQL due to the
| ------------------ | ------------ |
| `collection` | Required for Collection operations. Specifies the Collection slug to operate against. |
| `data` | The data to use within the operation. Required for `create`, `update`. |
| `depth` | [Control auto-population](/docs/getting-started/concepts#depth) of nested relationship and upload fields. |
| `depth` | [Control auto-population](../queries/depth) of nested relationship and upload fields. |
| `locale` | Specify [locale](/docs/configuration/localization) for any returned documents. |
| `fallbackLocale` | Specify a [fallback locale](/docs/configuration/localization) to use for any returned documents. |
| `overrideAccess` | Skip access control. By default, this property is set to true within all Local API operations. |
Expand Down
94 changes: 94 additions & 0 deletions docs/queries/depth.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
---
title: Depth
label: Depth
order: 30
desc: Payload depth determines how many levels down related documents should be automatically populated when retrieved.
keywords: query, documents, pagination, documentation, Content Management System, cms, headless, javascript, node, react, nextjs
---

Documents in Payload can have relationships to other Documents. This is true for both [Collections](../configuration/collections) as well as [Globals](../configuration/globals). When you query a Document, you can specify the depth at which to populate any of its related Documents as full objects, or simply return their Document IDs.

Depth will optimize the performance of your application by limiting the number of queries made to the database, and significantly reducing the amount of data returned. Since Documents can be infinitely nested or recursively related, it's important to be able to control how deep your API populate.

When you specify a `depth` of `0`, for example, the API response might look like this:

```json
{
"id": "5ae8f9bde69e394e717c8832",
"title": "This is a great post",
"author": "5f7dd05cd50d4005f8bcab17"
}
```

But with a `depth` of `1`, the response might look like this:

```json
{
"id": "5ae8f9bde69e394e717c8832",
"title": "This is a great post",
"author": {
"id": "5f7dd05cd50d4005f8bcab17",
"name": "John Doe"
}
}
```

<Banner type="warning">
<strong>Important:</strong>
Depth has no effect in the [GraphQL API](../graphql/overview), because there, depth is based on the shape of your queries.
</Banner>

## Local API

To specify depth in the [Local API](../local-api/overview), you can use the `depth` option in your query:

```ts
const getPosts = async () => {
const posts = await payload.find({
collection: 'posts',
depth: 2, // highlight-line
})

return posts
}
```

<Banner type="info">
<strong>Reminder:</strong>
This is the same for [Globals](../configuration/globals) using the `findGlobal` operation.
</Banner>

## REST API

To specify depth in the [REST API](../rest-api/overview), you can use the `depth` parameter in your query:

```ts
fetch('https://localhost:3000/api/posts?depth=2') // highlight-line
.then((response) => response.json())
.then((data) => console.log(data))
```

<Banner type="info">
<strong>Reminder:</strong>
This is the same for [Globals](../configuration/globals) using the `/api/globals` endpoint.
</Banner>

## Max Depth

Fields like the [Relationship Field](../fields/relationship) or the [Upload Field](../fields/upload) can also set a maximum depth. If exceeded, this will limit the population depth regardless of the depth of the request.

To set a max depth for a field, use the `maxDepth` property in your field configuration:

```js
{
slug: 'posts',
fields: [
{
name: 'author',
type: 'relationship',
relationTo: 'users',
maxDepth: 2, // highlight-line
}
]
}
```
Loading

0 comments on commit fac5425

Please sign in to comment.