Bird
Raised Fist0
GraphQLquery~10 mins

One-to-one relationships in GraphQL - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - One-to-one relationships
Define Type A
↓
Define Type B
↓
Add field in Type A referencing Type B
↓
Add field in Type B referencing Type A
↓
Query Type A with linked Type B
↓
Return combined data from both types
This flow shows how two GraphQL types are linked one-to-one by referencing each other, then queried together.
Execution Sample
GraphQL
type User {
  id: ID!
  profile: Profile
}

type Profile {
  id: ID!
  user: User
}

query {
  user(id: "1") {
    id
    profile {
      id
    }
  }
}
Defines User and Profile types linked one-to-one, then queries a user with their profile.
Execution Table
StepActionEvaluationResult
1Define User type with profile fieldUser has profile field of type ProfileUser and Profile types linked
2Define Profile type with user fieldProfile has user field of type UserBidirectional link established
3Query user with id '1'Find user with id '1'User object found
4Resolve profile field for userFetch profile linked to user '1'Profile object with id '101' found
5Return user data with profile idCombine user and profile data{ user: { id: "1", profile: { id: "101" } } }
6End of query executionAll requested fields resolvedQuery complete
💡 Query ends after all requested fields are resolved and data returned
Variable Tracker
VariableStartAfter Step 3After Step 4Final
userundefined{ id: "1" }{ id: "1", profile: { id: "101" } }{ id: "1", profile: { id: "101" } }
profileundefinedundefined{ id: "101" }{ id: "101" }
Key Moments - 2 Insights
Why do both User and Profile types reference each other?
Because in a one-to-one relationship, each side holds a reference to the other to allow querying from either type, as shown in execution_table steps 1 and 2.
What happens if the profile field is missing for a user?
The profile field would be null or undefined in the result, meaning no linked profile exists, as would be seen if step 4 returned no profile.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of 'profile' after step 4?
A{ id: '101' }
Bundefined
C{ id: '1' }
Dnull
💡 Hint
Check the 'profile' variable value in variable_tracker after step 4
At which step is the user with id '1' found?
AStep 2
BStep 4
CStep 3
DStep 5
💡 Hint
Look at the 'Action' and 'Evaluation' columns in execution_table for user lookup
If the profile field was removed from User type, what would happen in the query?
AQuery would fail at step 3
BProfile data would not be returned
CUser data would be missing
DQuery would return an error at step 5
💡 Hint
Consider how the profile field is resolved in step 4 and returned in step 5
Concept Snapshot
One-to-one relationships in GraphQL:
- Define two types referencing each other
- Each type has a field linking to the other
- Query one type and include the linked type's fields
- Result returns combined data from both linked types
- Enables easy navigation between related single objects
Full Transcript
This visual execution shows how one-to-one relationships work in GraphQL. First, two types, User and Profile, are defined with fields referencing each other. This creates a bidirectional link. Then a query requests a user by id and includes the user's profile data. The execution steps find the user, then resolve the linked profile, and finally return combined data. Variables track the user and profile objects as they are fetched. Key moments clarify why both types reference each other and what happens if linked data is missing. Quiz questions test understanding of variable values and query steps. The snapshot summarizes the key points for quick reference.

Practice

(1/5)
1. What does a one-to-one relationship in GraphQL represent?
easy
A. Each item in one type is linked to exactly one item in another type.
B. Each item in one type can link to many items in another type.
C. Items are not linked between types.
D. One item links to zero or many items in the same type.

Solution

  1. Step 1: Understand relationship types

    One-to-one means each item in one set matches exactly one item in another set.
  2. Step 2: Apply to GraphQL context

    In GraphQL, this means a field in one type points to exactly one item in another type.
  3. Final Answer:

    Each item in one type is linked to exactly one item in another type. -> Option A
  4. Quick Check:

    One-to-one = single matching pair [OK]
Hint: One-to-one means exactly one match per item [OK]
Common Mistakes:
  • Confusing one-to-one with one-to-many
  • Thinking items can link to multiple items
  • Assuming no links exist
2. Which of the following is the correct GraphQL syntax to define a one-to-one relationship between User and Profile types?
easy
A. type User { profile: String }
B. type User { profiles: [Profile] }
C. type User { profile: [Profile] }
D. type User { profile: Profile }

Solution

  1. Step 1: Identify one-to-one field type

    One-to-one means a single object, so the field should be of type Profile, not a list.
  2. Step 2: Check syntax correctness

    type User { profile: Profile } uses profile: Profile, which is correct syntax for one-to-one.
  3. Final Answer:

    type User { profile: Profile } -> Option D
  4. Quick Check:

    Single object field = one-to-one [OK]
Hint: Use single type, not list, for one-to-one fields [OK]
Common Mistakes:
  • Using list syntax for one-to-one
  • Using wrong field type like String
  • Missing colon or braces
3. Given the schema:
type User { id: ID! name: String profile: Profile }
type Profile { id: ID! bio: String }

And the query:
{ user(id: "1") { name profile { bio } } }

If user with id "1" has name "Alice" and profile bio "Loves cats", what is the expected output?
medium
A. { "data": { "user": { "name": "Alice", "profile": { "bio": "Loves cats" } } } }
B. { "data": { "user": { "name": "Alice", "profile": null } } }
C. { "data": { "user": { "name": "Alice" } } }
D. Error: profile field missing

Solution

  1. Step 1: Understand query fields

    The query requests user's name and profile's bio.
  2. Step 2: Match data to query

    User "Alice" has a profile with bio "Loves cats", so both fields return data.
  3. Final Answer:

    { "data": { "user": { "name": "Alice", "profile": { "bio": "Loves cats" } } } } -> Option A
  4. Quick Check:

    Query fields match data output [OK]
Hint: Returned data matches requested fields exactly [OK]
Common Mistakes:
  • Assuming profile is null when data exists
  • Omitting nested profile field
  • Expecting error when schema is correct
4. Identify the error in this GraphQL schema for one-to-one relationship:
type User { id: ID! profile: [Profile] }
type Profile { id: ID! user: User }
medium
A. User type must not have profile field.
B. Using list syntax [Profile] for one-to-one relationship is incorrect.
C. Missing exclamation mark after Profile.
D. Profile type cannot reference User.

Solution

  1. Step 1: Check field type for one-to-one

    One-to-one means a single object, not a list. Using [Profile] means many profiles.
  2. Step 2: Confirm correct syntax

    Field should be profile: Profile without brackets for one-to-one.
  3. Final Answer:

    Using list syntax [Profile] for one-to-one relationship is incorrect. -> Option B
  4. Quick Check:

    List means many, not one [OK]
Hint: No brackets for one-to-one fields [OK]
Common Mistakes:
  • Confusing one-to-one with one-to-many
  • Adding unnecessary exclamation marks
  • Thinking references are not allowed
5. You want to model a one-to-one relationship between Author and Biography in GraphQL. Which schema design ensures each author has exactly one biography and each biography belongs to exactly one author?
hard
A. type Author { id: ID! name: String biography: Biography } type Biography { id: ID! text: String }
B. type Author { id: ID! name: String biographies: [Biography]! } type Biography { id: ID! text: String author: Author }
C. type Author { id: ID! name: String biography: Biography! } type Biography { id: ID! text: String author: Author! }
D. type Author { id: ID! name: String biographyId: ID! } type Biography { id: ID! text: String }

Solution

  1. Step 1: Check for required one-to-one fields

    Both Author and Biography have non-nullable fields pointing to each other, ensuring exactly one match.
  2. Step 2: Validate mutual references

    Each Author has one Biography! and each Biography has one Author!, enforcing one-to-one.
  3. Final Answer:

    type Author { id: ID! name: String biography: Biography! } type Biography { id: ID! text: String author: Author! } -> Option C
  4. Quick Check:

    Mutual non-null fields = strict one-to-one [OK]
Hint: Use non-null mutual fields for strict one-to-one [OK]
Common Mistakes:
  • Using lists instead of single objects
  • Making fields nullable allowing missing links
  • Not linking both types mutually
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard