Skip to content

Commit

Permalink
Merge pull request #3 from apollographql-education/lh/final-updates
Browse files Browse the repository at this point in the history
update resolvers and add method to spotify api
  • Loading branch information
lizhennessy authored Apr 22, 2024
2 parents 527f60f + e3f8ba6 commit 4e10af5
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
9 changes: 7 additions & 2 deletions src/datasources/spotify-api.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { RESTDataSource } from "@apollo/datasource-rest";
import { Playlist } from "../types"
import { PlaylistModel, SnapshotOrError } from '../models'
import { PlaylistModel, TrackModel, SnapshotOrError } from '../models'

export class SpotifyAPI extends RESTDataSource {
baseURL = "https://spotify-demo-api-fe224840a08c.herokuapp.com/v1/";
Expand All @@ -13,6 +12,12 @@ export class SpotifyAPI extends RESTDataSource {
}>("browse/featured-playlists");

return response?.playlists?.items ?? [];

}

async getTracks(playlistId: string): Promise<TrackModel[]> {
const response = await this.get<{ items: { track: TrackModel }[] }>(`playlists/${playlistId}/tracks`)
return response?.items?.map(({track}) => track) ?? [];
}

getPlaylist(playlistId: string): Promise<PlaylistModel> {
Expand Down
5 changes: 2 additions & 3 deletions src/resolvers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,8 @@ export const resolvers: Resolvers = {
},
},
Playlist: {
tracks: ({ tracks }) => {
const { items = [] } = tracks;
return items.map(({ track }) => track);
tracks: async ({ tracks, id }, _, { dataSources }) => {
return tracks.items ? tracks.items.map(({track}) => track) : dataSources.spotifyAPI.getTracks(id);
}
},
Track: {
Expand Down

0 comments on commit 4e10af5

Please sign in to comment.