UNPKG

@envelop/apollo-datasources

Version:

This plugin integrates Apollo DataSources into Envelop.

32 lines (31 loc) 1.19 kB
import { InMemoryLRUCache } from '@apollo/utils.keyvaluecache'; import { isPromise } from '@envelop/core'; export function useApolloDataSources(config) { const cache = config.cache || new InMemoryLRUCache(); return { async onExecute({ extendContext, args }) { const dataSources = config.dataSources(); const initializers = []; for (const dataSource of Object.values(dataSources)) { if (dataSource.initialize) { const init$ = dataSource.initialize({ context: args.contextValue, cache, }); if (isPromise(init$)) { initializers.push(init$); } } } if (initializers.length) { await Promise.all(initializers); } if ('dataSources' in args.contextValue) { throw new Error('Please use the dataSources config option instead of putting dataSources on the context yourself.'); } extendContext({ dataSources, }); }, }; }