0
0
GraphQLquery~20 mins

Nested field queries in GraphQL - Practice Problems & Coding Challenges

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