diff --git a/packages/apollo-server-core/src/__tests__/dataSources.test.ts b/packages/apollo-server-core/src/__tests__/dataSources.test.ts index 995ef2ee497..4a6d1754c97 100644 --- a/packages/apollo-server-core/src/__tests__/dataSources.test.ts +++ b/packages/apollo-server-core/src/__tests__/dataSources.test.ts @@ -81,6 +81,26 @@ describe('ApolloServerBase dataSources', () => { expect(actualCallOrder).toEqual(expectedCallOrder); }); + it('initializes datasources from an asynchronous datasource creator function', async () => { + const initialize = jest.fn(); + + const server = new ApolloServerBase({ + typeDefs, + resolvers: { + Query: { + hello() { + return 'world'; + } + } + }, + dataSources: async () => ({ x: { initialize }, y: { initialize } }) + }); + + await server.executeOperation({ query: "query { hello }"}); + + expect(initialize).toHaveBeenCalledTimes(2); + }); + it('makes datasources available on resolver contexts', async () => { const message = 'hi from dataSource'; const getData = jest.fn(() => message); diff --git a/packages/apollo-server-core/src/graphqlOptions.ts b/packages/apollo-server-core/src/graphqlOptions.ts index b4c6f1a7b86..2771b33c8bd 100644 --- a/packages/apollo-server-core/src/graphqlOptions.ts +++ b/packages/apollo-server-core/src/graphqlOptions.ts @@ -59,7 +59,7 @@ export interface GraphQLServerOptions< tracing?: boolean; cacheControl?: CacheControlExtensionOptions; extensions?: Array<() => GraphQLExtension>; - dataSources?: () => DataSources; + dataSources?: () => DataSources | Promise>; cache?: KeyValueCache; persistedQueries?: PersistedQueryOptions; plugins?: ApolloServerPlugin[]; diff --git a/packages/apollo-server-core/src/requestPipeline.ts b/packages/apollo-server-core/src/requestPipeline.ts index d3588f04e1a..b1695793371 100644 --- a/packages/apollo-server-core/src/requestPipeline.ts +++ b/packages/apollo-server-core/src/requestPipeline.ts @@ -90,7 +90,7 @@ export interface GraphQLRequestPipelineConfig { executor?: GraphQLExecutor; fieldResolver?: GraphQLFieldResolver; - dataSources?: () => DataSources; + dataSources?: () => DataSources | Promise>; extensions?: Array<() => GraphQLExtension>; persistedQueries?: PersistedQueryOptions; @@ -718,7 +718,7 @@ export async function processGraphQLRequest( if (config.dataSources) { const context = requestContext.context; - const dataSources = config.dataSources(); + const dataSources = await config.dataSources(); const initializers: any[] = []; for (const dataSource of Object.values(dataSources)) {