# connector-spotify

This package is a wrapper to the Spotify Web API and defines types that you can use in graphql applications.
The primary use cases are for searching tracks, artists, and albums.

The publishing app uses the Spotify Web API to create GlobalSoundRecording nodes that are not currently in our graph database.
These GSRs and corresponding related nodes, such PublicTrack, PublicParticipant are created with the `Spotify` label.

## Instantiation

To instantiate the connector, you must provide an object with `clientId` and `clientSecret` as follows:

```
import { SpotifyConnector } from "@theorchard/connector-spotify";

spotify: new SpotifyConnector({
    spotifyClientId: <clientId>,
    spotifyClientSecret: <clientSecret>
})
```

The connector can be part of the list of dataSources in your service. The following is an example from `graphql-knowledge`:

```
const createDataSources = (config) => () => ({
    label: new Label(),
    labelSoundRecording: new LabelSoundRecording(),
    track: new Track(),
    product: new Product(),
    owsNotifications: new OwsNotifications(config),
    owsPermissions: new OwsPermissionsDataSource(config),
    spotify: new SpotifyConnector({
        clientId: config.spotifyClientId,
        clientSecret: config.spotifyClientSecret
    }),
    firebase: new Firebase(config),
    chartmetric: new Chartmetric(config),
    kafka: new Kafka(config),
    graphqlGateway: new GraphQLGateway(config)
});

export default createDataSources;
```

## Available methods

### searchTracks

This method allows you to search tracks by providing a term. Example of the current search in `graphql-knowledge`

```
const spotifyTrackSearchResolver = (parent, { term }, context) =>
    context.dataSources.spotify.searchTracks(term);
```

### searchAlbums

This method allows you to search albums by providing a term.

```
const spotifyTrackSearchResolver = (parent, { term }, context) =>
    context.dataSources.spotify.searchAlbums(term);
```

### getFullAlbumFromTrackId

The method takes a Spotify track id (usually used from the search above) and returns

```
{
  isrc: isrc of the spotify track,
  album: {
    ...metadata,
    tracks: {
      next: <pagination>,
      previous: <pagination>,
      href: <pagination>,
      limit: n,
      offset: 0,
      items: [
      	{
      		..trackMetadata
      	}
      ]
    },
    artists: [
    	{
    		...artistMetadata
    	}
    ]
  }
}
```

In the publishing use case, we create a GSR from spotify search results that are not in our graph.
Instead of only creating the track selected by the user, we seize the opportunity to have a more complete public subgraph by creating the entire album and subsequent tracks that the selected track belongs to.
This method returns a fully detailed album with its tracks and artists by making a few Spotify Web API calls to retrieve all this metadata. This metadata is then fed to this cypher [query](https://github.com/theorchard/graphql-knowledge/blob/master/src/cypher/global-sound-recording/create-from-spotify-api.cypher)
The current limitation is for albums that have more than 50 tracks. Since this method is part of the request cycle of `graphql-knowledge` mutation, its doesnt iterate through the track lists. There are TO DOs to offload to an asynchronous worker using AWS Batch or something similar.
