0
0
GraphQLquery~10 mins

Resolver chains in GraphQL - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to define a resolver that returns the user's name.

GraphQL
const resolvers = {
  Query: {
    user: (parent, args, context, info) => [1]
  }
};
Drag options to blanks, or click blank then click option'
Aparent.name
Bcontext.db.getUserName()
Cargs.id
Dinfo.fieldName
Attempts:
3 left
💡 Hint
Common Mistakes
Using args.id instead of parent.name
Trying to access context without it being passed
2fill in blank
medium

Complete the resolver chain to fetch the user's posts from the database.

GraphQL
const resolvers = {
  User: {
    posts: (parent, args, context) => [1]
  }
};
Drag options to blanks, or click blank then click option'
Aparent.posts
Bcontext.db.getPostsByUserId(parent.id)
Cargs.posts
Dcontext.posts
Attempts:
3 left
💡 Hint
Common Mistakes
Using parent.posts which may not be populated
Using args.posts which is incorrect
3fill in blank
hard

Fix the error in the resolver chain to correctly resolve the user's profile picture URL.

GraphQL
const resolvers = {
  User: {
    profilePicture: (parent, args, context) => {
      return context.storage.getUrl([1]);
    }
  }
};
Drag options to blanks, or click blank then click option'
Acontext.userId
Bargs.profilePicId
Cparent.id
Dparent.profilePicId
Attempts:
3 left
💡 Hint
Common Mistakes
Using args.profilePicId which is not passed
Using parent.id which is the user ID, not picture ID
4fill in blank
hard

Fill both blanks to create a resolver chain that fetches comments for a post and filters by author ID.

GraphQL
const resolvers = {
  Post: {
    comments: (parent, args, context) => {
      return context.db.getCommentsByPostId(parent.[1]).filter(comment => comment.[2] === args.authorId);
    }
  }
};
Drag options to blanks, or click blank then click option'
Aid
BpostId
CauthorId
DuserId
Attempts:
3 left
💡 Hint
Common Mistakes
Using parent.postId which does not exist
Filtering by comment.userId instead of authorId
5fill in blank
hard

Fill all three blanks to create a resolver chain that fetches a user's friends and maps their names.

GraphQL
const resolvers = {
  User: {
    friendsNames: async (parent, args, context) => {
      const friends = await context.db.getFriendsByUserId(parent.[1]);
      return friends.map(friend => friend.[2]).filter(name => name [3] '');
    }
  }
};
Drag options to blanks, or click blank then click option'
Aid
BuserId
Cname
D!==
Attempts:
3 left
💡 Hint
Common Mistakes
Using userId instead of id
Mapping friend.userId instead of friend.name
Using == instead of !== for filtering