Skip to content

Commit

Permalink
update with final models and codegen
Browse files Browse the repository at this point in the history
  • Loading branch information
Liz Hennessy authored and Liz Hennessy committed Feb 2, 2024
1 parent f3e7e5a commit 3afd6cb
Show file tree
Hide file tree
Showing 5 changed files with 131 additions and 0 deletions.
5 changes: 5 additions & 0 deletions codegen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ const config: CodegenConfig = {
plugins: ["typescript", "typescript-resolvers"],
config: {
contextType: "./context#DataSourceContext",
mappers: {
Playlist: "./models#PlaylistModel",
Track: "./models#TrackModel",
AddItemsToPlaylistPayload: "./models#AddItemsToPlaylistPayloadModel",
},
},
},
},
Expand Down
13 changes: 13 additions & 0 deletions src/datasources/spotify-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,17 @@ export class SpotifyAPI extends RESTDataSource {
const { items = [] } = playlists;
return items;
}

addItemsToPlaylist(input: { playlistId: string; uris: string[] }) {
const { playlistId, uris } = input;
return this.post(`playlists/${playlistId}/tracks`, {
params: {
uris: uris.join(","),
},
});
}

getPlaylist(playlistId: string) {
return this.get(`playlists/${playlistId}`);
}
}
29 changes: 29 additions & 0 deletions src/models.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
export type PlaylistModel = {
id: string;
name: string;
description: string;
tracks: {
items: TrackModelFromPlaylist[];
};
};

/* this model reflects how a playlist stores and returns
its track objects */
export type TrackModelFromPlaylist = {
track: TrackModel;
};

export type TrackModel = {
id: string;
name: string;
duration_ms: number;
explicit: boolean;
uri: string;
};

export type AddItemsToPlaylistPayloadModel = {
code: number;
success: boolean;
message: string;
playlistId: string;
};
45 changes: 45 additions & 0 deletions src/resolvers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,50 @@ export const resolvers: Resolvers = {
featuredPlaylists: (_, __, { dataSources }) => {
return dataSources.spotifyAPI.getFeaturedPlaylists();
},
playlist: (_, { id }, { dataSources }) => {
return dataSources.spotifyAPI.getPlaylist(id);
},
},
Mutation: {
async addItemsToPlaylist(_, { input }, { dataSources }) {
try {
const response = await dataSources.spotifyAPI.addItemsToPlaylist(input);
if (response.snapshot_id) {
return {
code: 200,
success: true,
message: "Tracks added to playlist!",
playlistId: response.snapshot_id,
};
} else {
throw Error("snapshot_id property not found");
}
} catch (e) {
return {
code: 500,
success: false,
message: `Something went wrong: ${e}`,
playlistId: null,
};
}
},
},
AddItemsToPlaylistPayload: {
playlist: ({ playlistId }, _, { dataSources }) => {
return dataSources.spotifyAPI.getPlaylist(playlistId);
},
},
Playlist: {
tracks: (parent) => {
const { tracks } = parent;
const { items = [] } = tracks;
return items.map(({ track }) => track);
},
},
Track: {
durationMs: (parent) => {
const { duration_ms: durationMs } = parent;
return durationMs;
},
},
};
39 changes: 39 additions & 0 deletions src/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,30 @@ export const typeDefs = gql`
type Query {
"Playlists hand-picked to be featured to all users."
featuredPlaylists: [Playlist!]!
"Retrieves a specific playlist."
playlist(id: ID!): Playlist
}
type Mutation {
"Add one or more items to a user's playlist."
addItemsToPlaylist(
input: AddItemsToPlaylistInput!
): AddItemsToPlaylistPayload!
}
input AddItemsToPlaylistInput {
"The ID of the playlist."
playlistId: ID!
"A comma-separated list of Spotify URIs to add."
uris: [String!]!
}
type AddItemsToPlaylistPayload {
"Similar to HTTP status code, represents the status of the mutation"
code: Int!
"Indicates whether the mutation was successful"
success: Boolean!
"Human-readable message for the UI"
message: String!
"The playlist that contains the newly added items"
playlist: Playlist
}
"A curated collection of tracks designed for a specific activity or mood."
type Playlist {
Expand All @@ -13,5 +37,20 @@ export const typeDefs = gql`
name: String!
"Describes the playlist, what to expect and entices the user to listen."
description: String
"The tracks of the playlist."
tracks: [Track!]!
}
"A single audio file, usually a song."
type Track {
"The ID for the track."
id: ID!
"The name of the track"
name: String!
"The track length in milliseconds."
durationMs: Int!
"Whether or not the track has explicit lyrics (true = yes it does; false = no it does not OR unknown)"
explicit: Boolean!
"The URI for the track, usually a Spotify link."
uri: String!
}
`;

0 comments on commit 3afd6cb

Please sign in to comment.