Odyssey

Lift-off II: Resolvers

Journey of a GraphQL queryExploring our dataApollo RESTDataSourceImplementing our RESTDataSourceThe shape of a resolverImplementing query resolversConnecting the dots in server-landQuerying live dataErrors! When queries go sidewaysJourney's end
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 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.

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 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.

1
const server = new ApolloServer({
2
typeDefs,
3
resolvers,
4
});

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:

1
const server = new ApolloServer({
2
typeDefs,
3
resolvers,
4
dataSources: () => {
5
return {
6
trackAPI: new TrackAPI(),
7
};
8
},
9
});
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!)

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

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

Previous
Next

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.

              resolvers

              A function that populates data for a particular field in a GraphQL schema. For example:

              const resolvers = {
              Query: {
              author(root, args, context, info) {
              return find(authors, { id: args.id });
              },
              },
              };
              Apollo Server

              An open-source library for server-side GraphQL operation handling. It can be used as a monolithic GraphQL server or a subgraph server within a supergraph.

              resolvers

              A function that populates data for a particular field in a GraphQL schema. For example:

              const resolvers = {
              Query: {
              author(root, args, context, info) {
              return find(authors, { id: args.id });
              },
              },
              };
              Apollo Server

              An open-source library for server-side GraphQL operation handling. It can be used as a monolithic GraphQL server or a subgraph server within a supergraph.

              resolvers

              A function that populates data for a particular field in a GraphQL schema. For example:

              const resolvers = {
              Query: {
              author(root, args, context, info) {
              return find(authors, { id: args.id });
              },
              },
              };
              resolvers

              A function that populates data for a particular field in a GraphQL schema. For example:

              const resolvers = {
              Query: {
              author(root, args, context, info) {
              return find(authors, { id: args.id });
              },
              },
              };
              resolvers

              A function that populates data for a particular field in a GraphQL schema. For example:

              const resolvers = {
              Query: {
              author(root, args, context, info) {
              return find(authors, { id: args.id });
              },
              },
              };
              Apollo Server

              An open-source library for server-side GraphQL operation handling. It can be used as a monolithic GraphQL server or a subgraph server within a supergraph.

              NEW COURSE ALERT

              Introducing Apollo Connectors

              Connectors are the new and easy way to get started with GraphQL, using existing REST APIs.

              Say goodbye to GraphQL servers and resolvers—now, everything happens in the schema!

              Take the course