We've got our resolvers and data source ready, but they don't know yet how to work together.
Apollo Server is where all the elements we've built previously (the schema, the resolvers, and the data sources) come together in perfect coordination.
In server/src/index.js
, where we configured our Apollo Server in Part I, we can now replace our mocks with resolvers.
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 resolvers taken care of.
Next, just below our resolvers
import, we'll require track-api
, our data source 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 resolvers. Apollo Server 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(),};},});
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!)
dataSources
key in ApolloServer
options?Our server is now fully configured to work with live data.
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.