Fields in schema

As explained in the Basic Structure of a Schema, each field of the data model (corresponding to columns in a relational database) is defined in the fields property of the schema file.

Here, we will discuss how to configure the schema within the Fields property.

Properties of Fields

As shown in the example schema below, Fields contains the following structure

  • Field name: the name for the field (required).
  • Description: the description of the field in String. This will appear in GraphQL SDL and Playground (required).
  • Type: the data type of the field (required). Have to be one of Data types

With just this, the GraphQL API is automatically generated.

<span><span style="color: var(--shiki-color-text)">...</span></span>
<span><span style="color: var(--shiki-token-comment)">// Schema definition of &quot;Product&quot; model begins here</span></span>
<span><span style="color: var(--shiki-color-text)">Product: commonType.#CommonType </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)">		shopifyID: {</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)">Shopify product ID</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)">		title: {</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)">Product title</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)">		handle: {</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)">Product handle</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)">Product description</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)">		featuredImageID: {</span></span>
<span><span style="color: var(--shiki-color-text)">			Type:        tailordb.#TypeUUID</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)">Featured image ID</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)">		featuredImage: {</span></span>
<span><span style="color: var(--shiki-color-text)">			Type:        </span><span style="color: var(--shiki-color-text)">&quot;</span><span style="color: var(--shiki-token-string-expression)">ProductImage</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)">Featured image of the product</span><span style="color: var(--shiki-color-text)">&quot;</span></span>
<span><span style="color: var(--shiki-color-text)">			SourceId:    </span><span style="color: var(--shiki-color-text)">&quot;</span><span style="color: var(--shiki-token-string-expression)">featuredImageID</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)">	TypePermission: permissions.editorAccess</span></span>
<span><span style="color: var(--shiki-color-text)">}</span></span>
<span></span>

Naming rules

Field names should use lower camel case (e.g., priceTotal) and must not contain spaces or special characters. Therefore, snake case naming (e.g., price_total) is not allowed. Use only alphabetic characters (A-Z, a-z) and numeric characters (0-9), and do not start with a numeric character. Even if the field name violates this rule, the API is still auto-generated, but it does not work correctly.

Data type

As for the Type field, set one of the following data types as tailordb.#<field_type>.

For example, to set the TypeString data type to the name field, use the following setting:

<span><span style="color: var(--shiki-token-comment)">// Example: set data type as FieldTypeString</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)">}</span></span>
<span></span>

Here are the available list of data types in Tailor DB. For details about the filter, refer to Tailor DB Filter specification.

Predefined Data TypeType DescriptionAvailable Filters
TypeStringStringeq, ne, in, nin, contains, regex
TypeUUIDString of of 32 hexadecimal digits as defined in RFC4122 ex. f81d4fae-7dec-11d0-a765-00a0c91e6bf6eq, ne, in, nin
TypeInt32-bit integereq, ne, lt, lte, gt, gte, between, in, nin
TypeFloat128-bit decimal-based floating-point numbereq, ne, lt, lte, gt, gte, between, in, nin
TypeEnumEnum. Values are defined in values fieldeq, ne, in, nin
TypeBoolBooleaneq, ne
TypeTimeString in the format HH:MM ex. "12:45"eq, ne, lt, lte, gt, gte, between, in, nin
TypeDateString in the format YYYY-MM-DD ex. "2022-10-10"eq, ne, lt, lte, gt, gte, between, in, nin
TypeDateTimeString in the ISO 8601 format yyyy-MM-ddTHH:mm:ss ex. "2024-06-25T19:34:42Z"eq, ne, lt, lte, gt, gte, between, in, nin
TypeNestedA type with nested fields. Nested fields are defined in fields field----

Auto-generated GraphQL data fields

Besides the fields specified within the fields parameter, Tailor DB automatically generates the following system fields for each Type in the GraphQL Schema:

Please be aware that these names are reserved for system use and cannot be employed as user-defined field names.

GraphQL field nameData TypeDescription
idUUIDUUID formatted unique identifier of the resource. Acts as the primary key of the resources.
createdBycreatedByID of the user and the user's organization who created the resource.
updatedByupdatedByID of the user and the user's organization who updated the resource.

Supporting fields

Describing more complex data models is also simple. By adding the following optional parameters to the field, you can set default values, array types, relations to other models, unique keys or foreign keys, and input value validation, among other things.

Required

If this field is set, the value must be provided. The data type of the field is Boolean.

Array

If this field is set, the field value will be an array. The data type of the field is Boolean.

Unique / Index

If these fields are enabled, the field value will be subject to uniqueness constraints. For this to take effect, both options must be enabled. The data type of the fields are both Boolean.

AllowedValues (used only in Enum)

If the field data type is TypeEnum, this can be used to define the enumerated values. The data type of the field is String. You can describe each defined value using the Description property.

<span><span style="color: var(--shiki-token-comment)">// define enum values</span></span>
<span><span style="color: var(--shiki-color-text)">inventoryType:{</span></span>
<span><span style="color: var(--shiki-color-text)">  Type:        tailordb.#TypeEnum</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)">Different categories of product</span><span style="color: var(--shiki-color-text)">&quot;</span></span>
<span><span style="color: var(--shiki-color-text)">  AllowedValues: [</span></span>
<span><span style="color: var(--shiki-color-text)">	{Value: </span><span style="color: var(--shiki-color-text)">&quot;</span><span style="color: var(--shiki-token-string-expression)">INVENTORY</span><span style="color: var(--shiki-color-text)">&quot;</span><span style="color: var(--shiki-token-punctuation)">,</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)">INVENTORY type</span><span style="color: var(--shiki-color-text)">&quot;</span><span style="color: var(--shiki-color-text)">}</span><span style="color: var(--shiki-token-punctuation)">,</span></span>
<span><span style="color: var(--shiki-color-text)">	{Value: </span><span style="color: var(--shiki-color-text)">&quot;</span><span style="color: var(--shiki-token-string-expression)">NON_INVENTORY</span><span style="color: var(--shiki-color-text)">&quot;</span><span style="color: var(--shiki-token-punctuation)">,</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)">NON_INVENTORY type</span><span style="color: var(--shiki-color-text)">&quot;</span><span style="color: var(--shiki-color-text)">}</span><span style="color: var(--shiki-token-punctuation)">,</span></span>
<span><span style="color: var(--shiki-color-text)">	{Value: </span><span style="color: var(--shiki-color-text)">&quot;</span><span style="color: var(--shiki-token-string-expression)">SERVICE</span><span style="color: var(--shiki-color-text)">&quot;</span><span style="color: var(--shiki-token-punctuation)">,</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)">SERVICE type</span><span style="color: var(--shiki-color-text)">&quot;</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>

Fields (used only in Nested)

If the field data type is TypeNested, use this to define the nested fields within the structure. The data type of the field is the same as that of the parent field.

SourceId (used for linking other Type)

If you want to link the data from different type, you can use the SourceId field to create the link. The data type of SourceId field is UUID.

Example

<span><span style="color: var(--shiki-token-comment)">// add a link to the Supplier data</span></span>
<span><span style="color: var(--shiki-color-text)">supplier: {</span></span>
<span><span style="color: var(--shiki-color-text)">  Type:        </span><span style="color: var(--shiki-color-text)">&quot;</span><span style="color: var(--shiki-token-string-expression)">Supplier</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)">Link to the model Supplier</span><span style="color: var(--shiki-color-text)">&quot;</span></span>
<span><span style="color: var(--shiki-color-text)">  SourceId: </span><span style="color: var(--shiki-color-text)">&quot;</span><span style="color: var(--shiki-token-string-expression)">supplierId</span><span style="color: var(--shiki-color-text)">&quot;</span><span style="color: var(--shiki-color-text)"> </span><span style="color: var(--shiki-token-comment)">// parameter &quot;SourceId&quot; is required to define the link</span></span>
<span><span style="color: var(--shiki-color-text)">}</span></span>
<span></span>
<span><span style="color: var(--shiki-token-comment)">// Defines which record to link within Type Supplier</span></span>
<span><span style="color: var(--shiki-color-text)">supplierId: {</span></span>
<span><span style="color: var(--shiki-color-text)">  Type:        tailordb.#TypeUUID</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)">UUID of type Supplier</span><span style="color: var(--shiki-color-text)">&quot;</span></span>
<span><span style="color: var(--shiki-color-text)">}</span></span>
<span></span>

ForeignKey / ForeignKeyType / ForeignKeyField

These fields can be configured in case SourceId field is set. If these fields are set, the field value will be subject to foreign key constraints. For this to take effect, both ForeignKey and ForeignKeyType must be enabled. ForeignKeyField is an optional field where you can specify ForeignKeyType's field. By default, it is id. Note, that you can only specify fields that have both "Unique" and "Index" properties set to true. The data type of ForeignKey is Boolean, that of ForeignKeyType and ForeignKeyField is String.

Example

<span><span style="color: var(--shiki-token-comment)">// add foreign key constraints to the Supplier</span></span>
<span><span style="color: var(--shiki-color-text)">supplierId: {</span></span>
<span><span style="color: var(--shiki-color-text)">  Type:           tailordb.#TypeUUID</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)">UUID of type Supplier</span><span style="color: var(--shiki-color-text)">&quot;</span></span>
<span><span style="color: var(--shiki-color-text)">  ForeignKey:     </span><span style="color: var(--shiki-token-constant)">true</span></span>
<span><span style="color: var(--shiki-color-text)">  ForeignKeyType: </span><span style="color: var(--shiki-color-text)">&quot;</span><span style="color: var(--shiki-token-string-expression)">Supplier</span><span style="color: var(--shiki-color-text)">&quot;</span></span>
<span><span style="color: var(--shiki-color-text)">  ForeignKeyField: </span><span style="color: var(--shiki-color-text)">&quot;</span><span style="color: var(--shiki-token-string-expression)">code</span><span style="color: var(--shiki-color-text)">&quot;</span><span style="color: var(--shiki-color-text)"> </span><span style="color: var(--shiki-token-comment)">// optional. if you don&#39;t set &quot;ForeignKeyField&quot;, &quot;id&quot; is used by default.</span></span>
<span><span style="color: var(--shiki-color-text)">}</span></span>
<span></span>

Validate

If this field is set, validation rules will be applied to the field value. Validations are evaluated after all properties are evaluated. For example, hooks will always run first then validation. The data type of the field is an array of maps, with Expr and Action defined. Learn more in Data Validations.

Hooks

If this field is set, the field value is computed using the given expression. Hooks are evaluated before validation. Learn more in Hooks.