Skip to content

Commit 00d9156

Browse files
authored
docs: clarify draft parameter and _status field interaction (#14723)
### What? Clarifies how the `draft` parameter and `_status` field work together in the drafts documentation. ### Why? The existing docs implied that the `draft` parameter controls whether documents are published or not, but that's not accurate. The `draft` parameter controls validation and write location, while `_status` controls the actual publish state. This led to confusion about why `draft: false` doesn't publish documents and when each parameter takes precedence. ### How? - Separated the explanation of `draft` parameter (validation + write location) from `_status` field (publish state) - Clarified that on initial create, both `draft: true/false` write to main collection - Added reference table showing all combinations of `draft` and `_status` for create/update operations - Updated examples to show explicit `_status` usage Fixes #13393
1 parent bd9f15f commit 00d9156

File tree

1 file changed

+65
-16
lines changed

1 file changed

+65
-16
lines changed

docs/versions/drafts.mdx

Lines changed: 65 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,9 @@ Within the Admin UI, if drafts are enabled, a document can be shown with one of
4949
specify if you are interacting with drafts or with live documents.
5050
</Banner>
5151

52-
#### Updating or creating drafts
52+
#### Using the `draft` parameter
5353

54-
If you enable drafts on a collection or global, the `create` and `update` operations for REST, GraphQL, and Local APIs expose a new option called `draft` which allows you to specify if you are creating or updating a **draft**, or if you're just sending your changes straight to the published document.
55-
56-
For example:
54+
When drafts are enabled, the `create`, `update`, `find`, and `findByID` operations for REST, GraphQL, and Local APIs expose a `draft` parameter. For write operations, it controls validation and where data is written. For read operations, it determines whether to return draft versions.
5755

5856
```ts
5957
// REST API
@@ -65,7 +63,7 @@ await payload.create({
6563
data: {
6664
// your data here
6765
},
68-
draft: true, // This is required to create a draft
66+
draft: true,
6967
})
7068

7169
// GraphQL
@@ -76,29 +74,80 @@ mutation {
7674
}
7775
```
7876
79-
**Required fields**
77+
**Understanding `draft` parameter and `_status` field**
78+
79+
The `draft` parameter and `_status` field work together but serve different purposes:
80+
81+
**`draft` parameter** - Controls two things:
82+
83+
1. **Validation**: When `draft: true`, required fields are not enforced, allowing you to save incomplete documents
84+
2. **Write location**: Determines whether the main collection document is updated
85+
- `draft: true` - Saves ONLY to versions table (main collection unchanged)
86+
- `draft: false` or omitted - Saves to BOTH main collection AND versions table
87+
88+
**`_status` field** - Indicates whether a document is published or in draft state
89+
90+
- Defaults to `'draft'` when not explicitly provided
91+
- Can be explicitly set in your data to `'published'` or `'draft'`
92+
93+
**First document creation**
94+
95+
When you first create a document, it's always written to the main collection (since no document exists yet):
96+
97+
- If you don't specify `_status`, it defaults to `_status: 'draft'`
98+
- A version is also created in the versions table
99+
- The `draft` parameter controls validation but doesn't change where the initial document is written
100+
101+
**Subsequent updates**
80102
81-
If `draft` is enabled while creating or updating a document, all fields are considered as not required, so that you can save drafts that are incomplete.
103+
After initial creation, the `draft` parameter controls where your updates are written:
82104
83-
Setting `_status: "draft"` will not bypass the required fields. You need to set `draft: true` as shown in the previous examples.
105+
- **`draft: true`** - Only the versions table is updated; the main collection document remains unchanged
106+
- **`draft: false` or omitted** - Both the main collection and versions table are updated
107+
108+
**Important:** The `draft` parameter does NOT control whether a document is published or not. A document remains with `_status: 'draft'` by default unless you explicitly set `_status: 'published'` in your data.
109+
110+
**Publishing a document**
111+
112+
To publish a document, you must explicitly set `_status: 'published'` in your data. When you do this:
113+
114+
- If you use `draft: false` or omit it, the main collection will be updated with the published status
115+
- If you use `draft: true`, the `_status: 'published'` takes precedence and will still update the main collection as published (overriding the `draft: true` behavior)
116+
117+
**Quick reference**
118+
119+
| Operation | `draft` param | `_status` in data | Result |
120+
| --------- | ------------------ | -------------------- | -------------------------------------------------------------- |
121+
| Create | `true` or `false` | omitted | Main collection updated with `_status: 'draft'` |
122+
| Create | `true` or `false` | `'published'` | Main collection updated with `_status: 'published'` |
123+
| Update | `true` | omitted or `'draft'` | Only versions table updated, main collection unchanged |
124+
| Update | `true` | `'published'` | Main collection updated with `_status: 'published'` (override) |
125+
| Update | `false` or omitted | omitted | Main collection updated with `_status: 'draft'` |
126+
| Update | `false` or omitted | `'published'` | Main collection updated with `_status: 'published'` |
127+
128+
**Required fields**
129+
130+
Setting `_status: "draft"` will not bypass required field validation. You need to set `draft: true` to save incomplete documents as shown in the previous examples.
84131
85132
#### Reading drafts vs. published documents
86133
87134
In addition to the `draft` argument within `create` and `update` operations, a `draft` argument is also exposed for `find` and `findByID` operations.
88135
89-
If `draft` is set to `true` while reading a document, **Payload will automatically replace returned document(s) with their newest drafts** if any newer drafts are available.
136+
When `draft` is set to `true` while reading a document, **Payload will return the most recent version from the versions table**, regardless of whether it's a draft or published document.
137+
138+
**Example scenario:**
90139
91-
**For example, let's take the following scenario:**
140+
1. You create a document with `_status: 'published'` (published in main collection)
141+
1. You update with `draft: true` to make changes without affecting the published version
142+
1. You update again with `draft: true` to make more draft changes
92143
93-
1. You create a new collection document and publish it right away
94-
1. You then make some updates, and save the updates as a draft
95-
1. You then make some further updates, and save more updates as another draft
144+
At this point, your published document remains unchanged in the main collection, and you have two newer draft versions in the `_[collectionSlug]_versions` table.
96145
97-
Here, you will have a published document that resides in your main collection, and then you'll have two _newer_ drafts that reside in the `_[collectionSlug]_versions` database collection.
146+
When you fetch the document with a standard `find` or `findByID` operation, the published document from the main collection is returned and draft versions are ignored.
98147
99-
If you simply fetch your created document using a `find` or `findByID` operation, your published document will be returned and the drafts will be ignored.
148+
However, if you pass `draft: true` to the read operation, Payload will return the most recent version from the versions table. In the scenario above with two draft versions, you'll get the latest (second) draft.
100149
101-
But, if you specify `draft` as `true`, Payload will automatically replace your published document's content with content coming from the most recently saved `version`. In this case, as we have created _two_ versions in the above scenario, Payload will send back data from the newest (second) draft and your document will appear as the most recently drafted version instead of the published version.
150+
**Note:** If there are no newer drafts (e.g., you published a document and haven't made draft changes since), querying with `draft: true` will still return the latest version from the versions table, which would be the same published content as in the main collection.
102151
103152
<Banner type="error">
104153
**Important:** the `draft` argument on its own will not restrict documents

0 commit comments

Comments
 (0)