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 committed Jan 13, 2022
1 parent 6c46a01 commit 88bcaf5
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 @@ -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.
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 88bcaf5

Please sign in to comment.