Hygraph
Docs
View as Markdown

#Field types

Fields define the data a model can store. Each field uses one field type, and the type you choose determines how content editors enter the value and how the API returns it. This page helps you choose the right field type and links to the guides for setting up each one.

#Add a field to a model

  1. Open the model in the Schema builder.
  2. From the Add fields sidebar, select a field type.
  3. Configure the field display name, API ID, and any options or validations.
  4. Click Add.

You can add fields to custom models and to the system Asset model. Every model also includes system fields, which Hygraph manages automatically.

Avoid reserved terms when you set API IDs.

#Choose a field type

Field typeWhat it storesTypical use
Single line textA stringHeadings, page titles, email
Multi line textA string with no formattingDescriptions, raw text such as HTML or XML that you parse yourself
MarkdownA Markdown stringAn alternative to Rich text, MDX
SlugA URL-safe identifierPage URLs, such as getting-started
Rich textFormatted text, returned as raw, html, markdown, and textBody copy with headings, links, tables, images, and lists
IntegerA whole numberPrices in cents, stock quantities
FloatA floating point numberDistance, weight, volume, ratings
Booleantrue, false, or nullOn sale, part of a bundle, accepts comments
DateAn ISO 8601 dateEvent dates
Date and timeAn ISO 8601 date and timeEvent start times
JSONA JSON valueLarge amounts of data from other systems
ColorA color as HEX, RGBA, and CSS valuesBackground or theme colors
LocationLatitude and longitudeShop or venue locations
AssetA connection to one or more assetsImages, PDFs, and other files
EnumerationOne value from a predefined listCommodity type, status
TaxonomyA node from a hierarchical list of termsProduct categories
ComponentA reusable group of fieldsBanners, page sections, blocks
ReferenceA connection to entries in one or more modelsAuthors, related posts, categories
Remote fieldData fetched from a remote sourceProduct price and availability from an external API

#Text fields

Single line text, Multi line text, Markdown, and Slug are all variants of the String type. They differ in how they appear to content editors, but the API queries all of them the same way and returns a string.

  • Single line text: Use for short values such as headings, page titles, and email addresses.
  • Multi line text: Use for text that needs no formatting, or for raw text such as HTML and XML where you control the parsing.
  • Markdown: Use as an alternative to Rich text when you want to store Markdown, for example to use it with MDX.
  • Slug: Use to generate a URL-safe, human-readable identifier for an entry. You can configure it to generate its value from a template built from other fields on the model, including fields on a related model. See Slug field.

#Rich text

Rich text stores formatted content and renders an editor with tools for headings, links, tables, images, and lists. The API returns it in four formats by default: raw, html, markdown, and text. A json format is also available when embeds are enabled.

When you add a Rich text field, you can enable embedding and select which models editors can embed in the field.

If you allow multiple values for a Rich text field, you cannot mark the field as required.

To work with Rich text in the API, see Rich text field.

#Numbers and booleans

  • Integer: Use for whole numbers, such as prices in cents or stock quantities.
  • Float: Use for fractional values that need precision, such as distance, weight, volume, or ratings.
  • Boolean: Use for yes/no values, such as whether a product is on sale or a post accepts comments. Booleans default to null, and can be true or false.

#Dates

Both date field types follow the ISO 8601 standard.

  • Date: Use when you only need the date. For example, September 22, 1989 is stored as 1989-09-22.
  • Date and time: Use when you also need the time, for example 1989-09-22T09:30:00+00:00.

#JSON

JSON stores a JSON (JavaScript Object Notation) value. Use it to store large amounts of data from other systems. Editors don't normally work with this field.

#Color

Color stores a color that the API can return as a HEX value, an RGBA value, or a CSS value. In the content editor, editors pick the color from a palette or enter a HEX or RGB code, and can set opacity.

#Location

Location stores a latitude and longitude. In the content editor, editors select the coordinates on a map. In the API, you can also query the distance in meters from a latitude and longitude that you provide.

#Assets

Assets can be any file type, not only images. Adding an asset field to a model connects that model to the system Asset model. Depending on the field configuration, editors can select one asset or several.

The Asset model comes with its own asset fields, and you can add your own fields to it. To learn how assets work, see Assets.

#Enumerations and taxonomies

Both field types let editors pick from a predefined list of values. You create the enumeration or taxonomy in the schema first, and then add a field that uses it to any model.

  • Enumeration: A flat, predefined list of values. Editors select a value from a dropdown. See Enumerations.
  • Taxonomy: A group of terms arranged in a hierarchy. Editors select a node from the hierarchy, and the API returns both the selected node and the full path to it. See Taxonomies.

If you allow multiple values for an enumeration or taxonomy field, you cannot mark the field as required.

#Components

A component is a reusable group of fields. You define the fields once, and editors fill them with different content each time the component is used in an entry. There are two component field types, and both support multiple values:

  • Basic component field: Allows only one component type.
  • Modular component field: Allows two or more component types. Editors choose which component to add.

The Studio UI shows up to 50 component instances per entry, and you can nest components up to 4 levels deep. See Components.

#References

References connect entries across models. When you add a reference field, you choose:

  • Reference type: Whether the field references one model, or multiple models (Union Type).
  • Reference direction: One-way, where the field is queryable only from the model you add it to, or two-way, where Hygraph adds a reverse field to the referenced model. Union Type references are always two-way.
  • Relation cardinality: How many entries can be connected on each side, such as one to many or many to many.

See References to set up a reference field, and Components or references to decide which one fits your content.

#Remote fields

Remote fields fetch data from a remote source and connect it to an entry of the model. Each remote field is related to a single remote source and a single custom type. You must configure the remote source on your project before you can add a remote field.

  • REST remote field: Sends a GET or POST request to a path you specify on a REST remote source.
  • GraphQL remote field: Sends a GET or POST request to a GraphQL remote source, using the query entry point you select.

See Remote content to add a remote field, and Remote Fields vs Top-level Remote Fields to choose between them.

#Configure fields

After you choose a field type, you can configure how the field behaves. Not all options are available for all field types. Common options include:

  • Allow multiple values: Returns an array to the API. You can only set this when you create the field.
  • Localize field: Allows translations for each locale in your project.
  • Make field required and Set field as unique: Validate entries before they can be saved.
  • Field visibility: Controls whether editors can see or edit the field in the UI. Field visibility has no relation to permissions or security.
  • Conditional visibility: Shows a field to editors only when they need it. See Conditional fields.

For every option, its constraints, and the matching Management SDK parameters, see Field configuration.