graphbrainz/src/extensions/fanart-tv/loader.js
Brian Beck 898ec78a6f Add a schema extension API and several extensions (#42)
* Add a schema extension API and several extensions
* Update graphql-markdown to use new diffSchema function
* Update Node support
2017-10-19 01:00:21 -07:00

51 lines
1.5 KiB
JavaScript

import DataLoader from 'dataloader'
import LRUCache from 'lru-cache'
const debug = require('debug')('graphbrainz:extensions/fanart-tv')
export default function createLoader (options) {
const { client } = options
const cache = LRUCache({
max: options.cacheSize,
maxAge: options.cacheTTL,
dispose (key) {
debug(`Removed from cache. key=${key}`)
}
})
// Make the cache Map-like.
cache.delete = cache.del
cache.clear = cache.reset
const loader = new DataLoader(keys => {
return Promise.all(keys.map(key => {
const [ entityType, id ] = key
return client.musicEntity(entityType, id)
.catch(err => {
if (err.statusCode === 404) {
// 404s are OK, just return empty data.
return {
artistbackground: [],
artistthumb: [],
musiclogo: [],
hdmusiclogo: [],
musicbanner: [],
musiclabel: [],
albums: {}
}
}
throw err
}).then(body => {
if (entityType === 'artist') {
const releaseGroupIDs = Object.keys(body.albums)
debug(`Priming album cache with ${releaseGroupIDs.length} album(s).`)
releaseGroupIDs.forEach(key => loader.prime(['release-group', key], body))
}
return body
})
}))
}, {
cacheKeyFn: ([ entityType, id ]) => `${entityType}/${id}`,
cacheMap: cache
})
return loader
}