2017-10-19 08:00:21 +00:00
|
|
|
import createLoaders from './loaders'
|
|
|
|
|
|
|
|
|
|
const debug = require('debug')('graphbrainz:context')
|
|
|
|
|
|
2017-11-07 05:54:56 +00:00
|
|
|
export function extendContext(extension, context, options) {
|
2017-10-19 08:00:21 +00:00
|
|
|
if (extension.extendContext) {
|
|
|
|
|
if (typeof extension.extendContext === 'function') {
|
2017-11-07 05:54:56 +00:00
|
|
|
debug(
|
|
|
|
|
`Extending context via a function from the “${
|
|
|
|
|
extension.name
|
|
|
|
|
}” extension.`
|
|
|
|
|
)
|
2017-10-19 08:00:21 +00:00
|
|
|
context = extension.extendContext(context, options)
|
|
|
|
|
} else {
|
|
|
|
|
throw new Error(
|
|
|
|
|
`Extension “${extension.name}” contains an invalid \`extendContext\` ` +
|
2017-11-07 05:54:56 +00:00
|
|
|
`value: ${extension.extendContext}`
|
2017-10-19 08:00:21 +00:00
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return context
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-07 05:54:56 +00:00
|
|
|
export function createContext(options = {}) {
|
2017-10-19 08:00:21 +00:00
|
|
|
const { client } = options
|
|
|
|
|
const loaders = createLoaders(client)
|
|
|
|
|
const context = { client, loaders }
|
2017-11-05 19:59:39 +00:00
|
|
|
const { extensions = [] } = options
|
|
|
|
|
return extensions.reduce((context, extension) => {
|
2017-10-19 08:00:21 +00:00
|
|
|
return extendContext(extension, context, options)
|
|
|
|
|
}, context)
|
|
|
|
|
}
|