0
0
GraphQLquery~20 mins

Resolver organization in GraphQL - Practice Problems & Coding Challenges

Choose your learning style9 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.