Hygraph
Docs

#Lesson 4.1 - Build components

In this lesson, you will build all components the project needs. These components must exist before you can attach them to models in Lesson 4.2.

The Related Products component enables product pages to display other products a buyer might be interested in. It contains a read-only title field and a reference field that connects to other Product entries.

Add Related Products component field
  1. In the Schema editor, click +Add next to Components.

  2. Provide the following information, and click Add Component to save:

    FieldInput
    Display NameRelated Products
    API IDRelatedProduct
    Plural API IDRelatedProducts
  3. Add the following fields to the Related Products component:

    Single line text - Title:

    FieldInput
    Display nameTitle
    Use as title fieldSelect checkbox
    Make field requiredSelect checkbox
    Set initial valueSelect checkbox, and enter Related Products in the initial value field
    Field visibilityRead only

    The read-only title with an initial value ensures every instance of this component displays the same heading without editors being able to change it.

    Reference - Products:

    FieldInput
    Reference typeAllow only one model to be referenced
    Model to referenceProduct
    Reference directionsOne-way reference
    Allow multiple Products per RelatedProductSelect checkbox
    Display nameProducts
    API IDproducts
    DescriptionAdd 4 related products here
    Field visibilityRead / Write
  4. Click Add to save.

Your Related Products component should look like this:

Related Products componentRelated Products component

#Product type components

The store sells four product types: clothing, shoes, accessories, and decor. Each type requires different fields. Rather than creating four separate Product models, you will use four components, one per product type, and nest them inside a modular Product Variant component. The content editor then presents only the fields relevant to the selected product type.

These four components use the enumerations created in Lesson 3.1.

#Clothing

  1. Click +Add next to Components.

  2. Provide the following information, and click Add Component to save:

    FieldInput
    Display NameClothing
    API IDClothing
    Plural API IDClothingItems
  3. Add the following fields:

    Enumeration - Size:

    FieldInput
    Display nameSize
    API IDAuto-completed. Leave as is.
    EnumerationClothes size
    Make field requiredSelect checkbox

    Enumeration - Color:

    FieldInput
    Display nameColor
    EnumerationWearable items color
    Make field requiredSelect checkbox

Your Clothing component should look like this:

Clothing componentClothing component

#Shoes

  1. Click +Add next to Components.

  2. Provide the following information, and click Add Component to save:

    FieldInput
    Display NameShoes
    API IDShoe
    Plural API IDShoes
  3. Add the following fields:

    Enumeration - Size:

    FieldInput
    Display nameSize
    EnumerationShoes size
    Make field requiredSelect checkbox

    Enumeration - Color:

    FieldInput
    Display nameColor
    EnumerationWearable items color
    Make field requiredSelect checkbox

Your Shoes component should look like this:

Shoes componentShoes component

#Accessories

  1. Click +Add next to Components.

  2. Provide the following information, and click Add Component to save:

    FieldInput
    Display NameAccessories
    API IDAccessory
    Plural API IDAccessories
  3. Add the following field:

    Enumeration - Color:

    FieldInput
    Display nameColor
    EnumerationWearable items color
    Make field requiredSelect checkbox

Your Accessories component should look like this:

Accessories componentAccessories component

#Decor

  1. Click +Add next to Components.

  2. Provide the following information, and click Add Component to save:

    FieldInput
    Display NameDecor
    API IDDecor
    Plural API IDDecorItems
  3. Add the following field:

    Enumeration - Color:

    FieldInput
    Display nameColor
    EnumerationDecor items color
    Make field requiredSelect checkbox

Your Decor component should look like this:

Decor componentDecor component

#Product Variant

The Product Variant component contains a modular component field that presents Clothing, Shoes, Accessories, or Decor based on the product type an editor selects. This is the component that makes a single Product model work for all four product types.

  1. Click +Add next to Components.

  2. Provide the following information, and click Add Component to save:

    FieldInput
    Display NameProduct Variant
    API IDProductVariant
    Plural API IDProductVariants
  3. Add the following field:

    Modular component - Product type:

    FieldInput
    Display nameProduct type
    API IDproductType
    DescriptionSelect the product type to reveal fields
    Select allowed componentsClothing, Shoes, Accessories, Decor
  4. Click Add to save.

Your Product Variant component should look like this:

Product Variant componentProduct Variant component

#Button

The Button component holds a text label and a URL. It is used inside the Call to Action component in the next step.

  1. Click +Add next to Components.

  2. Provide the following information, and click Add to save:

    FieldInput
    Display NameButton
    API IDButton
    Plural API IDButtons
  3. Add the following fields:

    Single line text - Text:

    FieldInput
    Display nameText
    Localize fieldSelect checkbox

    Slug - URL:

    FieldInput
    Display nameURL
    Match a specific patternSelect checkbox, then select URL from the dropdown
    Custom error messageInput value does not match the expected format.

Your Button component should look like this:

Button componentButton component

#Call to Action

The Call to Action component is used as a page section on Landing pages. It embeds the Button component created in the previous step.

  1. Click +Add next to Components.

  2. Provide the following information, and click Add Component to save:

    FieldInput
    Display NameCall to Action
    API IDCallToAction
    Plural API IDCallToActions
  3. Add the following fields:

    Single line text - Heading:

    FieldInput
    Display nameHeading
    Localize fieldSelect checkbox

    Rich Text - Body:

    FieldInput
    Display nameBody
    Localize fieldSelect checkbox

    Asset picker - Image:

    FieldInput
    Display nameImage

    Basic component - Button:

    FieldInput
    Display nameButton
    Select componentButton

Your Call to Action component should look like this:

CTA componentCTA component

#Product Grid

The Product Grid component is used as a page section on Landing pages. It displays a headline, a description, and a set of Product references.

  1. Click +Add next to Components.

  2. Provide the following information, and click Add Component to save:

    FieldInput
    Display NameProduct Grid
    API IDProductGrid
    Plural API IDProductGrids
  3. Add the following fields:

    Single line text - Headline:

    FieldInput
    Display nameHeadline
    Localize fieldSelect checkbox

    Rich Text - Description:

    FieldInput
    Display nameDescription

    Reference - Products:

    FieldInput
    Reference typeAllow only one model to be referenced
    Model to referenceProduct
    Reference directionsOne-way reference
    Allow multiple Products per ProductGridSelect checkbox
    Display nameProducts
    API IDproducts
    Field visibilityRead / Write

Your Product Grid component should look like this:

Product Grid componentProduct Grid component

The Link component is included in the cloned project. It already contains two fields: Display text and External URL. You need to add a Reference field to it that connects it to the Blog post, Landing page, and Product models. This reference field could not be included in the original clone because it references models that did not exist until lesson 1.2.

This is the same layered schema pattern seen in lesson 1.2. Models and components are built in layers because some connections can only be made after the things they connect exist.

  1. In the Schema editor, under the Components section, select the Link component from the left sidebar.

  2. From the right sidebar, add a Reference field with the following information:

    FieldInput
    Reference typeAllow multiple models to be referenced
    Model to referenceBlog post, Landing page, Product
    Reference directionsOne-way reference
    Relation cardinalityTo one
    Display namePage
    API IDpage
    Field visibilityRead / Write
  3. Click Add to save.

Your finished Link component should look like this:

Link componentLink component

#What's next

Lesson 4.2 - Add components to your models

Or, go to the Tutorial overview for the full lesson list.