import { safeApiCall , syncedPoll } from '../utils' ; import * as _ from 'lodash' ; const sequence = require ( 'when/sequence' ) ; export function trackResolvers ( spotifyApiClient ) { return { artists ( track , variables ) { if ( ! ! variables . full ) { return syncedPoll ( 'Track.artists' , ( ) => { return new Promise ( ( resolve , reject ) => { let queries = _ ( track . artists ) . map ( 'id' ) . compact ( ) . chunk ( 50 ) . map ( ( idsToQuery ) => { return ( ) : Promise < any > => { return safeApiCall ( spotifyApiClient , 'getArtists' , ( response ) => response . body . artists , idsToQuery ) ; } ; } ) ; sequence ( Array . from ( queries ) ) . then ( ( results ) => { resolve ( _ ( results ) . flatten ( ) ) ; } , ( e ) => { reject ( e ) ; } ) ; } ) ; } , variables . throttle || 5 ) ; } else { return track . artists ; } } , album ( track , variables ) { if ( ! ! variables . full ) { return syncedPoll ( 'Track.album' , ( ) => { return safeApiCall ( spotifyApiClient , 'getAlbum' , null , track . album . id ) . then ( ( album ) => { return album ; } ) ; } , variables . throttle || 5 ) ; } else { return Promise . resolve ( track . album ) ; } } , audio_features ( track ) { return safeApiCall ( spotifyApiClient , 'getAudioFeaturesForTrack' , null , track . id ) ; } } ; }