Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to define a resolver for the Query type that returns a list of users.
GraphQL
const resolvers = {
Query: {
users: () => [1]
}
}; Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Returning a string instead of an array.
Returning null or undefined which causes errors.
✗ Incorrect
The resolver for 'users' should return an array of user objects. Option D correctly returns a list of user objects.
2fill in blank
mediumComplete the resolver to fetch a user by ID from a data source.
GraphQL
const resolvers = {
Query: {
user: (_, { id }) => [1]
}
}; Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using filter which returns an array instead of a single object.
Accessing users by index which may not match the id.
✗ Incorrect
To find a user by ID, use the find method to locate the user object with matching id.
3fill in blank
hardFix the error in the resolver that returns the user's full name by combining first and last names.
GraphQL
const resolvers = {
User: {
fullName: (user) => user.firstName [1] user.lastName
}
}; Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using arithmetic operators that cause NaN or errors.
Forgetting the space between names.
✗ Incorrect
To combine strings with a space, use the + operator with ' ' in between.
4fill in blank
hardFill both blanks to create a resolver that returns posts filtered by author ID.
GraphQL
const resolvers = {
Query: {
postsByAuthor: (_, { authorId }) => posts.filter(post => post.[1] === [2])
}
}; Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong field names that don't exist on post.
Comparing to the wrong variable.
✗ Incorrect
The post object has an 'authorId' field, so filter by post.authorId equal to the argument authorId.
5fill in blank
hardFill all three blanks to write a resolver that returns a map of user IDs to their email addresses for users older than 18.
GraphQL
const resolvers = {
Query: {
adultUserEmails: () => {
return users.reduce((acc, [1]) => {
if ([2].age [3] 18) {
acc[[1].id] = [1].email;
}
return acc;
}, {});
}
}
}; Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variable names inconsistently.
Using the wrong comparison operator.
✗ Incorrect
The variable name for each user is 'user'. The condition checks if user.age is greater than 18. So blanks are: variable name 'user', variable name 'user', and operator '>'.