Hygraph
Docs

#Lesson 2.1 - Configure reference fields

In this lesson, you will add reference fields to the Product and Landing page models to connect them to other models in the schema. The reference type, direction, and cardinality configured here determine exactly what you will query later in the API Playground.

#Products and categories

Products need to be assignable to multiple categories, and categories need to display all their products. This requires a many-to-many, two-way reference. A two-way reference is required so you can query in both directions: from a product to its categories, and from a category to all its products.

Adding a reference field
  1. Open the Product model in the Schema editor and from the right sidebar, add a Reference field.

  2. Configure the reference field with the following information:

    Define relationship

    FieldInput
    Reference typeAllow only one model to be referenced
    Model to referenceProductCategory
    Reference directionsTwo-way reference
    Allow multiple Products per ProductCategorySelect checkbox
    Allow multiple ProductCategories per ProductSelect checkbox
    Relationship cardinalityThe graphic displays that the relationship is of type many-to-many

    Configure reference

    FieldInput
    Display namePre-configured. Leave as is.
    API IDPre-configured. Leave as is.
    DescriptionSelect the categories that apply to your product
    Field visibilityRead / Write

    Configure reverse field

    FieldInput
    Display namePre-configured. Leave as is.
    API IDPre-configured. Leave as is.
    Field visibilityRead / Write
  3. Click Add to save.

The reference field appears at the bottom of the model. Use the six-dot handle to drag it above the slug field.

Reorder reference field in schema

What this enables: In lesson 7, you will be able to query productCategory(where: { slug: "sportswear" }) { products { productName } }. The query returns all products within that category. This is possible because of the two-way direction configured here.

#Landing page references

The Landing page model needs to display featured blog posts, product categories, and seller information. Each of these is a one-way reference. The Landing page queries into those models, but those models do not need to query back into Landing pages.

  1. Open the Landing page model in the Schema editor and from the right sidebar under Relation, select Reference.

  2. Configure the reference field for the Blog post model with the following information:

    Define relationship

    FieldInput
    Reference typeAllow only one model to be referenced
    Model to referenceBlogPost
    Reference directionsOne-way reference
    Allow multiple BlogPosts per LandingPageSelect checkbox

    Configure reference

    FieldInput
    Display namePre-configured. Leave as is.
    API IDPre-configured. Leave as is.
    DescriptionFeatured item
    Field visibilityRead / Write
  3. Click Add to save.

  4. Add another Reference field from the right sidebar. Configure the reference field for the Product category model with the following information:

    Define relationship

    FieldInput
    Reference typeAllow only one model to be referenced
    Model to referenceProductCategory
    Reference directionsOne-way reference
    Allow multiple ProductCategories per LandingPageSelect checkbox

    Configure reference

    FieldInput
    Display namePre-configured. Leave as is.
    API IDPre-configured. Leave as is.
    DescriptionBrowse our categories
    Field visibilityRead / Write
  5. Click Add to save.

  6. Add another Reference field from the right sidebar. Configure the reference field for the Seller information model with the following information:

    Define relationship

    FieldInput
    Reference typeAllow only one model to be referenced
    Model to referenceSellerInformation
    Reference directionsOne-way reference
    Relation cardinalityTo one

    Configure reference

    FieldInput
    Display namePre-configured. Leave as is.
    API IDPre-configured. Leave as is.
    DescriptionBusiness information
    Field visibilityRead / Write
  7. Click Add to save.

Your Landing page model should now look like this:

Your Landing page model so farYour Landing page model so far

Why one-way for Landing page references, but two-way for Products and categories? A Blog post, Product category, or Seller information entry has no use case for querying which landing pages reference it. The two-way reference on Products and categories exists because the project needs to navigate from both sides. Where that bidirectional query isn't needed, a one-way reference keeps the schema clean.

#What's next

Lesson 3 - Enumerations

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