Skip to content

Commit

Permalink
RemoteGraphQLDataSource uses make-fetch-happen by default (#1284)
Browse files Browse the repository at this point in the history
* RemoteGraphQLDataSource uses `make-fetch-happen` by default
  • Loading branch information
clenfest authored Dec 15, 2021
1 parent e88ce2a commit 9c7b00e
Show file tree
Hide file tree
Showing 7 changed files with 19 additions and 10 deletions.
1 change: 1 addition & 0 deletions gateway-js/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

> 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.
- RemoteGraphQLDataSource will now use `make-fetch-happen` by default rather than `node-fetch` [PR #1284](https://github.com/apollographql/federation/pull/1284)
- __NOOP__: Fix OOB testing w.r.t. nock hygiene. Pushed error reporting endpoint responsibilities up into the gateway class, but there should be no effect on the runtime at all. [PR #1309](https://github.com/apollographql/federation/pull/1309)

## v2.0.0-alpha.2
Expand Down
4 changes: 3 additions & 1 deletion gateway-js/src/__mocks__/make-fetch-happen-fetcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ interface MakeFetchHappenMock extends jest.MockedFunction<typeof fetch> {
}

const mockMakeFetchHappen = jest.fn(fetcher) as unknown as MakeFetchHappenMock;
const defaults = () => mockMakeFetchHappen;

mockMakeFetchHappen.mockResponseOnce = (
data?: BodyInit,
Expand Down Expand Up @@ -47,7 +48,8 @@ mockMakeFetchHappen.mockJSONResponseOnce = (
};

const makeFetchMock = {
makeFetchHappenFetcher: mockMakeFetchHappen,
fetch: mockMakeFetchHappen,
defaults,
};

jest.doMock('make-fetch-happen', () => makeFetchMock);
Expand Down
2 changes: 1 addition & 1 deletion gateway-js/src/__tests__/gateway/buildService.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { fetch } from '../../__mocks__/apollo-server-env';
import { fetch } from '../../__mocks__/make-fetch-happen-fetcher';
import { ApolloServerBase as ApolloServer } from 'apollo-server-core';

import { RemoteGraphQLDataSource } from '../../datasources/RemoteGraphQLDataSource';
Expand Down
2 changes: 1 addition & 1 deletion gateway-js/src/__tests__/gateway/composedSdl.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { fetch } from '../../__mocks__/make-fetch-happen-fetcher';
import { ApolloGateway } from '@apollo/gateway';
import { ApolloServer } from 'apollo-server';
import { fetch } from '../../__mocks__/apollo-server-env';
import { getTestingSupergraphSdl } from '../execution-utils';

async function getSupergraphSdlGatewayServer() {
Expand Down
2 changes: 1 addition & 1 deletion gateway-js/src/__tests__/gateway/executor.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { fetch } from '../../__mocks__/make-fetch-happen-fetcher';
import gql from 'graphql-tag';
import { ApolloGateway } from '../../';
import { fixtures } from 'apollo-federation-integration-testsuite';
import { Logger } from 'apollo-server-types';
import { fetch } from '../../__mocks__/apollo-server-env';

let logger: {
warn: jest.MockedFunction<Logger['warn']>,
Expand Down
10 changes: 8 additions & 2 deletions gateway-js/src/datasources/RemoteGraphQLDataSource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,24 @@ import { isObject } from '../utilities/predicates';
import { GraphQLDataSource, GraphQLDataSourceProcessOptions, GraphQLDataSourceRequestKind } from './types';
import createSHA from 'apollo-server-core/dist/utils/createSHA';
import { parseCacheControlHeader } from './parseCacheControlHeader';

import fetcher from 'make-fetch-happen';
export class RemoteGraphQLDataSource<
TContext extends Record<string, any> = Record<string, any>,
> implements GraphQLDataSource<TContext>
{
fetcher: typeof fetch = fetch;
fetcher: typeof fetch;

constructor(
config?: Partial<RemoteGraphQLDataSource<TContext>> &
object &
ThisType<RemoteGraphQLDataSource<TContext>>,
) {
this.fetcher = fetcher.defaults({
// although this is the default, we want to take extra care and be very
// explicity to ensure that mutations cannot be retried. please leave this
// intact.
retry: false,
});
if (config) {
return Object.assign(this, config);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { fetch } from '../../__mocks__/apollo-server-env';
import { makeFetchHappenFetcher } from '../../__mocks__/make-fetch-happen-fetcher';
import { fetch as customFetcher } from '../../__mocks__/apollo-server-env';
import { fetch } from '../../__mocks__/make-fetch-happen-fetcher';

import {
ApolloError,
Expand Down Expand Up @@ -263,8 +263,8 @@ describe('fetcher', () => {
expect(data).toEqual({ injected: true });
});

it('supports a custom fetcher, like `make-fetch-happen`', async () => {
const injectedFetch = makeFetchHappenFetcher.mockJSONResponseOnce({
it('supports a custom fetcher, like `node-fetch`', async () => {
const injectedFetch = customFetcher.mockJSONResponseOnce({
data: { me: 'james' },
});
const DataSource = new RemoteGraphQLDataSource({
Expand Down

0 comments on commit 9c7b00e

Please sign in to comment.