Skip to content

Commit

Permalink
Add option to specify fetcher for schema reporting
Browse files Browse the repository at this point in the history
  • Loading branch information
sachindshinde committed May 5, 2021
1 parent e040cda commit ab8bd85
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
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 { 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.
*/
experimental_fetcher?: typeof fetch;
}

export function ApolloServerPluginSchemaReporting(
{
initialDelayMaxMs,
overrideReportedSchema,
endpointUrl,
experimental_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: experimental_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 @@ -220,7 +223,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

0 comments on commit ab8bd85

Please sign in to comment.