0
0
GraphQLquery~10 mins

Resolver organization 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 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'
Aundefined
B'users'
Cnull
D[{ id: 1, name: 'Alice' }, { id: 2, name: 'Bob' }]
Attempts:
3 left
💡 Hint
Common Mistakes
Returning a string instead of an array.
Returning null or undefined which causes errors.
2fill in blank
medium

Complete 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'
Ausers.find(user => user.id === id)
Busers[id]
Cusers.filter(user => user.id !== id)
Did
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.
3fill in blank
hard

Fix 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'
A*
B+ ' ' +
C-
D/
Attempts:
3 left
💡 Hint
Common Mistakes
Using arithmetic operators that cause NaN or errors.
Forgetting the space between names.
4fill in blank
hard

Fill 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'
AauthorId
Bauthor
Did
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong field names that don't exist on post.
Comparing to the wrong variable.
5fill in blank
hard

Fill 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'
Auser
C>
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variable names inconsistently.
Using the wrong comparison operator.