Skip to content

Commit

Permalink
gateway: Support custom fetcher for RemoteGraphQLDataSource. (#4149)
Browse files Browse the repository at this point in the history
Co-authored-by: Jesse Rosenberger <git@jro.cc>
  • Loading branch information
jhampton and abernix authored May 27, 2020
1 parent 960e073 commit 489139c
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 2 deletions.
6 changes: 5 additions & 1 deletion packages/apollo-gateway/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@

> 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.
- _Nothing yet! Stay tuned._
- __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)

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_.

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.

## 0.16.0

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import { GraphQLDataSource } from './types';
import createSHA from 'apollo-server-core/dist/utils/createSHA';

export class RemoteGraphQLDataSource<TContext extends Record<string, any> = Record<string, any>> implements GraphQLDataSource<TContext> {
fetcher: typeof fetch = fetch;

constructor(
config?: Partial<RemoteGraphQLDataSource<TContext>> &
object &
Expand Down Expand Up @@ -144,7 +146,8 @@ export class RemoteGraphQLDataSource<TContext extends Record<string, any> = Reco
});

try {
const httpResponse = await fetch(httpRequest);
// Use our local `fetcher` to allow for fetch injection
const httpResponse = await this.fetcher(httpRequest);

if (!httpResponse.ok) {
throw await this.errorFromResponse(httpResponse);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
import { RemoteGraphQLDataSource } from '../RemoteGraphQLDataSource';
import { Headers } from 'apollo-server-env';
import { GraphQLRequestContext } from 'apollo-server-types';
import { Response } from '../../../../../../apollo-tooling/packages/apollo-env/lib';

beforeEach(() => {
fetch.mockReset();
Expand Down Expand Up @@ -238,6 +239,29 @@ describe('constructing requests', () => {
});
});

describe('fetcher', () => {
it('uses a custom provided `fetcher`', async () => {
const injectedFetch = fetch.mockJSONResponseOnce({ data: { injected: true } });
const DataSource = new RemoteGraphQLDataSource({
url: 'https://api.example.com/foo',
fetcher: injectedFetch,
});

const { data } = await DataSource.process({
request: {
query: '{ me { name } }',
variables: { id: '1' },
},
context: {},
});

expect(injectedFetch).toHaveBeenCalled();
expect(data).toEqual({injected: true});

});

});

describe('willSendRequest', () => {
it('allows for modifying variables', async () => {
const DataSource = new RemoteGraphQLDataSource({
Expand Down

0 comments on commit 489139c

Please sign in to comment.