Skip to content

Commit 489139c

Browse files
jhamptonabernix
andauthored
gateway: Support custom fetcher for RemoteGraphQLDataSource. (#4149)
Co-authored-by: Jesse Rosenberger <[email protected]>
1 parent 960e073 commit 489139c

File tree

3 files changed

+33
-2
lines changed

3 files changed

+33
-2
lines changed

packages/apollo-gateway/CHANGELOG.md

+5-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@
44

55
> The changes noted within this `vNEXT` section have not been released yet. New PRs and commits which introduce changes should include an entry in this `vNEXT` section as part of their development. When a release is being prepared, a new header will be (manually) created below and the appropriate changes within that release will be moved into the new section.
66
7-
- _Nothing yet! Stay tuned._
7+
- __NEW__: Provide the ability to pass a custom `fetcher` during `RemoteGraphQLDataSource` construction to be used when executing operations against downstream services. Providing a custom `fetcher` may be necessary to accommodate more advanced needs, e.g., configuring custom TLS certificates for internal services. [PR #4149](https://github.com/apollographql/apollo-server/pull/4149)
8+
9+
The `fetcher` specified should be a compliant implementor of the [Fetch API standard](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API). This addition compliments, though is still orthognonal to, similar behavior originally introduced in [#3783](https://github.com/apollographql/apollo-server/pull/3783), which allowed customization of the implementation used to fetch _gateway configuration and federated SDL from services_ in managed and unmanaged modes, but didn't affect the communication that takes place during _operation execution_.
10+
11+
For now, the default `fetcher` will remain the same ([`node-fetch`](https://npm.im/node-fetch)) implementation. A future major-version bump will update it to be consistent with other feature-rich implementations of the Fetch API which are used elsewhere in the Apollo Server stack where we use [`make-fetch-happen`](https://npm.im/make-fetch-happen). In all likelihood, `ApolloGateway` will pass its own `fetcher` to the `RemoteGraphQLDataSource` during service initialization.
812

913
## 0.16.0
1014

packages/apollo-gateway/src/datasources/RemoteGraphQLDataSource.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ import { GraphQLDataSource } from './types';
2020
import createSHA from 'apollo-server-core/dist/utils/createSHA';
2121

2222
export class RemoteGraphQLDataSource<TContext extends Record<string, any> = Record<string, any>> implements GraphQLDataSource<TContext> {
23+
fetcher: typeof fetch = fetch;
24+
2325
constructor(
2426
config?: Partial<RemoteGraphQLDataSource<TContext>> &
2527
object &
@@ -144,7 +146,8 @@ export class RemoteGraphQLDataSource<TContext extends Record<string, any> = Reco
144146
});
145147

146148
try {
147-
const httpResponse = await fetch(httpRequest);
149+
// Use our local `fetcher` to allow for fetch injection
150+
const httpResponse = await this.fetcher(httpRequest);
148151

149152
if (!httpResponse.ok) {
150153
throw await this.errorFromResponse(httpResponse);

packages/apollo-gateway/src/datasources/__tests__/RemoteGraphQLDataSource.test.ts

+24
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
import { RemoteGraphQLDataSource } from '../RemoteGraphQLDataSource';
1010
import { Headers } from 'apollo-server-env';
1111
import { GraphQLRequestContext } from 'apollo-server-types';
12+
import { Response } from '../../../../../../apollo-tooling/packages/apollo-env/lib';
1213

1314
beforeEach(() => {
1415
fetch.mockReset();
@@ -238,6 +239,29 @@ describe('constructing requests', () => {
238239
});
239240
});
240241

242+
describe('fetcher', () => {
243+
it('uses a custom provided `fetcher`', async () => {
244+
const injectedFetch = fetch.mockJSONResponseOnce({ data: { injected: true } });
245+
const DataSource = new RemoteGraphQLDataSource({
246+
url: 'https://api.example.com/foo',
247+
fetcher: injectedFetch,
248+
});
249+
250+
const { data } = await DataSource.process({
251+
request: {
252+
query: '{ me { name } }',
253+
variables: { id: '1' },
254+
},
255+
context: {},
256+
});
257+
258+
expect(injectedFetch).toHaveBeenCalled();
259+
expect(data).toEqual({injected: true});
260+
261+
});
262+
263+
});
264+
241265
describe('willSendRequest', () => {
242266
it('allows for modifying variables', async () => {
243267
const DataSource = new RemoteGraphQLDataSource({

0 commit comments

Comments
 (0)