7. Connecting the dots in server-land
2m
You're currently on an older version of this course. View course changelog.

We've got our and ready, but they don't know yet how to work together.

is where all the elements we've built previously (the schema, the , and the ) come together in perfect coordination.

Hand-drawn illustration depicting a GraphQL server juggling three components: the schema, resolver function and data sources

In server/src/index.js, where we configured our in Part I, we can now replace our mocks with .

Let's remove the mocks object, as well as the mocks property in the ApolloServer constructor.

Next, let's import our resolvers file at the top.

const resolvers = require("./resolvers");

And then add it to the ApolloServer options.

const server = new ApolloServer({
typeDefs,
resolvers,
});

That's the taken care of.

Next, just below our resolvers import, we'll require track-api, our file (extending RESTDataSource), and call it TrackAPI (note the PascalCase convention, as we're dealing with the class here).

const TrackAPI = require("./datasources/track-api");

To connect our server with our TrackAPI, we'll add the dataSources key. This is what enables us to access the dataSources.trackAPI (and its methods) from the context parameter of our . takes care of all the plumbing for us, pretty neat!

To learn more about the options that ApolloServer can receive, check out the documentation.

This is what our server configuration will look like when it's finished:

const server = new ApolloServer({
typeDefs,
resolvers,
dataSources: () => {
return {
trackAPI: new TrackAPI(),
};
},
});
Code Challenge!

Configure the ApolloServer options with the dataSources key for a RestDataSource Class named SpaceCatsAPI, that we need to access at dataSources.spaceCatsAPI from our resolver. (Watch out, this is case sensitive!)

Why do we need to configure the dataSources key in ApolloServer options?

Our server is now fully configured to work with live data.

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.