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

apollo-server-core: Add option to specify fetcher for schema reporting #5179

Merged
merged 6 commits into from May 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -12,6 +12,7 @@ The version headers in this history reflect the versions of Apollo Server itself
> 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.

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

## v2.24.1

Expand Down
Expand Up @@ -2,6 +2,7 @@ import os from 'os';
import type { InternalApolloServerPlugin } from '../internalPlugin';
import { v4 as uuidv4 } from 'uuid';
import { printSchema, validateSchema, buildSchema } from 'graphql';
import type { fetch } from 'apollo-server-env';
import { SchemaReporter } from './schemaReporter';
import createSHA from '../../utils/createSHA';
import { schemaIsFederated } from '../schemaIsFederated';
Expand Down Expand Up @@ -48,13 +49,18 @@ export interface ApolloServerPluginSchemaReportingOptions {
* Apollo use.
*/
endpointUrl?: string;
/**
* Specifies which Fetch API implementation to use when reporting schemas.
*/
fetcher?: typeof fetch;
}

export function ApolloServerPluginSchemaReporting(
{
initialDelayMaxMs,
overrideReportedSchema,
endpointUrl,
fetcher,
}: ApolloServerPluginSchemaReportingOptions = Object.create(null),
): InternalApolloServerPlugin {
const bootId = uuidv4();
Expand Down Expand Up @@ -162,6 +168,7 @@ export function ApolloServerPluginSchemaReporting(
Math.random() * (initialDelayMaxMs ?? 10_000),
),
fallbackReportingDelayInMs: 20_000,
fetcher,
});

schemaReporter.start();
Expand Down
Expand Up @@ -49,6 +49,7 @@ export class SchemaReporter {
private readonly logger: Logger;
private readonly initialReportingDelayInMs: number;
private readonly fallbackReportingDelayInMs: number;
private readonly fetcher: typeof fetch;

private isStopped: boolean;
private pollTimer?: NodeJS.Timer;
Expand All @@ -62,6 +63,7 @@ export class SchemaReporter {
logger: Logger;
initialReportingDelayInMs: number;
fallbackReportingDelayInMs: number;
fetcher?: typeof fetch;
}) {
this.headers = new Headers();
this.headers.set('Content-Type', 'application/json');
Expand All @@ -85,6 +87,7 @@ export class SchemaReporter {
this.logger = options.logger;
this.initialReportingDelayInMs = options.initialReportingDelayInMs;
this.fallbackReportingDelayInMs = options.fallbackReportingDelayInMs;
this.fetcher = options.fetcher ?? fetch;
}

public stopped(): Boolean {
Expand Down Expand Up @@ -224,7 +227,7 @@ export class SchemaReporter {
body: JSON.stringify(request),
});

const httpResponse = await fetch(httpRequest);
const httpResponse = await this.fetcher(httpRequest);

if (!httpResponse.ok) {
throw new Error(
Expand Down
Expand Up @@ -5,7 +5,7 @@ import {
Logger,
GraphQLRequestContext,
} from 'apollo-server-types';
import { RequestAgent } from 'apollo-server-env';
import type { fetch, RequestAgent } from 'apollo-server-env';
import type { Trace } from 'apollo-reporting-protobuf';

export interface ApolloServerPluginUsageReportingOptions<TContext> {
Expand Down Expand Up @@ -156,6 +156,10 @@ export interface ApolloServerPluginUsageReportingOptions<TContext> {
* Apollo.
*/
requestAgent?: RequestAgent | false;
/**
* Specifies which Fetch API implementation to use when sending usage reports.
*/
fetcher?: typeof fetch;
/**
* How often to send reports to Apollo. We'll also send reports when the
* report gets big; see maxUncompressedReportSize.
Expand Down
Expand Up @@ -265,12 +265,13 @@ export function ApolloServerPluginUsageReporting<TContext>(
});
});

// Wrap fetch with async-retry for automatic retrying
// Wrap fetcher with async-retry for automatic retrying
const fetcher = options.fetcher ?? fetch;
const response: Response = await retry(
// Retry on network errors and 5xx HTTP
// responses.
async () => {
const curResponse = await fetch(
const curResponse = await fetcher(
(options.endpointUrl ||
'https://usage-reporting.api.apollographql.com') +
'/api/ingress/traces',
Expand Down