0
0
GraphQLquery~20 mins

Entity references in GraphQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
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.