Bird
0
0

Given the following GraphQL schema and resolvers, what will be the output of the query { user { id name posts { title } } }?

medium📝 query result Q13 of 15
GraphQL - Resolvers
Given the following GraphQL schema and resolvers, what will be the output of the query { user { id name posts { title } } }?
type User {
  id: ID!
  name: String!
  posts: [Post!]!
}

type Post {
  title: String!
}

const resolvers = {
  User: {
    posts(parent) {
      return postsData.filter(post => post.userId === parent.id);
    }
  }
};

const postsData = [
  { title: "Post 1", userId: "1" },
  { title: "Post 2", userId: "2" },
  { title: "Post 3", userId: "1" }
];
A{ "user": { "id": "2", "name": "Bob", "posts": [{ "title": "Post 1" }, { "title": "Post 3" }] } }
B{ "user": { "id": "1", "name": "Alice", "posts": [{ "title": "Post 2" }] } }
C{ "user": { "id": "1", "name": "Alice", "posts": [{ "title": "Post 1" }, { "title": "Post 3" }] } }
DError: posts resolver missing parent argument
Step-by-Step Solution
Solution:
  1. Step 1: Understand parent argument usage in posts resolver

    The posts resolver uses parent.id to filter posts belonging to the user.
  2. Step 2: Match posts with user id "1"

    Posts with userId === "1" are "Post 1" and "Post 3".
  3. Final Answer:

    { "user": { "id": "1", "name": "Alice", "posts": [{ "title": "Post 1" }, { "title": "Post 3" }] } } -> Option C
  4. Quick Check:

    Parent.id filters posts correctly [OK]
Quick Trick: Parent holds user data to filter posts [OK]
Common Mistakes:
  • Ignoring parent.id and returning all posts
  • Mixing user IDs in filtering
  • Assuming posts resolver lacks parent argument

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More GraphQL Quizzes