Hygraph
Docs

#Lesson 2 - References

This section covers adding reference fields to the models built in lesson 1.2.

References connect models to each other. The connections you configure here are what make it possible to query a product and receive its categories in a single API call.

Without references, a product entry has no connection to its categories, and a landing page has no connection to its blog posts. This is why references are a critical part of the schema.

By the end of lesson 2.1, the following model relationships are configured:

  • Products connected to Product categories (many-to-many, two-way)
  • Landing page connected to Blog posts, Product categories, and Seller information (one-way)

#How references work

The way you configure reference fields determines the following:

Type controls which models can be referenced. Selecting a single model means only that model's entries can be connected. Selecting multiple models creates a union reference.

Direction controls which side of the relationship can be queried. A one-way reference allows querying only from the model where the field lives. A two-way reference adds a reverse field to the referenced model, so you can query from either side.

Cardinality controls how many entries can be connected. The combination of direction and cardinality produces four reference patterns:

PatternWhat it meansWhen to use it
One-to-oneOne entry connects to one entryA landing page linked to one seller information entry
One-to-manyOne entry connects to many entriesA landing page linked to multiple blog posts
Many-to-manyMany entries connect to many entriesProducts assigned to multiple categories
Many-to-oneMany entries connect to one entryMultiple landing pages linked to one seller information entry

The references created in this lesson impact the queries in lesson 7.

For the complete reference field documentation, see References.

#Lessons in this section

LessonWhat you will do
2.1 Configure reference fieldsAdd reference fields to the Product and Landing page models and configure their type, direction, and cardinality

#What's next

Lesson 2.1 - Configure reference fields

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