#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.
-
Open the Product model in the Schema editor and from the right sidebar, add a Reference field.
-
Configure the reference field with the following information:
Define relationship
Field Input Reference type Allow only one model to be referenced Model to reference ProductCategory Reference directions Two-way reference Allow multiple Products per ProductCategory Select checkbox Allow multiple ProductCategories per Product Select checkbox Relationship cardinality The graphic displays that the relationship is of type many-to-many Configure reference
Field Input Display name Pre-configured. Leave as is. API ID Pre-configured. Leave as is. Description Select the categories that apply to your product Field visibility Read / Write Configure reverse field
Field Input Display name Pre-configured. Leave as is. API ID Pre-configured. Leave as is. Field visibility Read / Write -
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.
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.
-
Open the Landing page model in the Schema editor and from the right sidebar under Relation, select Reference.
-
Configure the reference field for the
Blog postmodel with the following information:Define relationship
Field Input Reference type Allow only one model to be referenced Model to reference BlogPost Reference directions One-way reference Allow multiple BlogPosts per LandingPage Select checkbox Configure reference
Field Input Display name Pre-configured. Leave as is. API ID Pre-configured. Leave as is. Description Featured item Field visibility Read / Write -
Click Add to save.
-
Add another Reference field from the right sidebar. Configure the reference field for the
Product categorymodel with the following information:Define relationship
Field Input Reference type Allow only one model to be referenced Model to reference ProductCategory Reference directions One-way reference Allow multiple ProductCategories per LandingPage Select checkbox Configure reference
Field Input Display name Pre-configured. Leave as is. API ID Pre-configured. Leave as is. Description Browse our categories Field visibility Read / Write -
Click Add to save.
-
Add another Reference field from the right sidebar. Configure the reference field for the
Seller informationmodel with the following information:Define relationship
Field Input Reference type Allow only one model to be referenced Model to reference SellerInformation Reference directions One-way reference Relation cardinality To one Configure reference
Field Input Display name Pre-configured. Leave as is. API ID Pre-configured. Leave as is. Description Business information Field visibility Read / Write -
Click Add to save.
Your Landing page model should now look like this:
Your 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 - EnumerationsOr, go to the Tutorial overview for the full lesson list.