Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RemoteGraphQLDataSource uses make-fetch-happen by default #1284

Merged
merged 4 commits into from
Dec 15, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions gateway-js/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

> 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)

## v2.0.0-alpha.2

- Conditional schema update based on ifAfterId [PR #1152](https://github.com/apollographql/federation/pull/1152)
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