Adding a new field to data model with Tailor Platform

Tailor Platform makes it easy to modify data schemas. The main database in Tailor Platform is called Tailor DB. With Tailor DB, you can effortlessly modify existing data schemas (data models) in our templates or add new Types (equivalent to Tables in a Relational Database). The GraphQL endpoint will be automatically generated, eliminating the need for additional coding.

This tutorial demonstrates how to use Tailor DB to modify the data schema of an application. You'll learn essential concepts and techniques for working with Tailor DB.

Tutorials – Modify Schema

  • Complete Quickstart first If you haven't yet built the Inventory-tracker app from our template.
  • See Core concepts to get an overview of Workspace, Organization, Application and Service.

Tailor DB

Tailor DB is a service that enables users to manage data schemas and generate GraphQL APIs based on those schemas. It enables you to quickly create GraphQL APIs for your clients. See Tailor DB concept page for more information.

Tutorial steps

To add a new inStock field to the Product type, you will need to:

  1. Add inStock field in product.cue file
  2. Apply the change using tailorctl
  3. Verify schema change through GraphQL

1. Add inStock field in product.cue file

To add a new field, you need to provide its type and description. For a field that handles date type, the type should be specified as tailordb.#FieldTypeDate. For more information about other types, see the Tailor DB concept page.

<span><span style="color: var(--shiki-token-keyword)">package</span><span style="color: var(--shiki-color-text)"> master</span></span>
<span><span style="color: var(--shiki-color-text)">...</span></span>
<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)">  Fields: {</span></span>
<span><span style="color: var(--shiki-color-text)">    ...</span></span>
<span><span style="color: var(--shiki-color-text)">    inStock: {</span></span>
<span><span style="color: var(--shiki-color-text)">      Type:        tailordb.#TypeInt</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)">Quantity of the product</span><span style="color: var(--shiki-color-text)">&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 style="color: var(--shiki-color-text)">}</span></span>
<span></span>
<span></span>

The field like id is generated without specifically defined in product.cue. This is a pre-defined field and the name of the field is reserved. Please refer to Auto-generated fields.

2. Apply the change by tailorctl.

Generate new workspace CUE file and apply the Tailor DB changes.

<span><span style="color: var(--shiki-token-function)">cue</span><span style="color: var(--shiki-color-text)"> </span><span style="color: var(--shiki-token-string)">eval</span><span style="color: var(--shiki-color-text)"> </span><span style="color: var(--shiki-token-string)">-f</span><span style="color: var(--shiki-color-text)"> </span><span style="color: var(--shiki-token-string)">manifest/workspace.cue</span><span style="color: var(--shiki-color-text)"> </span><span style="color: var(--shiki-token-string)">-o</span><span style="color: var(--shiki-color-text)"> </span><span style="color: var(--shiki-token-string)">generated/workspace.cue</span></span>
<span><span style="color: var(--shiki-token-function)">tailorctl</span><span style="color: var(--shiki-color-text)"> </span><span style="color: var(--shiki-token-string)">workspace</span><span style="color: var(--shiki-color-text)"> </span><span style="color: var(--shiki-token-string)">apply</span><span style="color: var(--shiki-color-text)"> </span><span style="color: var(--shiki-token-string)">-m</span><span style="color: var(--shiki-color-text)"> </span><span style="color: var(--shiki-token-string)">generated/workspace.cue</span></span>
<span></span>

3. Verify schema change through GraphQL

<span><span style="color: var(--shiki-token-comment)"># Open the GraphQL playground in the browser</span></span>
<span><span style="color: var(--shiki-token-function)">tailorctl</span><span style="color: var(--shiki-color-text)"> </span><span style="color: var(--shiki-token-string)">workspace</span><span style="color: var(--shiki-color-text)"> </span><span style="color: var(--shiki-token-string)">app</span><span style="color: var(--shiki-color-text)"> </span><span style="color: var(--shiki-token-string)">open</span><span style="color: var(--shiki-color-text)"> </span><span style="color: var(--shiki-token-string)">-n</span><span style="color: var(--shiki-color-text)"> </span><span style="color: var(--shiki-token-string)">inventory-tracker</span></span>
<span></span>

Let's create a Product resource with inStock field.

<span><span style="color: var(--shiki-token-comment)"># Create new Product data</span></span>
<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)">  createProduct(input: { </span><span style="color: var(--shiki-token-string)">inStock</span><span style="color: var(--shiki-color-text)">: </span><span style="color: var(--shiki-token-constant)">20</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>
<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-keyword)">&quot;data&quot;</span><span style="color: var(--shiki-token-punctuation)">:</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-keyword)">&quot;createProduct&quot;</span><span style="color: var(--shiki-token-punctuation)">:</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-keyword)">&quot;id&quot;</span><span style="color: var(--shiki-token-punctuation)">:</span><span style="color: var(--shiki-color-text)"> </span><span style="color: var(--shiki-token-string-expression)">&quot;c3e9313b-ef80-45da-a5b2-f6d77810dd92&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 style="color: var(--shiki-color-text)">}</span></span>
<span></span>

You can view the inStock value for the Product resource by running the following query. Replace product's ID with ID obtained from the previous query.

<span><span style="color: var(--shiki-token-comment)"># Get Product data</span></span>
<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)">  product(id: </span><span style="color: var(--shiki-token-string-expression)">&quot;c3e9313b-ef80-45da-a5b2-f6d77810dd92&quot;</span><span style="color: var(--shiki-color-text)">) {</span></span>
<span><span style="color: var(--shiki-color-text)">    inStock</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 style="color: var(--shiki-color-text)">{</span></span>
<span><span style="color: var(--shiki-color-text)">  </span><span style="color: var(--shiki-token-keyword)">&quot;data&quot;</span><span style="color: var(--shiki-token-punctuation)">:</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-keyword)">&quot;product&quot;</span><span style="color: var(--shiki-token-punctuation)">:</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-keyword)">&quot;inStock&quot;</span><span style="color: var(--shiki-token-punctuation)">:</span><span style="color: var(--shiki-color-text)"> </span><span style="color: var(--shiki-token-constant)">20</span></span>
<span><span style="color: var(--shiki-color-text)">    }</span></span>
<span><span style="color: var(--shiki-color-text)">  }</span></span>
<span><span style="color: var(--shiki-color-text)">}</span></span>
<span></span>

Next steps

Now that you have learned how to add a field, you can proceed to Creating Data Schema.

Further information