Skip to content

Commit d6b1bd4

Browse files
apollo-server-core: Add fetcher option to schema and usage reporting (#5179)
1 parent 087862f commit d6b1bd4

File tree

5 files changed

+20
-4
lines changed

5 files changed

+20
-4
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ The version headers in this history reflect the versions of Apollo Server itself
1212
> 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. With few exceptions, the format of the entry should follow convention (i.e., prefix with package name, use markdown `backtick formatting` for package names and code, suffix with a link to the change-set à la `[PR #YYY](https://link/pull/YYY)`, etc.). 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.
1313
1414
- `apollo-server-core`: Fix a race condition where schema reporting could lead to a delay at process shutdown. [PR #5222](https://github.com/apollographql/apollo-server/pull/5222)
15+
- `apollo-server-core`: Allow the Fetch API implementation to be overridden for the schema reporting and usage reporting plugins via a new `fetcher` option. [PR #5179](https://github.com/apollographql/apollo-server/pull/5179)
1516

1617
## v2.24.1
1718

packages/apollo-server-core/src/plugin/schemaReporting/index.ts

+7
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import os from 'os';
22
import type { InternalApolloServerPlugin } from '../internalPlugin';
33
import { v4 as uuidv4 } from 'uuid';
44
import { printSchema, validateSchema, buildSchema } from 'graphql';
5+
import type { fetch } from 'apollo-server-env';
56
import { SchemaReporter } from './schemaReporter';
67
import createSHA from '../../utils/createSHA';
78
import { schemaIsFederated } from '../schemaIsFederated';
@@ -48,13 +49,18 @@ export interface ApolloServerPluginSchemaReportingOptions {
4849
* Apollo use.
4950
*/
5051
endpointUrl?: string;
52+
/**
53+
* Specifies which Fetch API implementation to use when reporting schemas.
54+
*/
55+
fetcher?: typeof fetch;
5156
}
5257

5358
export function ApolloServerPluginSchemaReporting(
5459
{
5560
initialDelayMaxMs,
5661
overrideReportedSchema,
5762
endpointUrl,
63+
fetcher,
5864
}: ApolloServerPluginSchemaReportingOptions = Object.create(null),
5965
): InternalApolloServerPlugin {
6066
const bootId = uuidv4();
@@ -162,6 +168,7 @@ export function ApolloServerPluginSchemaReporting(
162168
Math.random() * (initialDelayMaxMs ?? 10_000),
163169
),
164170
fallbackReportingDelayInMs: 20_000,
171+
fetcher,
165172
});
166173

167174
schemaReporter.start();

packages/apollo-server-core/src/plugin/schemaReporting/schemaReporter.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ export class SchemaReporter {
4949
private readonly logger: Logger;
5050
private readonly initialReportingDelayInMs: number;
5151
private readonly fallbackReportingDelayInMs: number;
52+
private readonly fetcher: typeof fetch;
5253

5354
private isStopped: boolean;
5455
private pollTimer?: NodeJS.Timer;
@@ -62,6 +63,7 @@ export class SchemaReporter {
6263
logger: Logger;
6364
initialReportingDelayInMs: number;
6465
fallbackReportingDelayInMs: number;
66+
fetcher?: typeof fetch;
6567
}) {
6668
this.headers = new Headers();
6769
this.headers.set('Content-Type', 'application/json');
@@ -85,6 +87,7 @@ export class SchemaReporter {
8587
this.logger = options.logger;
8688
this.initialReportingDelayInMs = options.initialReportingDelayInMs;
8789
this.fallbackReportingDelayInMs = options.fallbackReportingDelayInMs;
90+
this.fetcher = options.fetcher ?? fetch;
8891
}
8992

9093
public stopped(): Boolean {
@@ -224,7 +227,7 @@ export class SchemaReporter {
224227
body: JSON.stringify(request),
225228
});
226229

227-
const httpResponse = await fetch(httpRequest);
230+
const httpResponse = await this.fetcher(httpRequest);
228231

229232
if (!httpResponse.ok) {
230233
throw new Error(

packages/apollo-server-core/src/plugin/usageReporting/options.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import {
55
Logger,
66
GraphQLRequestContext,
77
} from 'apollo-server-types';
8-
import { RequestAgent } from 'apollo-server-env';
8+
import type { fetch, RequestAgent } from 'apollo-server-env';
99
import type { Trace } from 'apollo-reporting-protobuf';
1010

1111
export interface ApolloServerPluginUsageReportingOptions<TContext> {
@@ -156,6 +156,10 @@ export interface ApolloServerPluginUsageReportingOptions<TContext> {
156156
* Apollo.
157157
*/
158158
requestAgent?: RequestAgent | false;
159+
/**
160+
* Specifies which Fetch API implementation to use when sending usage reports.
161+
*/
162+
fetcher?: typeof fetch;
159163
/**
160164
* How often to send reports to Apollo. We'll also send reports when the
161165
* report gets big; see maxUncompressedReportSize.

packages/apollo-server-core/src/plugin/usageReporting/plugin.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -265,12 +265,13 @@ export function ApolloServerPluginUsageReporting<TContext>(
265265
});
266266
});
267267

268-
// Wrap fetch with async-retry for automatic retrying
268+
// Wrap fetcher with async-retry for automatic retrying
269+
const fetcher = options.fetcher ?? fetch;
269270
const response: Response = await retry(
270271
// Retry on network errors and 5xx HTTP
271272
// responses.
272273
async () => {
273-
const curResponse = await fetch(
274+
const curResponse = await fetcher(
274275
(options.endpointUrl ||
275276
'https://usage-reporting.api.apollographql.com') +
276277
'/api/ingress/traces',

0 commit comments

Comments
 (0)