0
0
GraphQLquery~20 mins

Mocking resolvers in GraphQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
GraphQL Mocking 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 mocked resolver query?

Given this GraphQL schema and mocked resolver, what will be the output of the query?

type Query {
  user(id: ID!): User
}

type User {
  id: ID!
  name: String
  age: Int
}

// Mock resolver for user
const mocks = {
  Query: () => ({
    user: (_parent, args) => ({
      id: args.id,
      name: "Alice",
      age: 30
    })
  })
};

query {
  user(id: "123") {
    id
    name
    age
  }
}
GraphQL
const mocks = {
  Query: () => ({
    user: (_parent, args) => ({
      id: args.id,
      name: "Alice",
      age: 30
    })
  })
};
A{ "data": { "user": { "id": "123", "name": "Alice", "age": 30 } } }
B{ "data": { "user": { "id": null, "name": "Alice", "age": 30 } } }
C{ "data": { "user": { "id": "123", "name": null, "age": 30 } } }
D{ "data": { "user": null } }
Attempts:
2 left
💡 Hint

Look at how the id is returned from the args in the resolver.

📝 Syntax
intermediate
2:00remaining
Which mocked resolver code snippet has a syntax error?

Identify the option that contains a syntax error in the mocked resolver definition.

Aconst mocks = { Query: () => ({ user: (_parent, args) => ({ id: args.id, name: "Bob" }) }) };
Bconst mocks = { Query: () => ({ user: (_parent, args) => { id: args.id, name: "Bob" } }) };
C;} )} )} "boB" :eman ,di.sgra :di {( >= )sgra ,tnerap_( :resu {( >= )( :yreuQ { = skcom tsnoc
Donst mocks = { Query: () => ({ user: (_parent, args) => ({ id: args.id, name: "Bob" }) }) };
Attempts:
2 left
💡 Hint

Check the arrow function syntax and object return syntax carefully.

optimization
advanced
2:00remaining
How to optimize mocking multiple similar resolvers?

You have many similar resolvers returning user data with different IDs. Which approach optimizes mocking these resolvers efficiently?

AMock only one user and return null for others.
BWrite separate resolver functions for each user ID manually.
CHardcode a single user object and ignore the <code>id</code> argument in all resolvers.
DUse a generic resolver function that returns user data based on the <code>id</code> argument dynamically.
Attempts:
2 left
💡 Hint

Think about how to reuse code and handle dynamic input.

🔧 Debug
advanced
2:00remaining
Why does this mocked resolver return null for a field?

Given this mocked resolver snippet, why does the email field return null in the query result?

const mocks = {
  User: () => ({
    id: "1",
    name: "Eve"
  })
};

query {
  user(id: "1") {
    id
    name
    email
  }
}
AThe <code>email</code> field is not defined in the mocked resolver, so it returns null by default.
BThe resolver returns an error causing the <code>email</code> field to be null.
CThe query is malformed and does not request the <code>email</code> field properly.
DThe schema does not include the <code>email</code> field in the User type.
Attempts:
2 left
💡 Hint

Check which fields the mock resolver explicitly returns.

🧠 Conceptual
expert
2:00remaining
What is the main benefit of mocking resolvers in GraphQL development?

Choose the best explanation for why developers use mocked resolvers during GraphQL API development.

ATo automatically generate database schemas from GraphQL queries.
BTo permanently replace the backend with static data for production use.
CTo simulate API responses without needing a real backend, enabling frontend development and testing independently.
DTo enforce strict type checking on client-side queries.
Attempts:
2 left
💡 Hint

Think about development workflow and decoupling frontend from backend.