Store data as a draft
Storing data as a draft lets you save incomplete or preliminary information in a table without finalizing it. This feature is crucial for scenarios where data entry needs to be reviewed, edited, or completed over multiple iterations. Using drafts ensures that the data is accurate and complete before its publication.
Once you enable the Draft
setting for your type, you can use the following GraphQL mutations to manage drafts:
appendDraft<type>
confirmDraft<type>
cancelDraft<type>
As an example, let's create a draft record for the Product
type and then publish it.
1. Enable Draft
feature
To enable Draft
feature for a Product
type, update the settings as shown below.
<span><span style="color: var(--shiki-color-text)">Product: tailordb.#Type </span><span style="color: var(--shiki-token-keyword)">&</span><span style="color: var(--shiki-color-text)"> {</span></span>
<span><span style="color: var(--shiki-color-text)"> Description: </span><span style="color: var(--shiki-color-text)">"</span><span style="color: var(--shiki-token-string-expression)">Product model</span><span style="color: var(--shiki-color-text)">"</span></span>
<span><span style="color: var(--shiki-color-text)"> Settings: {</span></span>
<span><span style="color: var(--shiki-color-text)"> </span><span style="color: var(--shiki-token-comment)">// ...</span></span>
<span><span style="color: var(--shiki-color-text)"> </span><span style="color: var(--shiki-token-comment)">// enable draft</span></span>
<span><span style="color: var(--shiki-color-text)"> Draft: </span><span style="color: var(--shiki-token-constant)">true</span></span>
<span><span style="color: var(--shiki-color-text)"> }</span></span>
<span><span style="color: var(--shiki-color-text)">}</span></span>
<span></span>
2. Create a draft of the record
Create a draft record by passing the ID and updated field values for an existing record to the appendDraftProduct
mutation.
<span><span style="color: var(--shiki-token-keyword)">mutation</span><span style="color: var(--shiki-color-text)"> {</span></span>
<span><span style="color: var(--shiki-color-text)"> appendDraftProduct(id: </span><span style="color: var(--shiki-token-string-expression)">"d2847716-18b4-4fe1-b63a-ce01dbc21c5a"</span><span style="color: var(--shiki-color-text)">, input: { </span><span style="color: var(--shiki-token-string)">title</span><span style="color: var(--shiki-color-text)">: </span><span style="color: var(--shiki-token-string-expression)">"Draft product"</span><span style="color: var(--shiki-color-text)"> })</span></span>
<span><span style="color: var(--shiki-color-text)">}</span></span>
<span></span>
A token ID will be returned as a reference for the newly created draft record. Use this token ID in subsequent mutations to confirm or cancel the draft.
<span><span style="color: var(--shiki-color-text)">{</span></span>
<span><span style="color: var(--shiki-color-text)"> </span><span style="color: var(--shiki-token-comment)">"data"</span><span style="color: var(--shiki-color-text)">: {</span></span>
<span><span style="color: var(--shiki-color-text)"> </span><span style="color: var(--shiki-token-comment)">"appendDraftProduct"</span><span style="color: var(--shiki-color-text)">: </span><span style="color: var(--shiki-token-comment)">"019147c7-79da-71d6-95f7-a28cbc05401e"</span></span>
<span><span style="color: var(--shiki-color-text)"> }</span></span>
<span><span style="color: var(--shiki-color-text)">}</span></span>
<span></span>
You can view the created draft record using the following query.
<span><span style="color: var(--shiki-token-keyword)">query</span><span style="color: var(--shiki-color-text)"> {</span></span>
<span><span style="color: var(--shiki-color-text)"> draftProduct( id: </span><span style="color: var(--shiki-token-string-expression)">"09d103e9-716e-4f12-9960-e7d842a9bc2f"</span><span style="color: var(--shiki-color-text)"> token: </span><span style="color: var(--shiki-token-string-expression)">"019147c7-79da-71d6-95f7-a28cbc05401e"</span><span style="color: var(--shiki-color-text)">) {</span></span>
<span><span style="color: var(--shiki-color-text)"> id,</span></span>
<span><span style="color: var(--shiki-color-text)"> description</span></span>
<span><span style="color: var(--shiki-color-text)"> }</span></span>
<span><span style="color: var(--shiki-color-text)">}</span></span>
<span></span>
<span></span>
To determine whether a draft record exists for a Product
, run the hasDraftProduct
query with the record ID. If a draft record exists, it will return the token ID
; otherwise, it will return null
.
query {
hasDraftProduct(id: "09d103e9-716e-4f12-9960-e7d842a9bc2f")
}
3. Publish the draft record
To publish the draft record, use the confirmDraftProduct
mutation with the record ID and token ID.
<span><span style="color: var(--shiki-token-keyword)">mutation</span><span style="color: var(--shiki-color-text)"> {</span></span>
<span><span style="color: var(--shiki-color-text)"> confirmDraftProduct(id:</span><span style="color: var(--shiki-token-comment)">"09d103e9-716e-4f12-9960-e7d842a9bc2f"</span><span style="color: var(--shiki-color-text)">, token:</span><span style="color: var(--shiki-token-comment)">"019147c7-79da-71d6-95f7-a28cbc05401e"</span><span style="color: var(--shiki-color-text)">) {</span></span>
<span><span style="color: var(--shiki-color-text)"> id</span></span>
<span><span style="color: var(--shiki-color-text)"> }</span></span>
<span><span style="color: var(--shiki-color-text)">}</span></span>
<span></span>
To cancel the draft record, use the cancelDraftProduct
mutation with the record ID and token ID.
<span><span style="color: var(--shiki-token-keyword)">mutation</span><span style="color: var(--shiki-color-text)"> {</span></span>
<span><span style="color: var(--shiki-color-text)"> cancelDraftProduct(id:</span><span style="color: var(--shiki-token-comment)">"09d103e9-716e-4f12-9960-e7d842a9bc2f"</span><span style="color: var(--shiki-color-text)">, token:</span><span style="color: var(--shiki-token-comment)">"019147c7-79da-71d6-95f7-a28cbc05401e"</span><span style="color: var(--shiki-color-text)">)</span></span>
<span><span style="color: var(--shiki-color-text)">}</span></span>
<span></span>