-
Notifications
You must be signed in to change notification settings - Fork 123
feat(js-sdk): circuit breaker and cdn mirror for CDN apis #7287
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
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
5dd9752
feat: circuit breaker for supergraph fetcher
n1ru4l 8526743
persisted documents as well?
n1ru4l 058efa0
missing
n1ru4l ffb8fca
support multiple endpoints
n1ru4l 8d39a7b
improve circuit breaker api for artifacts fetcher
n1ru4l 38f4f69
mirror fallback with circuit breaker?
n1ru4l c7e7a9b
a bit of docs
n1ru4l eec7901
persisted document tests
n1ru4l 138723c
cdn artifact fetcher tests
n1ru4l c817142
oops
n1ru4l 28b880a
changeset
n1ru4l 1f6daa2
ooops deps
n1ru4l d265dc6
fix changeset
n1ru4l File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| --- | ||
| '@graphql-hive/core': minor | ||
| '@graphql-hive/apollo': minor | ||
| '@graphql-hive/yoga': minor | ||
| --- | ||
|
|
||
| **Persisted Documents Improvements** | ||
|
|
||
| Persisted documents now support specifying a mirror endpoint that will be used in case the main CDN | ||
| is unreachable. Provide an array of endpoints to the client configuration. | ||
|
|
||
| ```ts | ||
| import { createClient } from '@graphql-hive/core' | ||
|
|
||
| const client = createClient({ | ||
| experimental__persistedDocuments: { | ||
| cdn: { | ||
| endpoint: [ | ||
| 'https://cdn.graphql-hive.com/artifacts/v1/9fb37bc4-e520-4019-843a-0c8698c25688', | ||
| 'https://cdn-mirror.graphql-hive.com/artifacts/v1/9fb37bc4-e520-4019-843a-0c8698c25688' | ||
| ], | ||
| accessToken: '' | ||
| } | ||
| } | ||
| }) | ||
| ``` | ||
|
|
||
| In addition to that, the underlying logic for looking up documents now uses a circuit breaker. If a | ||
| single endpoint is unreachable, further lookups on that endpoint are skipped. | ||
|
|
||
| The behaviour of the circuit breaker can be customized via the `circuitBreaker` configuration. | ||
|
|
||
| ```ts | ||
| import { createClient } from '@graphql-hive/core' | ||
|
|
||
| const client = createClient({ | ||
| experimental__persistedDocuments: { | ||
| cdn: { | ||
| endpoint: [ | ||
| 'https://cdn.graphql-hive.com/artifacts/v1/9fb37bc4-e520-4019-843a-0c8698c25688', | ||
| 'https://cdn-mirror.graphql-hive.com/artifacts/v1/9fb37bc4-e520-4019-843a-0c8698c25688' | ||
| ], | ||
| accessToken: '' | ||
| }, | ||
| circuitBreaker: { | ||
| // open circuit if 50 percent of request result in an error | ||
| errorThresholdPercentage: 50, | ||
| // start monitoring the circuit after 10 requests | ||
| volumeThreshold: 10, | ||
| // time before the backend is tried again after the circuit is open | ||
| resetTimeout: 30_000 | ||
| } | ||
| } | ||
| }) | ||
| ``` | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| --- | ||
| '@graphql-hive/apollo': minor | ||
| --- | ||
|
|
||
| **Supergraph Manager Improvements** | ||
|
|
||
| Persisted documents now support specifying a mirror endpoint that will be used in case the main CDN | ||
| is unreachable. Provide an array of endpoints to the supergraph manager configuration. | ||
|
|
||
| ```ts | ||
| import { createSupergraphManager } from '@graphql-hive/apollo' | ||
|
|
||
| const supergraphManager = createSupergraphManager({ | ||
| endpoint: [ | ||
| 'https://cdn.graphql-hive.com/artifacts/v1/9fb37bc4-e520-4019-843a-0c8698c25688/supergraph', | ||
| 'https://cdn-mirror.graphql-hive.com/artifacts/v1/9fb37bc4-e520-4019-843a-0c8698c25688/supergraph' | ||
| ], | ||
| key: '' | ||
| }) | ||
| ``` | ||
|
|
||
| In addition to that, the underlying logic for looking up documents now uses a circuit breaker. If a | ||
| single endpoint is unreachable, further lookups on that endpoint are skipped. | ||
|
|
||
| ```ts | ||
| import { createSupergraphManager } from '@graphql-hive/apollo' | ||
|
|
||
| const supergraphManager = createSupergraphManager({ | ||
| endpoint: [ | ||
| 'https://cdn.graphql-hive.com/artifacts/v1/9fb37bc4-e520-4019-843a-0c8698c25688/supergraph', | ||
| 'https://cdn-mirror.graphql-hive.com/artifacts/v1/9fb37bc4-e520-4019-843a-0c8698c25688/supergraph' | ||
| ], | ||
| key: '', | ||
| circuitBreaker: { | ||
| // open circuit if 50 percent of request result in an error | ||
| errorThresholdPercentage: 50, | ||
| // start monitoring the circuit after 10 requests | ||
| volumeThreshold: 10, | ||
| // time before the backend is tried again after the circuit is open | ||
| resetTimeout: 30_000 | ||
| } | ||
| }) | ||
| ``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| --- | ||
| '@graphql-hive/core': minor | ||
| --- | ||
|
|
||
| **New CDN Artifact Fetcher** | ||
|
|
||
| We have a new interface for fetching CDN artifacts (such as supergraph and services) with a cache | ||
| from the CDN. This fetcher supports providing a mirror endpoint and comes with a circuit breaker | ||
| under the hood. | ||
|
|
||
| ```ts | ||
| const supergraphFetcher = createCDNArtifactFetcher({ | ||
| endpoint: [ | ||
| 'https://cdn.graphql-hive.com/artifacts/v1/9fb37bc4-e520-4019-843a-0c8698c25688', | ||
| 'https://cdn-mirror.graphql-hive.com/artifacts/v1/9fb37bc4-e520-4019-843a-0c8698c25688' | ||
| ], | ||
| accessKey: '' | ||
| }) | ||
|
|
||
| supergraphFetcher.fetch() | ||
| ``` | ||
|
|
||
| --- | ||
|
|
||
| `createSupergraphSDLFetcher` is now deprecated. Please upgrade to use `createCDNArtifactFetcher`. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,13 +2,15 @@ import { GraphQLError, type DocumentNode } from 'graphql'; | |
| import type { ApolloServerPlugin, HTTPGraphQLRequest } from '@apollo/server'; | ||
| import { | ||
| autoDisposeSymbol, | ||
| createCDNArtifactFetcher, | ||
| createHive as createHiveClient, | ||
| createSupergraphSDLFetcher, | ||
| HiveClient, | ||
| HivePluginOptions, | ||
| isHiveClient, | ||
| SupergraphSDLFetcherOptions, | ||
| joinUrl, | ||
| type CircuitBreakerConfiguration, | ||
| } from '@graphql-hive/core'; | ||
| import { Logger } from '@graphql-hive/logger'; | ||
| import { version } from './version.js'; | ||
|
|
||
| export { | ||
|
|
@@ -17,34 +19,83 @@ export { | |
| createServicesFetcher, | ||
| createSupergraphSDLFetcher, | ||
| } from '@graphql-hive/core'; | ||
|
|
||
| /** @deprecated Use {CreateSupergraphManagerArgs} instead */ | ||
| export type { SupergraphSDLFetcherOptions } from '@graphql-hive/core'; | ||
|
|
||
| export function createSupergraphManager({ | ||
| pollIntervalInMs, | ||
| ...superGraphFetcherOptions | ||
| }: { pollIntervalInMs?: number } & SupergraphSDLFetcherOptions) { | ||
| pollIntervalInMs = pollIntervalInMs ?? 30_000; | ||
| const fetchSupergraph = createSupergraphSDLFetcher(superGraphFetcherOptions); | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note: I introduced a new |
||
| /** | ||
| * Configuration for {createSupergraphManager}. | ||
| */ | ||
| export type CreateSupergraphManagerArgs = { | ||
| /** | ||
| * The artifact endpoint to poll. | ||
| * E.g. `https://cdn.graphql-hive.com/<uuid>/supergraph` | ||
| */ | ||
| endpoint: string | [string, string]; | ||
| /** | ||
| * The CDN access key for fetching artifact. | ||
| */ | ||
| key: string; | ||
| logger?: Logger; | ||
| /** | ||
| * The supergraph poll interval in milliseconds | ||
| * Default: 30_000 | ||
| */ | ||
| pollIntervalInMs?: number; | ||
| /** Circuit breaker configuration override. */ | ||
| circuitBreaker?: CircuitBreakerConfiguration; | ||
| fetchImplementation?: typeof fetch; | ||
| /** | ||
| * Client name override | ||
| * Default: `@graphql-hive/apollo` | ||
| */ | ||
| name?: string; | ||
| /** | ||
| * Client version override | ||
| * Default: currents package version | ||
| */ | ||
| version?: string; | ||
| }; | ||
|
|
||
| export function createSupergraphManager(args: CreateSupergraphManagerArgs) { | ||
| const logger = args.logger ?? new Logger({ level: false }); | ||
| const pollIntervalInMs = args.pollIntervalInMs ?? 30_000; | ||
| let endpoints = Array.isArray(args.endpoint) ? args.endpoint : [args.endpoint]; | ||
|
|
||
| const endpoint = endpoints.map(endpoint => | ||
| endpoint.endsWith('/supergraph') ? endpoint : joinUrl(endpoint, 'supergraph'), | ||
| ); | ||
|
|
||
| const artifactsFetcher = createCDNArtifactFetcher({ | ||
| endpoint: endpoint as [string, string], | ||
| accessKey: args.key, | ||
| client: { | ||
| name: args.name ?? '@graphql-hive/apollo', | ||
| version: args.version ?? version, | ||
| }, | ||
| logger, | ||
| fetch: args.fetchImplementation, | ||
| circuitBreaker: args.circuitBreaker, | ||
| }); | ||
|
|
||
| let timer: ReturnType<typeof setTimeout> | null = null; | ||
|
|
||
| return { | ||
| async initialize(hooks: { update(supergraphSdl: string): void }): Promise<{ | ||
| supergraphSdl: string; | ||
| cleanup?: () => Promise<void>; | ||
| }> { | ||
| const initialResult = await fetchSupergraph(); | ||
| const initialResult = await artifactsFetcher.fetch(); | ||
|
|
||
| function poll() { | ||
| timer = setTimeout(async () => { | ||
| try { | ||
| const result = await fetchSupergraph(); | ||
| if (result.supergraphSdl) { | ||
| hooks.update?.(result.supergraphSdl); | ||
| const result = await artifactsFetcher.fetch(); | ||
| if (result.contents) { | ||
| hooks.update?.(result.contents); | ||
| } | ||
| } catch (error) { | ||
| console.error( | ||
| `Failed to update supergraph: ${error instanceof Error ? error.message : error}`, | ||
| ); | ||
| logger.error({ error }, `Failed to update supergraph.`); | ||
| } | ||
| poll(); | ||
| }, pollIntervalInMs); | ||
|
|
@@ -53,11 +104,12 @@ export function createSupergraphManager({ | |
| poll(); | ||
|
|
||
| return { | ||
| supergraphSdl: initialResult.supergraphSdl, | ||
| supergraphSdl: initialResult.contents, | ||
| cleanup: async () => { | ||
| if (timer) { | ||
| clearTimeout(timer); | ||
| } | ||
| artifactsFetcher.dispose(); | ||
| }, | ||
| }; | ||
| }, | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't circuit breaker options be under the
cdnkey ? Since it's the CDN's circuit breaker ?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I remember I introduced
accessTokenandendpointundercdn, so it is obvious you need to provide credentials for the CDN.Should
fetchthen also be undercdnthen? I don't feel the strong need to move it there or not. For me it is just fine if it is a name space for credentials.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As you whish, I don't have strong opinion on it, I just found it surprising by reading the changelog :-)
Anyway, it is typed and documented, so not a big deal.