Odyssey

Growing your GraphQL API with TypeScript & Apollo Server
deprecated

Project setupAdding the Artist typeThe Artist data sourceUpdating the Track modelAdding the resolverWrapping up
3. The Artist data source
3m

🎯 Your goal for this step:

Add a new method to the data source class to retrieve artist-specific data

Artist data tasks

Use the following artist ID when completing these tasks.

An artist ID
3GBPw9NK25X1Wt2OUvOwY3

Solution: The Artist data source

models.ts
// Represents an artist object returned by the REST API's artists endpoint
export type ArtistModel = {
id: string;
name: string;
genres: string[];
uri: string;
followers: { total: number };
};
datasources/spotify-api.ts
import {
PlaylistModel,
TrackModel,
SnapshotOrError,
ArtistModel,
} from "../models";
export class SpotifyAPI extends RESTDataSource {
baseURL = "https://spotify-demo-api-fe224840a08c.herokuapp.com/v1/";
// ...other SpotifyAPI methods
async getArtist(artistId: string): Promise<ArtistModel> {
return this.get<ArtistModel>(`artists/${artistId}`);
}
}
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.

              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