0
0
GraphQLquery~20 mins

Resolver unit tests in GraphQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Resolver Unit Test Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
What is the output of this resolver unit test?

Given a GraphQL resolver that returns a list of users, what will be the result of this unit test?

const users = [{id: 1, name: "Alice"}, {id: 2, name: "Bob"}];

const resolver = {
  Query: {
    users: () => users
  }
};

// Test
const result = resolver.Query.users();
console.log(result.length);
GraphQL
const users = [{id: 1, name: "Alice"}, {id: 2, name: "Bob"}];

const resolver = {
  Query: {
    users: () => users
  }
};

const result = resolver.Query.users();
console.log(result.length);
A0
Bundefined
C2
D1
Attempts:
2 left
💡 Hint

Think about how many user objects are in the array returned by the resolver.

📝 Syntax
intermediate
2:00remaining
Which option causes a syntax error in this resolver test?

Identify which resolver unit test code snippet will cause a syntax error.

const resolver = {
  Query: {
    user: (parent, args) => {
      return {id: args.id, name: "Test User"};
    }
  }
};

// Test call
const result = resolver.Query.user(null, {id: 1});
console.log(result);
GraphQL
const resolver = {
  Query: {
    user: (parent, args) => {
      return {id: args.id, name: "Test User"};
    }
  }
};

const result = resolver.Query.user(null, {id: 1});
console.log(result);
Aconst result = resolver.Query.user(null, id: 1); console.log(result);
Bconst result = resolver.Query.user(null, {id: 1}); console.log(result);
C;)tluser(gol.elosnoc ;)}1 :di{ ,llun(resu.yreuQ.revloser = tluser tsnoc
Donst result = resolver.Query.user(null, {id: 1}); console.log(result);
Attempts:
2 left
💡 Hint

Look carefully at how the arguments are passed to the resolver function.

optimization
advanced
2:00remaining
Which option optimizes resolver unit test performance best?

You want to optimize a resolver unit test that fetches user data from a mock database. Which approach reduces test runtime most effectively?

AUse a mock function that returns a static user object immediately.
BCall the real database in each test to ensure data accuracy.
CUse a delayed promise to simulate network latency in tests.
DRun tests sequentially with real database calls.
Attempts:
2 left
💡 Hint

Think about what makes tests fast and reliable.

🔧 Debug
advanced
2:00remaining
Why does this resolver unit test fail?

Consider this resolver and test code:

const resolver = {
  Query: {
    user: (parent, args) => {
      if (!args.id) throw new Error("ID required");
      return {id: args.id, name: "User" + args.id};
    }
  }
};

// Test
try {
  resolver.Query.user(null, {});
} catch (e) {
  console.log(e.message);
}

What is the output of the test?

ATypeError
Bundefined
C"Userundefined"
D"ID required"
Attempts:
2 left
💡 Hint

Check what happens when args.id is missing.

🧠 Conceptual
expert
2:00remaining
What is the main benefit of mocking dependencies in resolver unit tests?

Why do developers mock external services or databases when writing resolver unit tests?

ATo ensure the resolver always connects to the real database.
BTo isolate the resolver logic and test it without external side effects.
CTo slow down tests and simulate real network delays.
DTo increase the complexity of tests by adding more dependencies.
Attempts:
2 left
💡 Hint

Think about what makes unit tests reliable and fast.