Bird
0
0

How should you define the types to enforce this one-to-one relationship?

hard📝 Application Q8 of 15
GraphQL - Type Relationships
You are designing a GraphQL schema where each Student has exactly one Locker, and each Locker is assigned to exactly one Student. How should you define the types to enforce this one-to-one relationship?
Atype Student { id: ID! locker: Locker! } type Locker { id: ID! student: Student! }
Btype Student { id: ID! lockers: [Locker]! } type Locker { id: ID! student: Student }
Ctype Student { id: ID! locker: Locker } type Locker { id: ID! }
Dtype Student { id: ID! lockerId: ID! } type Locker { id: ID! studentId: ID! }
Step-by-Step Solution
Solution:
  1. Step 1: Identify one-to-one enforcement

    Both sides must reference each other with non-nullable fields to enforce exactly one relation.
  2. Step 2: Evaluate options

    type Student { id: ID! locker: Locker! } type Locker { id: ID! student: Student! } uses non-nullable fields on both types, enforcing one-to-one. type Student { id: ID! lockers: [Locker]! } type Locker { id: ID! student: Student } uses lists or nullable fields, allowing multiple or optional relations. type Student { id: ID! locker: Locker } type Locker { id: ID! } omits back reference. type Student { id: ID! lockerId: ID! } type Locker { id: ID! studentId: ID! } uses scalar IDs without object references.
  3. Final Answer:

    type Student { id: ID! locker: Locker! } type Locker { id: ID! student: Student! } -> Option A
  4. Quick Check:

    Non-nullable mutual references enforce one-to-one [OK]
Quick Trick: Use non-nullable mutual object references for one-to-one [OK]
Common Mistakes:
  • Using lists instead of single object fields
  • Making fields nullable, allowing zero or many
  • Using scalar IDs instead of object types

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More GraphQL Quizzes