How GraphQL Resolvers Work?
Learn how GraphQL resolvers work and how Apollo Server uses them.
We'll cover the following...
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.
Press + to interact
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.
Press + to interact
query {allProducts {namedescriptionauthor {idfullName}}}
...