-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow async initialization of data sources (#3639)
* Allow async initialization of dataSources * Update return type of DataSource.initialize * Add dataSource integration tests
- Loading branch information
1 parent
a86b6a0
commit afbb150
Showing
4 changed files
with
118 additions
and
7 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
105 changes: 105 additions & 0 deletions
105
packages/apollo-server-core/src/__tests__/dataSources.test.ts
This file contains 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,105 @@ | ||
import { ApolloServerBase } from '../ApolloServer'; | ||
import gql from 'graphql-tag'; | ||
|
||
const typeDefs = gql` | ||
type Query { | ||
hello: String | ||
} | ||
`; | ||
|
||
describe('ApolloServerBase dataSources', () => { | ||
it('initializes synchronous datasources from a datasource creator function', async () => { | ||
const initialize = jest.fn(); | ||
|
||
const server = new ApolloServerBase({ | ||
typeDefs, | ||
resolvers: { | ||
Query: { | ||
hello() { | ||
return 'world'; | ||
} | ||
} | ||
}, | ||
dataSources: () => ({ x: { initialize }, y: { initialize } }) | ||
}); | ||
|
||
await server.executeOperation({ query: "query { hello }"}); | ||
|
||
expect(initialize).toHaveBeenCalledTimes(2); | ||
}); | ||
|
||
it('initializes all async and sync datasources before calling resolvers', async () => { | ||
const INITIALIZE = "datasource initializer call"; | ||
const METHOD_CALL = "datasource method call"; | ||
|
||
const expectedCallOrder = [ | ||
INITIALIZE, | ||
INITIALIZE, | ||
INITIALIZE, | ||
METHOD_CALL | ||
]; | ||
|
||
const actualCallOrder: string[] = []; | ||
|
||
const server = new ApolloServerBase({ | ||
typeDefs, | ||
resolvers: { | ||
Query: { | ||
hello(_, __, context) { | ||
context.dataSources.x.getData(); | ||
return "world"; | ||
} | ||
}, | ||
}, | ||
dataSources: () => ({ | ||
x: { | ||
initialize() { | ||
return Promise.resolve().then( | ||
() => { actualCallOrder.push(INITIALIZE); } | ||
); | ||
}, | ||
getData() { actualCallOrder.push(METHOD_CALL); } | ||
}, | ||
y: { | ||
initialize() { | ||
return new Promise(res => { | ||
setTimeout(() => { | ||
actualCallOrder.push(INITIALIZE); | ||
res(); | ||
}, 0); | ||
}); | ||
}, | ||
}, | ||
z: { | ||
initialize() { actualCallOrder.push(INITIALIZE); } | ||
} | ||
}) | ||
}); | ||
|
||
await server.executeOperation({ query: "query { hello }"}); | ||
|
||
expect(actualCallOrder).toEqual(expectedCallOrder); | ||
}); | ||
|
||
it('makes 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 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