Thuta Learning
GraphQL
ExercisesWeb Developmentbeginner

Exercise — Design an E-commerce Product Catalog Schema

What you'll walk away with

  • Explain the core ideas behind Exercise — Design an E-commerce Product Catalog Schema
  • Run the sample GraphQL query or code and verify its output
  • Apply the technique correctly to the Tutorial Platform and production scenarios

Build the mental model

This lesson is a design exercise, not runnable app code — it asks you to reapply schema and type-system ideas (lesson 3), relationships (lesson 8), enums/interfaces (lesson 9), input types (lesson 11), and pagination connections (lesson 13) to a fresh e-commerce domain. Deciding how to model relationships among products, categories, and reviews practices real, interview-style GraphQL schema-design skill.

Connect it to a real scenario

Mentally map the Tutorial Platform's course-catalog structure (tutorials/authors/categories) onto its e-commerce equivalent (products/sellers/categories) — model `Product↔Category` (many-to-many) the same way you modeled `Tutorial↔Category` earlier. Model reviews the way you modeled comments, adding a `rating: Int!` field.

Try the working example

graphql
type Product {
  id: ID!
  name: String!
  priceCents: Int!
  categories: [Category!]!
  reviews: [Review!]!
}

type Category {
  id: ID!
  name: String!
  products: [Product!]!
}

type Review {
  id: ID!
  rating: Int!
  body: String
  author: String!
}

# TODO: add Query fields for browsing products by category
# TODO: add a CreateReviewInput and a createReview mutation
You should see
You can design and write a schema modeling relationships among products, categories, and reviews.

5-minute try-it

Complete the starter schema above with a `Query` type (products list, product by id, category by id) and a `CreateReviewInput` plus `createReview` mutation. Also try applying the pagination connection pattern to the `products` list field.

One important caution

Designing `Product.category: Category!` as a single non-null field cannot express the real e-commerce requirement that a product belongs to many categories — use a list field when the relationship is many-to-many.

GraphQL — Schemas and TypesGraphQL

Easy traps

  • Designing `Product.category: Category!` as a single non-null field cannot express the real e-commerce requirement that a product belongs to many categories — use a list field when the relationship is many-to-many.
  • Validate sample queries and mutations on a local or test server with recoverable data before applying them to production.

Exercise

Complete the starter schema above with a `Query` type (products list, product by id, category by id) and a `CreateReviewInput` plus `createReview` mutation. Also try applying the pagination connection pattern to the `products` list field.

You'll know it worked when: You can design and write a schema modeling relationships among products, categories, and reviews.