Playing with the Spotify Web API has never been so simple.
As a music lover, Iâm really interested in understanding the magic behind music, by learning music theory, playing guitar and singing.
However, I wanted to combine my two passions by building web-projects around music.
I quickly discovered the
Spotify Web API, with all the data I neededâââeven more with audio features/analysis.
This post is the first about my journey on building tools and web-products on top of Spotify, I hope youâll enjoy it!
Why using GraphQL for Spotify Web APIÂ ?
REST is cool, however itâs a total nightmare to fetch data like :
âFetch all current user playlists with associated tracks and artists dataâ
Even with
async,
Promise or
when.js, you ended up with a complex âdata fetchingâ and data formatting code.
I started a prototype using the awesome
spotify-web-api-node library :
This code excerpt does not handle pagination, exceptions, data formatting.
Not that satisfying. đŁ
I quickly remembered this awesome project called GraphQL created by Facebook and applied the
do not reinvent the wheel rule.
I decided to write a library called
spotify-graphql
Okay, what is this awesome âGraphQLâ?
I will quote the tagline of graphql.org
A query language for your API
What it means is that GraphQL is basically a query language for APIsâââin fact it can be what you want.
GraphQL is a library that offer a query language and allows you to define how it should fetch the data using resolvers.
Querying Spotify with spotify-graphql
Remember the following statement ?
âFetch all current user playlists with associated tracks and artists dataâ
Hereâs how you do it with spotify-graphql :
10 lines and you end up with a well formatted data object. đ
Features added on-top of GraphQL â¤ď¸
The library with paginate automatically for you by inspecting API responses.
However you can set explicit rules by using query params.
1{
2 me {
3 playlists(limit: 10)
4 }
5}
API rate limit avoider
Fetching a lot of data with GraphQL can aim to do a lot of simultaneous requests.
To avoid that, SpotifyGraphQL is shipped with a throttle system and non-concurrency system.
In short, by default, SpotifyGraphQL will perform request in sequence of 2ms spaced calls.
embedded LRU cache
Throttling on big request can results of very long queries.
To avoid performing many times the same requests,
SpotifyGraphQL uses a
LRU cache, based on resources/params couple.
aliases with âsearch-on-the-flyâ
One âhipster featureâ of SpotifyGraphQL is âsearch on the flyâ.
1{
2 artist(name: "Kendrick Lamar") {
3 top_tracks {
4 track {
5 name
6 }
7 }
8}
However, beware of this feature, there is no way to ensure that the search by name will return the desired Artist/Track.
Spotify GraphQLÂ Console
It allows you to play with Spotify API, without having to code.
Whatâs next
spotify-graphql@2.0.0
- 100% read-only endpoints coverage
- rewrite of spotify-web-api-node (with caching and 100% API coverage)
- improved stability, more tests
- Release target date : June 2017
I hope you had fun reading this article and that you will have fun playing with SpotifyGraphQL.