Socket
Socket
Sign inDemoInstall

apollo-datasource-spotify

Package Overview
Dependencies
1
Maintainers
1
Versions
19
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

apollo-datasource-spotify

RESTDataSource wrapper for the Spotify API


Version published
Maintainers
1
0

Weekly downloads

Readme

Source

Apollo Data Source Spotify

Apollo Data Source Spotify encapsulates fetching data from the Spotify API.

Getting Started

Install

npm install apollo-datasource-rest dataloader graphql

Configure

import { SpotifyAPI } from 'apollo-datasource-spotify';

const apollo = new ApolloServer({
  context: ({ req }) => ({
    authorization: req.headers.authorization,
  }),
  dataSources: () => ({
    spotify: new SpotifyAPI(),
  }),
});

Usage

export const resolvers = {
  Query: {
    me(root, args, context) {
      return context.dataSources.spotify.me();
    },
  },
};
Batch Requests
import { batch } from 'apollo-datasource-spotify';

const resolver = async (root, args, context) {
  await batch({
    limit: 2500,
    request: (params) => dataSources.spotify.savedTracks(params),
    callback: async (savedTracks) => {
      console.log(savedTracks);
    },
  });

  return;
};

Throttle Requests

If you run into issues with rate-limiting, try throttling requests with something like bottleneck.

npm install --save bottleneck

import SpotifyAPI from 'apollo-datasource-rest';
import Bottleneck from 'bottleneck';

class ThrottledSpotifyAPI extends SpotifyAPI {
  constructor() {
    super();

    this.limiter = new Bottleneck({
      reservoir: 100,
      reservoirRefreshAmount: 100,
      reservoirRefreshInterval: 60 * 1000,
      maxConcurrent: 1,
      minTime: 333,
    });

    this.get = this.limiter.wrap(this.get.bind(this));
    this.post = this.limiter.wrap(this.post.bind(this));
    this.put = this.limiter.wrap(this.put.bind(this));
    this.delete = this.limiter.wrap(this.delete.bind(this));
  }
}

Spotify GraphQL Schema

To use the Spotify GraphQL schema implement your own custom scalar JSON type or use graphql-type-json.

Install (optional)

npm install --save graphql-type-json

Configure

import { ApolloServer, gql } from 'apollo-server';
import { SpotifyAPI, typeDefs, resolvers } from 'apollo-datasource-spotify';
import GraphQLJSON from 'graphql-type-json';

const apollo = new ApolloServer({
  context: ({ req }) => ({
    authorization: req.headers.authorization,
  }),
  dataSources: () => ({
    spotify: new SpotifyAPI(),
  }),
  typeDefs: gql(typeDefs()),
  resolvers: resolvers({ JSON: GraphQLJSON }),
});

Usage

query {
  spotify {
    getAlbum(id: "42j41uUwuHZT3bnedq2XtM") {
      id
      name
    }
    getAlbumsTracks(id: "42j41uUwuHZT3bnedq2XtM") {
      items {
        ... on SimplifiedTrackObject {
          id
          name
          preview_url
          artists {
            id
            name
          }
        }
      }
    }
  }
}

FAQs

Last updated on 02 Jun 2021

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc