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:

  1. appendDraft<type>
  2. confirmDraft<type>
  3. 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)">&amp;</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)">&quot;</span><span style="color: var(--shiki-token-string-expression)">Product model</span><span style="color: var(--shiki-color-text)">&quot;</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)">&quot;d2847716-18b4-4fe1-b63a-ce01dbc21c5a&quot;</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)">&quot;Draft product&quot;</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)">&quot;data&quot;</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)">&quot;appendDraftProduct&quot;</span><span style="color: var(--shiki-color-text)">: </span><span style="color: var(--shiki-token-comment)">&quot;019147c7-79da-71d6-95f7-a28cbc05401e&quot;</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)">&quot;09d103e9-716e-4f12-9960-e7d842a9bc2f&quot;</span><span style="color: var(--shiki-color-text)"> token: </span><span style="color: var(--shiki-token-string-expression)">&quot;019147c7-79da-71d6-95f7-a28cbc05401e&quot;</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)">&quot;09d103e9-716e-4f12-9960-e7d842a9bc2f&quot;</span><span style="color: var(--shiki-color-text)">, token:</span><span style="color: var(--shiki-token-comment)">&quot;019147c7-79da-71d6-95f7-a28cbc05401e&quot;</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)">&quot;09d103e9-716e-4f12-9960-e7d842a9bc2f&quot;</span><span style="color: var(--shiki-color-text)">, token:</span><span style="color: var(--shiki-token-comment)">&quot;019147c7-79da-71d6-95f7-a28cbc05401e&quot;</span><span style="color: var(--shiki-color-text)">)</span></span>
<span><span style="color: var(--shiki-color-text)">}</span></span>
<span></span>