Bird
Raised Fist0
GraphQLquery~20 mins

Nested field queries in GraphQL - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Nested Query 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 nested GraphQL query?

Given the following GraphQL schema snippet:

type Author {
  id: ID!
  name: String!
  posts: [Post!]!
}

type Post {
  id: ID!
  title: String!
  comments: [Comment!]!
}

type Comment {
  id: ID!
  content: String!
}

And the query:

{
  author(id: "1") {
    name
    posts {
      title
      comments {
        content
      }
    }
  }
}

Assuming the author with id "1" has 1 post titled "Hello World" with 2 comments "Nice post!" and "Thanks for sharing", what is the expected JSON output?

GraphQL
{
  author(id: "1") {
    name
    posts {
      title
      comments {
        content
      }
    }
  }
}
A{"data":{"author":{"name":"Alice","posts":[{"title":"Hello World"}]}}}
B{"data":{"author":{"name":"Alice","posts":[{"title":"Hello World","comments":[{"content":"Thanks for sharing"}]}]}}}
C{"data":{"author":{"name":"Alice","posts":[{"title":"Hello World","comments":[{"content":"Nice post!"},{"content":"Thanks for sharing"}]}]}}}
D{"data":{"author":{"name":"Alice","posts":[]}}}
Attempts:
2 left
💡 Hint

Remember that nested fields return all requested subfields for each item.

📝 Syntax
intermediate
1:30remaining
Which GraphQL query syntax is correct for nested fields?

Choose the syntactically valid GraphQL query to fetch an author's name and the titles of their posts.

A{ author(id: "1") { name, posts { title } } }
B{ author(id: "1") { name posts { title } } }
C{ author(id: "1") { name; posts { title } } }
D{ author(id: "1") { name posts: { title } } }
Attempts:
2 left
💡 Hint

GraphQL queries do not use commas or semicolons between fields.

❓ optimization
advanced
2:00remaining
How to optimize a nested GraphQL query to reduce data size?

Given a query fetching authors with their posts and comments, which modification reduces the data size returned?

{
  authors {
    id
    name
    posts {
      id
      title
      comments {
        id
        content
      }
    }
  }
}
AFetch only comment ids, not content.
BRemove fetching comments field entirely.
CFetch only author names without posts.
DFetch posts without ids.
Attempts:
2 left
💡 Hint

Consider which fields are necessary for your use case to minimize data transfer.

🔧 Debug
advanced
2:00remaining
Why does this nested GraphQL query return null for posts?

Given the query:

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

The response is:

{
  "data": {
    "author": {
      "name": "Bob",
      "posts": null
    }
  }
}

What is the most likely cause?

APosts field is misspelled in the query.
BSyntax error in the query causing posts to be null.
CServer does not support nested queries.
DAuthor with id "2" has no posts and posts field is nullable.
Attempts:
2 left
💡 Hint

Null in nested fields often means no data or nullable field.

🧠 Conceptual
expert
1:30remaining
What is the main benefit of nested field queries in GraphQL?

Choose the best explanation for why nested field queries are useful in GraphQL.

AThey allow fetching related data in a single request, reducing multiple round-trips.
BThey enforce strict schema validation on the server side.
CThey automatically cache all nested data on the client.
DThey prevent any data from being fetched unless explicitly authorized.
Attempts:
2 left
💡 Hint

Think about how nested queries affect network requests.

Practice

(1/5)
1. What is the main purpose of using nested field queries in GraphQL?
easy
A. To fetch related data in a single request
B. To update multiple records at once
C. To delete nested data automatically
D. To create new nested fields in the schema

Solution

  1. Step 1: Understand nested queries

    Nested queries allow fetching related data together, reducing multiple requests.
  2. Step 2: Compare options

    Only To fetch related data in a single request describes fetching related data in one request, which is the key benefit.
  3. Final Answer:

    To fetch related data in a single request -> Option A
  4. Quick Check:

    Nested queries = fetch related data [OK]
Hint: Nested queries fetch related data together in one request [OK]
Common Mistakes:
  • Thinking nested queries update or delete data
  • Confusing nested queries with schema creation
  • Assuming nested queries only fetch top-level fields
2. Which of the following is the correct syntax to query a nested field author inside book in GraphQL?
easy
A. { book > author { name } }
B. { book { author { name } } }
C. { book: { author { name } } }
D. { book.author { name } }

Solution

  1. Step 1: Recall GraphQL nested syntax

    Nested fields are queried using curly braces inside parent fields without dots or colons.
  2. Step 2: Check each option

    { book { author { name } } } uses correct nested braces. Options A, B, and C use invalid syntax like dots, colons, or arrows.
  3. Final Answer:

    { book { author { name } } } -> Option B
  4. Quick Check:

    Nested fields use braces, not dots or colons [OK]
Hint: Use curly braces for nested fields, no dots or colons [OK]
Common Mistakes:
  • Using dot notation instead of braces
  • Adding colons inside query fields
  • Using arrows or other symbols for nesting
3. Given this GraphQL query:
{ user(id: 1) { name posts { title comments { text } } } }

What fields will be included in the response?
medium
A. User's name and comments' texts only
B. Only user's name and posts' titles
C. User's name, posts' titles, and comments' texts
D. Only posts' titles and comments' texts

Solution

  1. Step 1: Analyze the query structure

    The query requests user with id 1, then nested fields: name, posts with title, and comments with text inside posts.
  2. Step 2: Determine included fields

    All requested fields appear in the response: user's name, posts' titles, and comments' texts.
  3. Final Answer:

    User's name, posts' titles, and comments' texts -> Option C
  4. Quick Check:

    Nested query fields = all requested fields [OK]
Hint: Response includes all nested fields requested in braces [OK]
Common Mistakes:
  • Ignoring nested comments field
  • Assuming only top-level fields return
  • Confusing which fields are nested
4. Identify the error in this GraphQL query:
{ product { id name { first last } } }
medium
A. Missing parentheses after product
B. Field id cannot be queried with name
C. Curly braces are not allowed inside queries
D. Field name is treated as an object but should be scalar

Solution

  1. Step 1: Understand field types

    If name is a scalar (like string), it cannot have nested fields first and last.
  2. Step 2: Check query structure

    The query treats name as an object with nested fields, causing an error if schema defines it as scalar.
  3. Final Answer:

    Field name is treated as an object but should be scalar -> Option D
  4. Quick Check:

    Scalar fields cannot have nested fields [OK]
Hint: Only object fields can have nested fields; scalars cannot [OK]
Common Mistakes:
  • Assuming all fields can have nested fields
  • Adding parentheses unnecessarily
  • Misunderstanding query braces usage
5. You want to fetch a list of authors with their books and each book's reviews including rating and comment. Which query correctly nests these fields?
hard
A. { authors { name books { title reviews { rating comment } } } }
B. { authors { name books { title } reviews { rating comment } } }
C. { authors { name { books { title reviews { rating comment } } } } }
D. { authors { name books reviews { rating comment } } }

Solution

  1. Step 1: Understand nesting requirements

    Reviews belong to each book, so reviews must be nested inside books.
  2. Step 2: Check each option's nesting

    { authors { name books { title reviews { rating comment } } } } correctly nests reviews inside books. { authors { name books { title } reviews { rating comment } } } places reviews outside books. { authors { name { books { title reviews { rating comment } } } } } nests books inside name incorrectly. { authors { name books reviews { rating comment } } } misses nesting reviews inside books.
  3. Final Answer:

    { authors { name books { title reviews { rating comment } } } } -> Option A
  4. Quick Check:

    Nested fields must follow data hierarchy [OK]
Hint: Nest fields inside their parent objects to match data structure [OK]
Common Mistakes:
  • Placing nested fields at wrong levels
  • Nesting fields inside scalar fields
  • Missing braces for nested objects
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