Why do developers organize resolvers in a structured way when building GraphQL APIs?
Think about how organizing code helps when the project grows larger.
Organizing resolvers helps developers find and update code easily, making the API easier to maintain and extend.
Given the following resolver structure, what will be the result of querying user.name?
const resolvers = { Query: { user: () => ({ id: 1, name: 'Alice' }) }, User: { name: (parent) => parent.name.toUpperCase() } };
Look at how the User.name resolver modifies the name.
The User.name resolver takes the original name and converts it to uppercase before returning it.
Which option contains the syntax error in organizing resolvers?
const resolvers = { Query: { user: () => ({ id: 1, name: 'Bob' }) }, User: { name: (parent) => parent.name.toLowerCase() } };
Check for missing brackets or punctuation.
Option C is missing the closing brace for the resolvers object, causing a syntax error.
Consider these resolver organizations. Which one best avoids fetching the same data multiple times?
Think about how to reduce repeated database calls.
Fetching all data once in the parent resolver and passing it down avoids multiple database calls, improving performance.
Given this resolver code, why does querying post.author.name cause an error?
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 */ }; }
Check what data the author resolver returns and what the name resolver expects.
The getUserById function returns an object missing the name property. When the name resolver tries to access parent.name, it is undefined, causing an error.