📖 Scenario: You are building a simple GraphQL API for a small library system. Each Book has exactly one Author, and each Author has exactly one Biography. You want to model these one-to-one relationships clearly in your GraphQL schema.
🎯 Goal: Create a GraphQL schema that defines the Book, Author, and Biography types with one-to-one relationships between them. You will start by defining the basic types, then add fields to link them, and finally complete the schema with the necessary queries.
📋 What You'll Learn
Define a Book type with fields id (ID!), title (String!), and author (Author).
Define an Author type with fields id (ID!), name (String!), and biography (Biography).
Define a Biography type with fields id (ID!) and text (String!).
Add a root Query type with a field book that returns a Book by id.
Ensure the one-to-one relationships are represented by linking the types correctly.
💡 Why This Matters
🌍 Real World
One-to-one relationships are common in real-world data models, such as a user profile linked to a single account or a product linked to a single detailed description.
💼 Career
Understanding how to model one-to-one relationships in GraphQL is essential for backend developers building APIs that reflect real-world data accurately and efficiently.
Progress0 / 4 steps
1
Define the basic types
Create the GraphQL types Book, Author, and Biography with the following fields exactly: Book has id: ID! and title: String!; Author has id: ID! and name: String!; Biography has id: ID! and text: String!.
GraphQL
Hint
Start by writing type Book { id: ID! title: String! } and similarly for Author and Biography.
2
Add one-to-one relationship fields
Add the field author: Author to the Book type and the field biography: Biography to the Author type to represent the one-to-one relationships.
Create a root Query type with a field book that takes an argument id: ID! and returns a Book.
GraphQL
Hint
Define type Query { book(id: ID!): Book } to allow fetching a book by its ID.
4
Complete the schema with one-to-one relationships
Ensure the full schema includes the Book, Author, Biography types with their one-to-one fields and the Query type as defined. The schema should be a complete GraphQL schema ready for use.
GraphQL
Hint
Make sure all types and fields are included exactly as specified to represent the one-to-one relationships.
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
Step 1: Understand relationship types
One-to-one means each item in one set matches exactly one item in another set.
Step 2: Apply to GraphQL context
In GraphQL, this means a field in one type points to exactly one item in another type.
Final Answer:
Each item in one type is linked to exactly one item in another type. -> Option A
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
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.
Step 2: Check syntax correctness
type User { profile: Profile } uses profile: Profile, which is correct syntax for one-to-one.
Final Answer:
type User { profile: Profile } -> Option D
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?
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
Step 1: Check field type for one-to-one
One-to-one means a single object, not a list. Using [Profile] means many profiles.
Step 2: Confirm correct syntax
Field should be profile: Profile without brackets for one-to-one.
Final Answer:
Using list syntax [Profile] for one-to-one relationship is incorrect. -> Option B
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
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.
Step 2: Validate mutual references
Each Author has one Biography! and each Biography has one Author!, enforcing one-to-one.
Final Answer:
type Author { id: ID! name: String biography: Biography! } type Biography { id: ID! text: String author: Author! } -> Option C
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 unknownstrongwhile earning +1 Grip toward your certificate.