0
0
GraphQLquery~20 mins

Parent (root) argument in GraphQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
GraphQL Parent Argument Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
Understanding the root parent argument in GraphQL resolver
Given the following GraphQL resolver snippet, what will be the value of the parent argument when resolving the user field in the root query?
GraphQL
const resolvers = {
  Query: {
    user(parent, args, context, info) {
      return { id: args.id, name: "Alice" };
    }
  }
};
AAn empty object {}
Bundefined
CThe root query object
Dnull
Attempts:
2 left
💡 Hint
Think about what the parent argument represents at the root level of a GraphQL query.
query_result
intermediate
2:00remaining
Parent argument value in nested GraphQL resolvers
Consider this GraphQL schema and resolver snippet. What will be the value of the parent argument when resolving the posts field inside the User type?
GraphQL
const resolvers = {
  Query: {
    user(parent, args) {
      return { id: 1, name: "Bob" };
    }
  },
  User: {
    posts(parent, args) {
      return [{ id: 101, title: "Hello" }];
    }
  }
};
AThe root query object
Bnull
Cundefined
DThe user object { id: 1, name: "Bob" }
Attempts:
2 left
💡 Hint
The parent argument in nested resolvers is the object returned by the parent field resolver.
📝 Syntax
advanced
2:00remaining
Identify the syntax error in resolver using parent argument
Which option contains a syntax error in the way the parent argument is used in this resolver function?
GraphQL
const resolvers = {
  User: {
    posts(parent, args) {
      return parent.posts;
    }
  }
};
Aposts(parent args) { return parent.posts; }
Bposts(parent, args) { return parent.posts; }
Cposts(parent, args) => { return parent.posts; }
Dposts: function(parent, args) { return parent.posts; }
Attempts:
2 left
💡 Hint
Check the function parameter list syntax carefully.
optimization
advanced
2:00remaining
Optimizing resolver usage of parent argument to avoid redundant database calls
Given a resolver for User.posts that fetches posts from the database, which option best uses the parent argument to avoid fetching the user again inside the posts resolver?
GraphQL
const resolvers = {
  Query: {
    user(parent, args) {
      return db.getUserById(args.id);
    }
  },
  User: {
    posts(parent, args) {
      // Which option below is best?
    }
  }
};
Areturn db.getPostsByUserId(args.userId);
Bconst user = db.getUserById(parent.id); return db.getPostsByUserId(user.id);
Creturn db.getPostsByUserId(parent.id);
Dreturn db.getPosts();
Attempts:
2 left
💡 Hint
Use the parent argument to get the user id directly.
🧠 Conceptual
expert
2:00remaining
Why is the parent argument null at the root level in GraphQL?
In GraphQL, the parent argument in resolvers is undefined at the root query level. Why is this the case?
ABecause the root query has no parent object above it in the query execution hierarchy.
BBecause the parent argument is only used for mutations, not queries.
CBecause the parent argument is deprecated and replaced by context.
DBecause the parent argument is always an empty object at the root.
Attempts:
2 left
💡 Hint
Think about the structure of a GraphQL query and how resolvers are called.