-
Notifications
You must be signed in to change notification settings - Fork 2k
Allow async initialization of data sources #3639
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
Merged
Merged
Changes from 3 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
9175cfe
Allow async initialization of dataSources
lostfictions 11a7687
change return type of DataSource.initialize
lostfictions 0570841
Add some dataSource integration tests
lostfictions 2341146
Merge branch 'master' into patch-1
trevor-scheer e14393d
make initialization order test a bit more robust
lostfictions e7306da
update changelog
lostfictions 9211a79
Merge branch 'master' into patch-1
lostfictions eb64a4a
use a minimal setTimeout
lostfictions feb7682
Await initializers concurrently, not one-by-one
lostfictions 6686794
Merge branch 'master' into patch-1
trevor-scheer File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
96 changes: 96 additions & 0 deletions
96
packages/apollo-server-core/src/__tests__/dataSources.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| import { ApolloServerBase } from '../ApolloServer'; | ||
| import gql from 'graphql-tag'; | ||
|
|
||
| const typeDefs = gql` | ||
| type Query { | ||
| hello: String | ||
| } | ||
| `; | ||
|
|
||
| const resolvers = { | ||
| Query: { | ||
| hello() { | ||
| return 'world'; | ||
| } | ||
| }, | ||
| }; | ||
|
|
||
| describe('ApolloServerBase dataSources', () => { | ||
| it('initializes synchronous datasources from a datasource creator function', async () => { | ||
| const initialize = jest.fn(); | ||
|
|
||
| const server = new ApolloServerBase({ | ||
| typeDefs, | ||
| resolvers, | ||
| dataSources: () => ({ x: { initialize }, y: { initialize } }) | ||
| }); | ||
|
|
||
| await server.executeOperation({ query: "query { hello }"}); | ||
|
|
||
| expect(initialize).toHaveBeenCalledTimes(2); | ||
| }); | ||
|
|
||
| it('initializes asynchronous datasources before calling resolvers', async () => { | ||
| const expectedMessage = 'success'; | ||
| let maybeInitialized = 'failure'; | ||
|
|
||
| const additionalInitializer = jest.fn(); | ||
|
|
||
| const server = new ApolloServerBase({ | ||
| typeDefs, | ||
| resolvers: { | ||
| Query: { | ||
| hello(_, __, context) { | ||
| return context.dataSources.x.getData(); | ||
| } | ||
| }, | ||
| }, | ||
| dataSources: () => ({ | ||
| x: { | ||
| initialize() { | ||
| return new Promise(res => { setTimeout(() => { | ||
| maybeInitialized = expectedMessage; | ||
| res(); | ||
| }, 200) }) | ||
| }, | ||
| getData() { return maybeInitialized; } | ||
| }, | ||
| y: { | ||
| initialize() { | ||
| return new Promise(res => { setTimeout(() => { | ||
| additionalInitializer(); | ||
| res(); | ||
| }, 400) }) | ||
| } | ||
| } | ||
| }) | ||
| }); | ||
|
|
||
| const res = await server.executeOperation({ query: "query { hello }"}); | ||
|
|
||
| expect(res.data?.hello).toBe(expectedMessage); | ||
| expect(additionalInitializer).toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('make datasources available on resolver contexts', async () => { | ||
| const message = 'hi from dataSource'; | ||
| const getData = jest.fn(() => message); | ||
|
|
||
| const server = new ApolloServerBase({ | ||
| typeDefs, | ||
| resolvers: { | ||
| Query: { | ||
| hello(_, __, context) { | ||
| return context.dataSources.x.getData(); | ||
| } | ||
| }, | ||
| }, | ||
| dataSources: () => ({ x: { initialize() {}, getData } }) | ||
| }); | ||
|
|
||
| const res = await server.executeOperation({ query: "query { hello }"}); | ||
|
|
||
| expect(getData).toHaveBeenCalled(); | ||
| expect(res.data?.hello).toBe(message); | ||
| }); | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.