Skip to content

Commit

Permalink
RemoteGraphQLDataSource uses make-fetch-happen by default
Browse files Browse the repository at this point in the history
As the subject says. Couple of things that I'm not sure about.

1. When I make a call to `.defaults()` do I need to pass any extra parameters? Didn't look like the `node-fetch` version did, but I want to make sure we don't want to add any extra headers or anything. For reasons stated in #192, I'm being careful and turning off retries completely, but I believe this was also the case in `node-fetch`

2. Obviously this needs to get into the CHANGELOG, but I'm assuming we do that when we cut a new version?
  • Loading branch information
clenfest committed Dec 9, 2021
1 parent 766564e commit 97b7bda
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 9 deletions.
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
7 changes: 5 additions & 2 deletions gateway-js/src/datasources/RemoteGraphQLDataSource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,21 @@ 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({
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 @@ -264,7 +264,7 @@ describe('fetcher', () => {
});

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

0 comments on commit 97b7bda

Please sign in to comment.