Bird
Raised Fist0
GraphQLquery~20 mins

Resolver organization in GraphQL - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
GraphQL Resolver Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
What is the main purpose of organizing resolvers in GraphQL?

Why do developers organize resolvers in a structured way when building GraphQL APIs?

ATo improve code readability and maintainability by grouping related resolvers together
BTo increase the size of the GraphQL schema automatically
CTo prevent clients from querying the API
DTo make the API slower by adding more layers of abstraction
Attempts:
2 left
💡 Hint

Think about how organizing code helps when the project grows larger.

❓ query_result
intermediate
2:00remaining
What is the output of this resolver organization pattern?

Given the following resolver structure, what will be the result of querying user.name?

GraphQL
const resolvers = {
  Query: {
    user: () => ({ id: 1, name: 'Alice' })
  },
  User: {
    name: (parent) => parent.name.toUpperCase()
  }
};
A"Alice"
B"ALICE"
Cnull
DError: name resolver missing
Attempts:
2 left
💡 Hint

Look at how the User.name resolver modifies the name.

📝 Syntax
advanced
2:00remaining
Identify the syntax error in this resolver organization code

Which option contains the syntax error in organizing resolvers?

GraphQL
const resolvers = {
  Query: {
    user: () => ({ id: 1, name: 'Bob' })
  },
  User: {
    name: (parent) => parent.name.toLowerCase()
  }
};
Aconst resolvers = { Query: { user: () => ({ id: 1, name: 'Bob' }) }, User: { name: (parent) => parent.name.toLowerCase() } };
Bconst resolvers = { Query: { user: () => ({ id: 1, name: 'Bob' }) }, User: { name: parent => parent.name.toLowerCase() } };
Cconst resolvers = { Query: { user: () => ({ id: 1, name: 'Bob' }) }, User: { name: (parent) => parent.name.toLowerCase() }
D;} } )(esaCrewoLot.eman.tnerap >= )tnerap( :eman { :resU ,} )} 'boB' :eman ,1 :di {( >= )( :resu { :yreuQ { = srevloser tsnoc
Attempts:
2 left
💡 Hint

Check for missing brackets or punctuation.

❓ optimization
advanced
2:00remaining
Which resolver organization improves performance by avoiding redundant data fetching?

Consider these resolver organizations. Which one best avoids fetching the same data multiple times?

AResolvers that cache data on the client side only
BSeparate resolvers for each field that each fetch data independently from the database
CResolvers that fetch data only when the client requests the entire schema
DA single resolver for the parent type that fetches all needed data once and passes it down
Attempts:
2 left
💡 Hint

Think about how to reduce repeated database calls.

🔧 Debug
expert
3:00remaining
Why does this resolver organization cause a 'Cannot read property' error?

Given this resolver code, why does querying post.author.name cause an error?

GraphQL
const resolvers = {
  Query: {
    post: () => ({ id: 1, authorId: 2 })
  },
  Post: {
    author: (parent) => getUserById(parent.authorId)
  },
  User: {
    name: (parent) => parent.name.toUpperCase()
  }
};

function getUserById(id) {
  return { id, /* missing name property */ };
}
ABecause getUserById returns an object without a 'name' property, so 'parent.name' is undefined
BBecause the 'author' resolver is missing a return statement
CBecause the 'name' resolver should not use 'parent' as argument
DBecause the 'post' resolver returns an object without 'author' property
Attempts:
2 left
💡 Hint

Check what data the author resolver returns and what the name resolver expects.

Practice

(1/5)
1. What is the main purpose of resolvers in a GraphQL server?
easy
A. To connect schema fields to the actual data sources
B. To define the GraphQL schema types and fields
C. To handle client-side state management
D. To style the GraphQL playground interface

Solution

  1. Step 1: Understand the role of resolvers

    Resolvers are functions that fetch or compute the data for each field in the GraphQL schema.
  2. Step 2: Identify what resolvers connect

    Resolvers connect the schema fields to the actual data sources like databases or APIs.
  3. Final Answer:

    To connect schema fields to the actual data sources -> Option A
  4. Quick Check:

    Resolvers = Connect schema to data [OK]
Hint: Resolvers link schema fields directly to data sources [OK]
Common Mistakes:
  • Confusing resolvers with schema definitions
  • Thinking resolvers handle UI or styling
  • Assuming resolvers manage client state
2. Which of the following is the correct way to organize resolvers in a GraphQL server?
easy
A. Group resolvers by Query, Mutation, and type names
B. Put all resolvers in a single flat list without grouping
C. Write resolvers only inside the schema definition file
D. Use one resolver function for the entire schema

Solution

  1. Step 1: Recognize common resolver organization

    Resolvers are typically grouped by operation types: Query, Mutation, and by object types for clarity.
  2. Step 2: Evaluate the options

    Grouping by Query, Mutation, and types helps maintain and scale the server easily.
  3. Final Answer:

    Group resolvers by Query, Mutation, and type names -> Option A
  4. Quick Check:

    Organize resolvers by operation type [OK]
Hint: Group resolvers by Query, Mutation, and types for clarity [OK]
Common Mistakes:
  • Putting all resolvers in one list without grouping
  • Mixing resolver code inside schema files
  • Using a single resolver for all fields
3. Given this resolver structure:
const resolvers = {
  Query: {
    user: () => ({ id: 1, name: "Alice" })
  },
  User: {
    fullName: (parent) => parent.name + " Smith"
  }
};
What will be the result of querying { user { fullName } }?
medium
A. Error: Resolver for fullName missing
B. { "user": { "fullName": "Smith" } }
C. { "user": { "fullName": null } }
D. { "user": { "fullName": "Alice Smith" } }

Solution

  1. Step 1: Understand the Query resolver

    The Query resolver returns a user object with id 1 and name "Alice".
  2. Step 2: Understand the User type resolver

    The User resolver for fullName appends " Smith" to the parent's name, resulting in "Alice Smith".
  3. Final Answer:

    { "user": { "fullName": "Alice Smith" } } -> Option D
  4. Quick Check:

    fullName = parent.name + " Smith" [OK]
Hint: User resolver uses parent data to build fullName [OK]
Common Mistakes:
  • Forgetting to use parent argument in field resolver
  • Expecting fullName to be directly in Query resolver
  • Assuming missing resolver causes error
4. Consider this resolver code snippet:
const resolvers = {
  Query: {
    posts: () => fetchPosts()
  },
  Post: {
    author: () => fetchAuthor()
  }
};
What is the main problem with this resolver organization?
medium
A. The schema is missing a Subscription resolver
B. The Post.author resolver ignores the parent post and fetches unrelated author data
C. Resolvers must be async functions, but these are not
D. The Query.posts resolver should be inside Mutation

Solution

  1. Step 1: Analyze Post.author resolver

    The author resolver does not use the parent post argument, so it cannot fetch the correct author for each post.
  2. Step 2: Understand resolver context

    Field resolvers should use the parent argument to access related data; ignoring it causes wrong or unrelated data.
  3. Final Answer:

    The Post.author resolver ignores the parent post and fetches unrelated author data -> Option B
  4. Quick Check:

    Use parent argument in nested resolvers [OK]
Hint: Nested resolvers must use parent to access related data [OK]
Common Mistakes:
  • Ignoring parent argument in nested resolvers
  • Misplacing Query resolvers inside Mutation
  • Assuming all resolvers must be async
5. You want to organize resolvers for a GraphQL server with Queries, Mutations, and a custom type Book. Which structure below best supports maintainability and scalability?
hard
A. const resolvers = { Query: {...}, Mutation: {...} }; // no Book resolvers
B. const resolvers = { allResolvers: {...} };
C. const resolvers = { Query: {...}, Mutation: {...}, Book: {...} };
D. const resolvers = { Book: {...} }; // no Query or Mutation

Solution

  1. Step 1: Identify best practice for resolver organization

    Grouping resolvers by operation type (Query, Mutation) and by custom types (Book) keeps code clear and modular.
  2. Step 2: Compare options

    const resolvers = { Query: {...}, Mutation: {...}, Book: {...} }; clearly separates concerns and allows easy updates and scaling.
  3. Final Answer:

    const resolvers = { Query: {...}, Mutation: {...}, Book: {...} }; -> Option C
  4. Quick Check:

    Separate resolvers by operation and type [OK]
Hint: Group resolvers by Query, Mutation, and types for best structure [OK]
Common Mistakes:
  • Putting all resolvers in one object without grouping
  • Omitting resolvers for custom types
  • Ignoring Mutation resolvers
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard