Bird
0
0

How do you design this relationship and avoid duplicate category entries when querying?

hard📝 Application Q9 of 15
GraphQL - Type Relationships
You have a GraphQL schema with types Product and Category. A product can belong to multiple categories, and categories can have multiple products. How do you design this relationship and avoid duplicate category entries when querying?
AUse many-to-many with lists and ensure unique IDs in resolver to prevent duplicates
BUse one-to-many with a single category per product
CUse one-to-one with a single category and product
DUse scalar fields for category names instead of relationships
Step-by-Step Solution
Solution:
  1. Step 1: Identify relationship type

    Multiple categories per product and vice versa is many-to-many.
  2. Step 2: Prevent duplicates

    Resolvers should return unique category IDs to avoid duplicate entries in queries.
  3. Final Answer:

    Use many-to-many with lists and ensure unique IDs in resolver to prevent duplicates -> Option A
  4. Quick Check:

    Many-to-many with unique IDs avoids duplicates [OK]
Quick Trick: Ensure unique IDs in many-to-many resolvers [OK]
Common Mistakes:
  • Using one-to-many incorrectly
  • Ignoring duplicates in resolver
  • Using scalar fields instead of relationships

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More GraphQL Quizzes