0
0
GraphQLquery~10 mins

Resolver unit tests 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 basic resolver function that returns a fixed string.

GraphQL
const resolvers = { Query: { hello: () => [1] } };
Drag options to blanks, or click blank then click option'
A"Hello, world!"
BHello, world!
Creturn "Hello, world!"
Dconsole.log("Hello, world!")
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting quotes around the string
Using console.log instead of returning a value
2fill in blank
medium

Complete the resolver to accept arguments and return the name argument.

GraphQL
const resolvers = { Query: { greet: (_, [1]) => name } };
Drag options to blanks, or click blank then click option'
Acontext
Bargs
C{ name }
Dinfo
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong parameter name
Not destructuring the argument object
3fill in blank
hard

Fix the error in the resolver that tries to return a user by id from a data source.

GraphQL
const resolvers = { Query: { user: (_, { id }, [1]) => [1].dataSources.userAPI.getUserById(id) } };
Drag options to blanks, or click blank then click option'
Ainfo
Bcontext
Cargs
Droot
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong parameter name for context
Trying to access dataSources from args
4fill in blank
hard

Fill both blanks to write a test that mocks the data source and checks the resolver output.

GraphQL
test('user resolver returns user data', async () => {
  const mockUser = { id: '1', name: 'Alice' };
  const context = { dataSources: { userAPI: { getUserById: jest.fn().mockResolvedValue([1]) } } } };
  const result = await resolvers.Query.user(null, { id: '1' }, [2]);
  expect(result).toEqual(mockUser);
});
Drag options to blanks, or click blank then click option'
AmockUser
Bcontext
CmockUser.id
Dnull
Attempts:
3 left
💡 Hint
Common Mistakes
Not passing context to the resolver
Mocking the wrong return value
5fill in blank
hard

Fill all three blanks to write a test that verifies the resolver throws an error when user is not found.

GraphQL
test('user resolver throws error if user not found', async () => {
  const context = { dataSources: { userAPI: { getUserById: jest.fn().mockResolvedValue([1]) } } } };
  await expect(resolvers.Query.user(null, { id: '2' }, [2])).rejects.toThrow([3]);
});
Drag options to blanks, or click blank then click option'
Anull
Bcontext
C"User not found"
Dundefined
Attempts:
3 left
💡 Hint
Common Mistakes
Not mocking null return value
Not passing context to resolver
Expecting wrong error message