Search⌘ K
AI Features

How GraphQL Resolvers Work?

Learn how to implement and customize GraphQL resolvers in Apollo Server. Understand how resolvers fetch nested data, override default trivial resolution, and enable complex object graphs for your back-end API.

How are resolvers used?

To see how Apollo Server uses our resolvers, let’s add log statements to the Query.allProducts and Product.author resolvers and run a GraphQL query. Then we’ll see how and when Apollo Server uses them.

To do this, we’ll add console.log statements in both of our resolvers.

const resolvers = {
Query: {
appName: () => 'ProductHunt clone',
allProducts: () => {
console.log('Query.allProducts')
return productsData
},
},
Product: {
author: (product) => {
console.log(`Query.Product.author for "${product.name}"`)
return usersData.find(user => user.id === product.authorId)
},
},
}

Notice that since the Product.author resolver receives a parent object, we also log additional information about what project the resolver is called for.

Now, we can run the same nested query we used the last time.

query {
allProducts {
name
description
author {
id
fullName
}
}
}

And ...