Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: allow dataSources creator function to be async #4222

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions packages/apollo-server-core/src/__tests__/dataSources.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion packages/apollo-server-core/src/graphqlOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export interface GraphQLServerOptions<
tracing?: boolean;
cacheControl?: CacheControlExtensionOptions;
extensions?: Array<() => GraphQLExtension>;
dataSources?: () => DataSources<TContext>;
dataSources?: () => DataSources<TContext> | Promise<DataSources<TContext>>;
cache?: KeyValueCache;
persistedQueries?: PersistedQueryOptions;
plugins?: ApolloServerPlugin[];
Expand Down
4 changes: 2 additions & 2 deletions packages/apollo-server-core/src/requestPipeline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ export interface GraphQLRequestPipelineConfig<TContext> {
executor?: GraphQLExecutor;
fieldResolver?: GraphQLFieldResolver<any, TContext>;

dataSources?: () => DataSources<TContext>;
dataSources?: () => DataSources<TContext> | Promise<DataSources<TContext>>;

extensions?: Array<() => GraphQLExtension>;
persistedQueries?: PersistedQueryOptions;
Expand Down Expand Up @@ -718,7 +718,7 @@ export async function processGraphQLRequest<TContext>(
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)) {
Expand Down