Data Schema in Tailor Platform

This tutorial demonstrates how a data schema is mapped to GraphQL APIs. If you haven't built the Inventory-tracker app from our templates, complete Quickstart first.

Data schema

A data model of your application is defined by the schema. Although multiple data models can be defined in one single schema file, we recommend using one or a few data models per one schema file for readability. See Tailor DB concept page to learn more about schema.

Tutorial steps

  1. Open GraphQL playground
  2. Open product.cue file in your repository
  3. See how the product.cue is mapped to the GraphQL APIs

1. Open GraphQL Playground

To get started, open the GraphQL playground by navigating to your Tailor Platform endpoint's URL and appending /playground at the end. For example, if your endpoint URL is https://your-endpoint.erp.dev, the GraphQL playground URL would be https://your-endpoint.erp.dev/playground.

Alternatively you can use our CLI to open the GraphQL playground in your browser.

<span><span style="color: var(--shiki-token-comment)"># Open the browser and open the GraphQL playground</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>

2. Open product.cue file in your repository

In your application's repository, locate and open the product.cue file. This file contains the schema definition for the Product type. The schema will look like this:

<span><span style="color: var(--shiki-token-keyword)">package</span><span style="color: var(--shiki-color-text)"> master</span></span>
<span></span>
<span><span style="color: var(--shiki-token-keyword)">import</span><span style="color: var(--shiki-color-text)"> (</span></span>
<span><span style="color: var(--shiki-color-text)">	</span><span style="color: var(--shiki-color-text)">&quot;</span><span style="color: var(--shiki-token-string-expression)">github.com/tailor-platform/tailorctl/schema/v2/tailordb</span><span style="color: var(--shiki-color-text)">&quot;</span></span>
<span><span style="color: var(--shiki-color-text)">	</span><span style="color: var(--shiki-color-text)">&quot;</span><span style="color: var(--shiki-token-string-expression)">tailor.build/inventory-tracker/manifest/services/tailordb</span><span style="color: var(--shiki-color-text)">:permissions</span><span style="color: var(--shiki-color-text)">&quot;</span></span>
<span><span style="color: var(--shiki-color-text)">)</span></span>
<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)">	Name:        </span><span style="color: var(--shiki-color-text)">&quot;</span><span style="color: var(--shiki-token-string-expression)">Product</span><span style="color: var(--shiki-color-text)">&quot;</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)">	Fields: {</span></span>
<span><span style="color: var(--shiki-color-text)">		code: {</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)">Product code.</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)">		name: {</span></span>
<span><span style="color: var(--shiki-color-text)">			Type:        tailordb.#TypeString</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)">Name 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)">		description: {</span></span>
<span><span style="color: var(--shiki-color-text)">			Type:        tailordb.#TypeString</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)">Description 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)">		price: {</span></span>
<span><span style="color: var(--shiki-color-text)">			Type:        tailordb.#TypeFloat</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)">Price 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)">		uom: {</span></span>
<span><span style="color: var(--shiki-color-text)">			Type:        tailordb.#TypeString</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)">Unit of measure 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)">		sellingStartDate: {</span></span>
<span><span style="color: var(--shiki-color-text)">			Type:        tailordb.#TypeDate</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)">The date this product starts selling. This field is used to calculate the sales velocity used for demand forecasting.</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)">		createdAt: tailordb.CreatedAtField</span></span>
<span><span style="color: var(--shiki-color-text)">		updatedAt: tailordb.UpdatedAtField</span></span>
<span><span style="color: var(--shiki-color-text)">	}</span></span>
<span></span>
<span><span style="color: var(--shiki-color-text)">	TypePermission: permissions.editorAccess</span></span>
<span><span style="color: var(--shiki-color-text)">}</span></span>
<span></span>

This file is written in CUE lang. CUE is a configuration language designed to simplify and unify configuration across different platforms and languages. It uses values and rules to define configurations and supports composition for easy reuse and maintenance.

To learn more about CUE, visit CUE basics.

3. See how the product.cue is mapped to the GraphQL APIs

Now that you've seen the schema definition in the product.cue file, let's explore how this maps to the GraphQL API. In the GraphQL playground, you can test the API with a query like the one below:

<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)">  products {</span></span>
<span><span style="color: var(--shiki-color-text)">    collection {</span></span>
<span><span style="color: var(--shiki-color-text)">      id</span></span>
<span><span style="color: var(--shiki-color-text)">	  code</span></span>
<span><span style="color: var(--shiki-color-text)">      name</span></span>
<span><span style="color: var(--shiki-color-text)">	  description</span></span>
<span><span style="color: var(--shiki-color-text)">      price</span></span>
<span><span style="color: var(--shiki-color-text)">	  uom</span></span>
<span><span style="color: var(--shiki-color-text)">	  sellingStartDate</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>

This query will return a list of Product objects with the specified fields. You can see how the schema defined in the product.cue file is used to create the GraphQL API, allowing you to interact with your data easily.

You will notice some of 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.

Next steps

In the following tutorials, you'll learn how to modify the schema by adding fields to existing types or creating new types for your application.