Bird
0
0

Which schema design correctly models this one-to-many relationship and allows querying all posts in a category?

hard📝 Application Q15 of 15
GraphQL - Type Relationships
You want to design a GraphQL schema for a blog where each Category has many Posts, and each Post belongs to one Category. Which schema design correctly models this one-to-many relationship and allows querying all posts in a category?
Atype Category { id: ID! name: String! post: Post } type Post { id: ID! title: String! category: Category } Query { posts: [Post!]! }
Btype Category { id: ID! name: String! posts: [Post!]! } type Post { id: ID! title: String! categoryId: ID! } Query { category(id: ID!): Category }
Ctype Category { id: ID! name: String! posts: Post } type Post { id: ID! title: String! categoryId: ID! } Query { category(id: ID!): Category }
Dtype Category { id: ID! name: String! posts: [Post] } type Post { id: ID! title: String! category: Category! } Query { category(id: ID!): Category }
Step-by-Step Solution
Solution:
  1. Step 1: Model one-to-many with list in Category

    Category should have posts as a list [Post!]! to represent many posts.
  2. Step 2: Use categoryId in Post to link back

    Post has categoryId to link to its single category, enabling queries from both sides.
  3. Step 3: Check Query type for fetching category

    Query allows fetching category by id, then posts can be queried from category.
  4. Final Answer:

    type Category { id: ID! name: String! posts: [Post!]! } type Post { id: ID! title: String! categoryId: ID! } Query { category(id: ID!): Category } -> Option B
  5. Quick Check:

    List in parent + foreign key in child = one-to-many [OK]
Quick Trick: Parent has list, child has foreign key ID for one-to-many [OK]
Common Mistakes:
  • Using single Post instead of list in Category
  • Missing foreign key in Post
  • Incorrect Query type for fetching data

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More GraphQL Quizzes