@@ -2,13 +2,15 @@ import { GraphQLError, type DocumentNode } from 'graphql';
22import type { ApolloServerPlugin , HTTPGraphQLRequest } from '@apollo/server' ;
33import {
44 autoDisposeSymbol ,
5+ createCDNArtifactFetcher ,
56 createHive as createHiveClient ,
6- createSupergraphSDLFetcher ,
77 HiveClient ,
88 HivePluginOptions ,
99 isHiveClient ,
10- SupergraphSDLFetcherOptions ,
10+ joinUrl ,
11+ type CircuitBreakerConfiguration ,
1112} from '@graphql-hive/core' ;
13+ import { Logger } from '@graphql-hive/logger' ;
1214import { version } from './version.js' ;
1315
1416export {
@@ -17,34 +19,83 @@ export {
1719 createServicesFetcher ,
1820 createSupergraphSDLFetcher ,
1921} from '@graphql-hive/core' ;
22+
23+ /** @deprecated Use {CreateSupergraphManagerArgs} instead */
2024export type { SupergraphSDLFetcherOptions } from '@graphql-hive/core' ;
2125
22- export function createSupergraphManager ( {
23- pollIntervalInMs,
24- ...superGraphFetcherOptions
25- } : { pollIntervalInMs ?: number } & SupergraphSDLFetcherOptions ) {
26- pollIntervalInMs = pollIntervalInMs ?? 30_000 ;
27- const fetchSupergraph = createSupergraphSDLFetcher ( superGraphFetcherOptions ) ;
26+ /**
27+ * Configuration for {createSupergraphManager}.
28+ */
29+ export type CreateSupergraphManagerArgs = {
30+ /**
31+ * The artifact endpoint to poll.
32+ * E.g. `https://cdn.graphql-hive.com/<uuid>/supergraph`
33+ */
34+ endpoint : string | [ string , string ] ;
35+ /**
36+ * The CDN access key for fetching artifact.
37+ */
38+ key : string ;
39+ logger ?: Logger ;
40+ /**
41+ * The supergraph poll interval in milliseconds
42+ * Default: 30_000
43+ */
44+ pollIntervalInMs ?: number ;
45+ /** Circuit breaker configuration override. */
46+ circuitBreaker ?: CircuitBreakerConfiguration ;
47+ fetchImplementation ?: typeof fetch ;
48+ /**
49+ * Client name override
50+ * Default: `@graphql-hive/apollo`
51+ */
52+ name ?: string ;
53+ /**
54+ * Client version override
55+ * Default: currents package version
56+ */
57+ version ?: string ;
58+ } ;
59+
60+ export function createSupergraphManager ( args : CreateSupergraphManagerArgs ) {
61+ const logger = args . logger ?? new Logger ( { level : false } ) ;
62+ const pollIntervalInMs = args . pollIntervalInMs ?? 30_000 ;
63+ let endpoints = Array . isArray ( args . endpoint ) ? args . endpoint : [ args . endpoint ] ;
64+
65+ const endpoint = endpoints . map ( endpoint =>
66+ endpoint . endsWith ( '/supergraph' ) ? endpoint : joinUrl ( endpoint , 'supergraph' ) ,
67+ ) ;
68+
69+ const artifactsFetcher = createCDNArtifactFetcher ( {
70+ endpoint : endpoint as [ string , string ] ,
71+ accessKey : args . key ,
72+ client : {
73+ name : args . name ?? '@graphql-hive/apollo' ,
74+ version : args . version ?? version ,
75+ } ,
76+ logger,
77+ fetch : args . fetchImplementation ,
78+ circuitBreaker : args . circuitBreaker ,
79+ } ) ;
80+
2881 let timer : ReturnType < typeof setTimeout > | null = null ;
2982
3083 return {
3184 async initialize ( hooks : { update ( supergraphSdl : string ) : void } ) : Promise < {
3285 supergraphSdl : string ;
3386 cleanup ?: ( ) => Promise < void > ;
3487 } > {
35- const initialResult = await fetchSupergraph ( ) ;
88+ const initialResult = await artifactsFetcher . fetch ( ) ;
3689
3790 function poll ( ) {
3891 timer = setTimeout ( async ( ) => {
3992 try {
40- const result = await fetchSupergraph ( ) ;
41- if ( result . supergraphSdl ) {
42- hooks . update ?.( result . supergraphSdl ) ;
93+ const result = await artifactsFetcher . fetch ( ) ;
94+ if ( result . contents ) {
95+ hooks . update ?.( result . contents ) ;
4396 }
4497 } catch ( error ) {
45- console . error (
46- `Failed to update supergraph: ${ error instanceof Error ? error . message : error } ` ,
47- ) ;
98+ logger . error ( { error } , `Failed to update supergraph.` ) ;
4899 }
49100 poll ( ) ;
50101 } , pollIntervalInMs ) ;
@@ -53,11 +104,12 @@ export function createSupergraphManager({
53104 poll ( ) ;
54105
55106 return {
56- supergraphSdl : initialResult . supergraphSdl ,
107+ supergraphSdl : initialResult . contents ,
57108 cleanup : async ( ) => {
58109 if ( timer ) {
59110 clearTimeout ( timer ) ;
60111 }
112+ artifactsFetcher . dispose ( ) ;
61113 } ,
62114 } ;
63115 } ,
0 commit comments