1 | import createLoaders from './loaders'
|
2 |
|
3 | const debug = require('debug')('graphbrainz:context')
|
4 |
|
5 | export function extendContext (extension, context, options) {
|
6 | if (extension.extendContext) {
|
7 | if (typeof extension.extendContext === 'function') {
|
8 | debug(`Extending context via a function from the “${extension.name}” extension.`)
|
9 | context = extension.extendContext(context, options)
|
10 | } else {
|
11 | throw new Error(
|
12 | `Extension “${extension.name}” contains an invalid \`extendContext\` ` +
|
13 | `value: ${extension.extendContext}`
|
14 | )
|
15 | }
|
16 | }
|
17 | return context
|
18 | }
|
19 |
|
20 | export function createContext (options) {
|
21 | const { client } = options
|
22 | const loaders = createLoaders(client)
|
23 | const context = { client, loaders }
|
24 | return options.extensions.reduce((context, extension) => {
|
25 | return extendContext(extension, context, options)
|
26 | }, context)
|
27 | }
|