4. Implementing our RESTDataSource
2m
You're currently on an older version of this course. View course changelog.

Let's add this RESTDataSource class to our project.

In the terminal, in our server/ folder, stop the current process that we started in the project setup section, then run:

npm install apollo-datasource-rest

Once that's done, start up the server again with npm start.

Next, let's create a folder called datasources in the server/src folder, where all our will live. We'll create a file called track-api.js.

First, we import our apollo-datasource-rest package.

const { RESTDataSource } = require("apollo-datasource-rest");

This gives us a RESTDataSource class that we can extend.

We'll declare a class called TrackAPI that extends RESTDataSource. While we're here, let's export it before we forget!

class TrackAPI extends RESTDataSource {
// ...
}
module.exports = TrackAPI;

Let's define a constructor method inside the class. Inside, we'll call super() to make sure we get access to our RESTDataSource features. We'll also assign our REST API's base url.

class TrackAPI extends RESTDataSource {
constructor() {
super();
this.baseURL = "https://odyssey-lift-off-rest-api.herokuapp.com/";
}
}
Code Challenge!

Create a class called SpaceCatsAPI that extends the RESTDataSource class. It should have a constructor method. Its baseURL should be set to https://fake-spacecats-rest-api.cat/. Assume that the RESTDataSource class has already been imported.

Let's define a method called getTracksForHome inside our TrackAPI class. The RESTDataSource class provides helper methods for HTTP requests. In our case, we want to perform a GET request to the tracks endpoint. Then, we return the results of that call.

Below our constructor method:

getTracksForHome() {
return this.get('tracks');
}

Next, it's time to define the getAuthor method inside our TrackAPI class. It takes an authorId as an and uses it in a GET call to the /author/:id endpoint. Then, we return the results of that call.

Below our getTracksForHome method:

getAuthor(authorId) {
return this.get(`author/${authorId}`);
}

Note the use of backticks (`) enclosing the author/:id endpoint, because we're using string interpolation to add the authorId at the end.

Nice work! We've got our set up and retrieving data from the REST API.

server/src/datasources/track-api.js
const { RESTDataSource } = require("apollo-datasource-rest");
class TrackAPI extends RESTDataSource {
constructor() {
super();
this.baseURL = "https://odyssey-lift-off-rest-api.herokuapp.com/";
}
getTracksForHome() {
return this.get("tracks");
}
getAuthor(authorId) {
return this.get(`author/${authorId}`);
}
}
module.exports = TrackAPI;
Code Challenge!

The SpaceCatsAPI class needs two methods. The first method is called getSpaceCats. This method makes a GET request to the spacecats endpoint and returns the results. The second method is called getMissions. It takes in a catId as an argument and makes a GET request to the spacecats/${catId}/missions endpoint and returns the results. Assume that the RESTDataSource class has already been imported.

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.