Bird
Raised Fist0
GraphQLquery~20 mins

Entity references in GraphQL - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Challenge - 5 Problems
🎖️
Entity Reference Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
What is the output of this GraphQL query with entity references?

Given the schema where Book has a reference to Author by ID, what does this query return?

{
  book(id: "1") {
    title
    author {
      name
    }
  }
}
GraphQL
type Author {
  id: ID!
  name: String!
}

type Book {
  id: ID!
  title: String!
  author: Author!
}

query {
  book(id: "1") {
    title
    author {
      name
    }
  }
}
A{"data":{"book":{"title":"GraphQL Basics","author":{"name":"Alice"}}}}
B{"data":{"book":{"title":"GraphQL Basics","author":null}}}
C{"data":{"book":null}}
DSyntaxError: Cannot query field "author" on type "Book".
Attempts:
2 left
💡 Hint

Remember that entity references allow nested queries to fetch related data.

🧠 Conceptual
intermediate
1:30remaining
Which statement correctly describes entity references in GraphQL?

Choose the correct description of how entity references work in GraphQL schemas.

AEntity references allow one type to include fields that link to another type, enabling nested queries.
BEntity references automatically duplicate data from one type into another to avoid joins.
CEntity references prevent querying nested objects to improve performance.
DEntity references are only used for scalar fields like strings and numbers.
Attempts:
2 left
💡 Hint

Think about how GraphQL lets you fetch related data in one query.

📝 Syntax
advanced
2:00remaining
Identify the syntax error in this GraphQL schema with entity references

Which option correctly fixes the syntax error in this schema snippet?

type Book {
  id: ID!
  title: String!
  author: Author
}

type Author {
  id: ID!
  name: String!
}

schema {
  query: Query
}

type Query {
  book(id: ID!): Book
}
GraphQL
type Book {
  id: ID!
  title: String!
  author: Author
}

type Author {
  id: ID!
  name: String!
}

type Query {
  book(id: ID!): Book
}

schema {
  query: Query
}
AChange 'schema { query: Query }' to 'schema { query: Query! }'.
BAdd exclamation mark to 'author: Author!' in Book type.
CNo syntax error; schema is valid.
DReplace 'book(id: ID!): Book' with 'book(id: ID!): [Book]'.
Attempts:
2 left
💡 Hint

Check if the schema declaration and types follow GraphQL syntax rules.

optimization
advanced
2:00remaining
How to optimize fetching entity references in GraphQL to reduce data size?

You want to fetch books and their authors but minimize data transfer. Which query modification best achieves this?

{
  books {
    title
    author {
      name
      biography
    }
  }
}
AReplace 'author' field with 'authorId' scalar to avoid nested query.
BUse fragments to fetch all author fields to avoid multiple queries.
CAdd 'limit: 10' argument to books query to fetch fewer books.
DRemove 'biography' field from author selection to fetch only 'name'.
Attempts:
2 left
💡 Hint

Think about selecting only the fields you need to reduce data size.

🔧 Debug
expert
2:30remaining
Why does this GraphQL query fail when fetching entity references?

Given this query:

{
  book(id: "2") {
    title
    author {
      name
    }
  }
}

And the schema where author is a non-nullable field, why might this query return an error?

GraphQL
type Book {
  id: ID!
  title: String!
  author: Author!
}

type Author {
  id: ID!
  name: String!
}

type Query {
  book(id: ID!): Book
}
AThe query syntax is invalid because 'author' cannot be nested inside 'book'.
BThe book with id "2" exists but its author reference is missing, causing a null error on a non-nullable field.
CThe schema does not define the 'author' type, causing a type error.
DThe 'book' query must return a list, not a single object.
Attempts:
2 left
💡 Hint

Consider what happens if a required field is missing data.

Practice

(1/5)
1. What is the main purpose of entity references in GraphQL?
easy
A. To create mutations for updating data
B. To define scalar types like Int and String
C. To connect one type to another and fetch related data
D. To write raw SQL queries inside GraphQL

Solution

  1. Step 1: Understand entity references

    Entity references link one GraphQL type to another, allowing related data to be fetched together.
  2. Step 2: Compare options

    Only To connect one type to another and fetch related data describes connecting types and fetching related data, which is the purpose of entity references.
  3. Final Answer:

    To connect one type to another and fetch related data -> Option C
  4. Quick Check:

    Entity references = connect types [OK]
Hint: Entity references link types to get related info fast [OK]
Common Mistakes:
  • Confusing entity references with scalar type definitions
  • Thinking entity references are for mutations
  • Assuming entity references are raw SQL queries
2. Which of the following is the correct way to define an entity reference in a GraphQL schema?
easy
A. type Book { author: Boolean }
B. type Book { author: String }
C. type Book { author: Int }
D. type Book { author: Author }

Solution

  1. Step 1: Identify entity reference syntax

    Entity references use another type's name as the field type, e.g., author: Author.
  2. Step 2: Check options

    Only type Book { author: Author } uses a type name (Author) as a field type, correctly defining an entity reference.
  3. Final Answer:

    type Book { author: Author } -> Option D
  4. Quick Check:

    Entity reference = field with another type name [OK]
Hint: Use type names, not scalars, for entity references [OK]
Common Mistakes:
  • Using scalar types instead of type names for references
  • Confusing field names with types
  • Missing curly braces in type definitions
3. Given the schema:
type Author { id: ID! name: String! } type Book { id: ID! title: String! author: Author }

What will the query { book { title author { name } } } return if the book's title is "GraphQL Guide" and the author's name is "Alice"?
medium
A. {"book": {"title": "GraphQL Guide", "author": "Alice"}}
B. {"book": {"title": "GraphQL Guide", "author": {"name": "Alice"}}}
C. {"book": {"title": "GraphQL Guide", "author": null}}
D. SyntaxError

Solution

  1. Step 1: Understand the query structure

    The query requests the book's title and the nested author's name, matching the schema's entity reference.
  2. Step 2: Predict the output

    The response will include the book title and an object for author with the name field, as in {"book": {"title": "GraphQL Guide", "author": {"name": "Alice"}}}.
  3. Final Answer:

    {"book": {"title": "GraphQL Guide", "author": {"name": "Alice"}}} -> Option B
  4. Quick Check:

    Nested entity reference returns nested object [OK]
Hint: Nested fields return nested objects, not strings [OK]
Common Mistakes:
  • Expecting author as a string instead of an object
  • Assuming null author when data exists
  • Confusing syntax errors with valid queries
4. Consider this schema snippet:
type Book { id: ID! title: String! author: Author }

and this query:
{ book { title author } }

Why will this query cause an error?
medium
A. Because author is an object type and requires subfields
B. Because title is missing
C. Because book is not defined
D. Because author should be a scalar type

Solution

  1. Step 1: Check field types in query

    The author field is an object type, so GraphQL requires specifying which subfields to fetch.
  2. Step 2: Identify error cause

    Querying author without subfields causes a validation error, as in Because author is an object type and requires subfields.
  3. Final Answer:

    Because author is an object type and requires subfields -> Option A
  4. Quick Check:

    Object fields need subfields in queries [OK]
Hint: Always specify subfields for object-type fields [OK]
Common Mistakes:
  • Querying object fields without subfields
  • Assuming scalar fields need subfields
  • Ignoring schema definitions
5. You have these types:
type User { id: ID! name: String! posts: [Post!]! } type Post { id: ID! content: String! author: User! }

How can you write a query to get each user's name and the content of their posts?
hard
A. { user { name posts { content } } }
B. { user { name posts } }
C. { user { posts { content } } }
D. { user { name content } }

Solution

  1. Step 1: Understand the schema relations

    User has a list of posts, each post has content. To get user name and posts content, query both fields with nested subfields.
  2. Step 2: Check query options

    { user { name posts { content } } } correctly queries user name and nested posts content. Others miss fields or subfields.
  3. Final Answer:

    { user { name posts { content } } } -> Option A
  4. Quick Check:

    Nested lists need subfields for content [OK]
Hint: Query nested lists with subfields for details [OK]
Common Mistakes:
  • Omitting subfields for list items
  • Missing user name field
  • Trying to query scalar fields as objects