From 88bcaf5d63e2d46f9db605b57b871d7d766b56ff Mon Sep 17 00:00:00 2001 From: Chris Lenfest Date: Wed, 15 Dec 2021 12:10:50 -0600 Subject: [PATCH] RemoteGraphQLDataSource uses `make-fetch-happen` by default (#1284) * RemoteGraphQLDataSource uses `make-fetch-happen` by default --- gateway-js/CHANGELOG.md | 1 + gateway-js/src/__mocks__/make-fetch-happen-fetcher.ts | 4 +++- gateway-js/src/__tests__/gateway/buildService.test.ts | 2 +- gateway-js/src/__tests__/gateway/composedSdl.test.ts | 2 +- gateway-js/src/__tests__/gateway/executor.test.ts | 2 +- gateway-js/src/datasources/RemoteGraphQLDataSource.ts | 10 ++++++++-- .../__tests__/RemoteGraphQLDataSource.test.ts | 8 ++++---- 7 files changed, 19 insertions(+), 10 deletions(-) diff --git a/gateway-js/CHANGELOG.md b/gateway-js/CHANGELOG.md index 1f93504a4..e992282cf 100644 --- a/gateway-js/CHANGELOG.md +++ b/gateway-js/CHANGELOG.md @@ -12,6 +12,7 @@ ## v0.45.0 +- 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) - __BREAKING__: Remove legacy GCS fetcher for schema updates. If you're currently opted-in to the backwards compatibility provided by setting `schemaConfigDeliveryEndpoint: null`, you may be affected by this update. Please see the PR for additional details. [PR #1225](https://github.com/apollographql/federation/pull/1225) - __Multi-cloud Uplink capability__ [PR #1283](https://github.com/apollographql/federation/pull/1283): now, by default two separate Uplink services will be used for schema fetching, the system will round-robin and if one service fails, a retry will occur and the other service will be called. diff --git a/gateway-js/src/__mocks__/make-fetch-happen-fetcher.ts b/gateway-js/src/__mocks__/make-fetch-happen-fetcher.ts index d91018d28..3b949c2c2 100644 --- a/gateway-js/src/__mocks__/make-fetch-happen-fetcher.ts +++ b/gateway-js/src/__mocks__/make-fetch-happen-fetcher.ts @@ -20,6 +20,7 @@ interface MakeFetchHappenMock extends jest.MockedFunction { } const mockMakeFetchHappen = jest.fn(fetcher) as unknown as MakeFetchHappenMock; +const defaults = () => mockMakeFetchHappen; mockMakeFetchHappen.mockResponseOnce = ( data?: BodyInit, @@ -47,7 +48,8 @@ mockMakeFetchHappen.mockJSONResponseOnce = ( }; const makeFetchMock = { - makeFetchHappenFetcher: mockMakeFetchHappen, + fetch: mockMakeFetchHappen, + defaults, }; jest.doMock('make-fetch-happen', () => makeFetchMock); diff --git a/gateway-js/src/__tests__/gateway/buildService.test.ts b/gateway-js/src/__tests__/gateway/buildService.test.ts index 6b6e30994..8c64e171e 100644 --- a/gateway-js/src/__tests__/gateway/buildService.test.ts +++ b/gateway-js/src/__tests__/gateway/buildService.test.ts @@ -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'; diff --git a/gateway-js/src/__tests__/gateway/composedSdl.test.ts b/gateway-js/src/__tests__/gateway/composedSdl.test.ts index 1f8ac1956..2c30c815c 100644 --- a/gateway-js/src/__tests__/gateway/composedSdl.test.ts +++ b/gateway-js/src/__tests__/gateway/composedSdl.test.ts @@ -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() { diff --git a/gateway-js/src/__tests__/gateway/executor.test.ts b/gateway-js/src/__tests__/gateway/executor.test.ts index 440f6f3a9..92d4a9cbb 100644 --- a/gateway-js/src/__tests__/gateway/executor.test.ts +++ b/gateway-js/src/__tests__/gateway/executor.test.ts @@ -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, diff --git a/gateway-js/src/datasources/RemoteGraphQLDataSource.ts b/gateway-js/src/datasources/RemoteGraphQLDataSource.ts index 13785f281..4a082db0e 100644 --- a/gateway-js/src/datasources/RemoteGraphQLDataSource.ts +++ b/gateway-js/src/datasources/RemoteGraphQLDataSource.ts @@ -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 = Record, > implements GraphQLDataSource { - fetcher: typeof fetch = fetch; + fetcher: typeof fetch; constructor( config?: Partial> & object & ThisType>, ) { + 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); } diff --git a/gateway-js/src/datasources/__tests__/RemoteGraphQLDataSource.test.ts b/gateway-js/src/datasources/__tests__/RemoteGraphQLDataSource.test.ts index 0873e1e23..3feada64b 100644 --- a/gateway-js/src/datasources/__tests__/RemoteGraphQLDataSource.test.ts +++ b/gateway-js/src/datasources/__tests__/RemoteGraphQLDataSource.test.ts @@ -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, @@ -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({