5. The shape of a resolver
3m
You're currently on an older version of this course. View course changelog.

Our RESTDataSource is ready to use, but we don't yet have any to use it!

A 's mission is to populate the data for a in your schema. Your mission is to implement those !

What exactly is a ? A resolver is a function. It has the same name as the that it populates data for. It can fetch data from any , then transforms that data into the shape your client requires.

Hand-drawn illustration depicting a resolver function retrieving data from data-land

In server/src/ we'll start by creating a resolvers.js file.

In that file, we'll declare a resolvers constant, assigning it an empty object for now. Let's export it, because we'll need it in our server config options.

const resolvers = {};
module.exports = resolvers;

Our resolvers object's keys will correspond to our schema's types and .

To create a for the tracksForHome , we'll first add a Query key to our resolvers object. The value of that key will be another object that contains the tracksForHome key.

This tracksForHome key is where we'll define our function for the corresponding .

While we're here, let's add a comment above the to clarify what it does.

Inside the resolvers object:

Query: {
// returns an array of Tracks that will be used to populate
// the homepage grid of our web client
tracksForHome: () => {},
}
Which of the following are true about resolvers?

How will our interact with our ? This is where context comes in. functions have a specific signature with four optional parameters: parent, args, context, and info.

Hand-drawn illustration depicting a resolver function signature with its four parameters
tracksForHome: (parent, args, context, info) => {},

Let's go over each parameter briefly to understand what they're responsible for:

  • parent:
    parent is the returned value of the for this 's parent. This will be useful when dealing with resolver chains.
  • args:
    args is an object that contains all that were provided for the by the GraphQL . When for a specific item (such as a specific track instead of all tracks), in client-land we'll make a with an id that will be accessible via this args parameter in server-land. We'll cover this further in Lift-off III.
  • context:
    context is an object shared across all that are executing for a particular . The resolver needs this context to share state, like authentication information, a database connection, or in our case the RESTDataSource.
  • info:
    info contains information about the 's execution state, including the name, the path to the field from the root, and more. It's not used as frequently as the others, but it can be useful for more advanced actions like setting cache policies at the level.
What is the context parameter useful for?
Code Challenge!

Write an empty resolver function for the field spaceCats with all four parameters as described above. The function should not return anything. Use arrow function syntax.

In this course, we'll be focusing on context, the third positional that's passed to every function. Let's dig into it.

Previous

Share your questions and comments about this lesson

Your feedback helps us improve! If you're stuck or confused, let us know and we'll help you out. All comments are public and must follow the Apollo Code of Conduct. Note that comments that have been resolved or addressed may be removed.

You'll need a GitHub account to post below. Don't have one? Post in our Odyssey forum instead.