Skip to content

Commit fa99626

Browse files
committed
feat: circuit breaker for supergraph fetcher
1 parent 791c025 commit fa99626

File tree

3 files changed

+64
-11
lines changed

3 files changed

+64
-11
lines changed

packages/libraries/apollo/src/index.ts

Lines changed: 54 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@ import { GraphQLError, type DocumentNode } from 'graphql';
22
import type { ApolloServerPlugin, HTTPGraphQLRequest } from '@apollo/server';
33
import {
44
autoDisposeSymbol,
5+
CDNArtifactFetcherCircuitBreakerConfiguration,
6+
createCDNArtifactFetcher,
57
createHive as createHiveClient,
6-
createSupergraphSDLFetcher,
78
HiveClient,
89
HivePluginOptions,
910
isHiveClient,
10-
SupergraphSDLFetcherOptions,
11+
joinUrl,
12+
Logger,
1113
} from '@graphql-hive/core';
1214
import { version } from './version.js';
1315

@@ -19,12 +21,53 @@ export {
1921
} from '@graphql-hive/core';
2022
export type { SupergraphSDLFetcherOptions } from '@graphql-hive/core';
2123

22-
export function createSupergraphManager({
23-
pollIntervalInMs,
24-
...superGraphFetcherOptions
25-
}: { pollIntervalInMs?: number } & SupergraphSDLFetcherOptions) {
26-
pollIntervalInMs = pollIntervalInMs ?? 30_000;
27-
const fetchSupergraph = createSupergraphSDLFetcher(superGraphFetcherOptions);
24+
export function createSupergraphManager(args: {
25+
/**
26+
* The supergraph poll interval in milliseconds
27+
* Default: 30_000
28+
*/
29+
pollIntervalInMs?: number;
30+
/**
31+
* The artifact endpoint to poll.
32+
* E.g. `https://cdn.graphql-hive.com/<uuid>/supergraph`
33+
*/
34+
endpoint: string;
35+
/**
36+
* The CDN access key for fetching artifact.
37+
*/
38+
key: string;
39+
logger?: Logger;
40+
/** Circuit breaker configuration override. */
41+
circuitBreaker?: CDNArtifactFetcherCircuitBreakerConfiguration;
42+
fetchImplementation?: typeof fetch;
43+
/**
44+
* Client name override
45+
* Default: `@graphql-hive/apollo`
46+
*/
47+
name?: string;
48+
/**
49+
* Client version override
50+
* Default: currents package version
51+
*/
52+
version?: string;
53+
}) {
54+
const pollIntervalInMs = args.pollIntervalInMs ?? 30_000;
55+
const endpoint = args.endpoint.endsWith('/supergraph')
56+
? args.endpoint
57+
: joinUrl(args.endpoint, 'supergraph');
58+
59+
const fetchSupergraph = createCDNArtifactFetcher({
60+
endpoint,
61+
accessKey: args.key,
62+
client: {
63+
name: args.name ?? '@graphql-hive/apollo',
64+
version: args.version ?? version,
65+
},
66+
logger: args.logger,
67+
fetch: args.fetchImplementation,
68+
circuitBreaker: args.circuitBreaker,
69+
});
70+
2871
let timer: ReturnType<typeof setTimeout> | null = null;
2972

3073
return {
@@ -38,8 +81,8 @@ export function createSupergraphManager({
3881
timer = setTimeout(async () => {
3982
try {
4083
const result = await fetchSupergraph();
41-
if (result.supergraphSdl) {
42-
hooks.update?.(result.supergraphSdl);
84+
if (result.contents) {
85+
hooks.update?.(result.contents);
4386
}
4487
} catch (error) {
4588
console.error(
@@ -53,7 +96,7 @@ export function createSupergraphManager({
5396
poll();
5497

5598
return {
56-
supergraphSdl: initialResult.supergraphSdl,
99+
supergraphSdl: initialResult.contents,
57100
cleanup: async () => {
58101
if (timer) {
59102
clearTimeout(timer);

packages/libraries/core/src/client/supergraph.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ import { http } from './http-client.js';
33
import type { Logger } from './types.js';
44
import { createHash, joinUrl } from './utils.js';
55

6+
/**
7+
* @deprecated Please use {createCDNArtifactFetcher} instead of createSupergraphSDLFetcher.
8+
*/
69
export interface SupergraphSDLFetcherOptions {
710
endpoint: string;
811
key: string;
@@ -12,6 +15,9 @@ export interface SupergraphSDLFetcherOptions {
1215
version?: string;
1316
}
1417

18+
/**
19+
* @deprecated Please use {createCDNArtifactFetcher} instead.
20+
*/
1521
export function createSupergraphSDLFetcher(options: SupergraphSDLFetcherOptions) {
1622
let cacheETag: string | null = null;
1723
let cached: {

packages/libraries/core/src/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,7 @@ export { isHiveClient, isAsyncIterable, createHash, joinUrl } from './client/uti
1313
export { http, URL } from './client/http-client.js';
1414
export { createSupergraphSDLFetcher } from './client/supergraph.js';
1515
export type { SupergraphSDLFetcherOptions } from './client/supergraph.js';
16+
export {
17+
createCDNArtifactFetcher,
18+
type CDNArtifactFetcherCircuitBreakerConfiguration,
19+
} from './client/artifacts.js';

0 commit comments

Comments
 (0)