-
Notifications
You must be signed in to change notification settings - Fork 104
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move createSupergraphSDLFetcher to /core package (#5367)
- Loading branch information
1 parent
1eb06c2
commit a896642
Showing
9 changed files
with
248 additions
and
215 deletions.
There are no files selected for viewing
This file contains 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,5 @@ | ||
--- | ||
'@graphql-hive/yoga': minor | ||
--- | ||
|
||
Add createSupergraphSDLFetcher to /yoga |
This file contains 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,6 @@ | ||
--- | ||
'@graphql-hive/apollo': patch | ||
'@graphql-hive/core': patch | ||
--- | ||
|
||
Move createSupergraphSDLFetcher to @graphql-hive/core package |
This file contains 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 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 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,71 @@ | ||
import { version } from '../version.js'; | ||
import { http } from './http-client.js'; | ||
import type { Logger } from './types.js'; | ||
import { createHash, joinUrl } from './utils.js'; | ||
|
||
export interface SupergraphSDLFetcherOptions { | ||
endpoint: string; | ||
key: string; | ||
logger?: Logger; | ||
} | ||
|
||
export function createSupergraphSDLFetcher(options: SupergraphSDLFetcherOptions) { | ||
let cacheETag: string | null = null; | ||
let cached: { | ||
id: string; | ||
supergraphSdl: string; | ||
} | null = null; | ||
const endpoint = options.endpoint.endsWith('/supergraph') | ||
? options.endpoint | ||
: joinUrl(options.endpoint, 'supergraph'); | ||
|
||
return function supergraphSDLFetcher(): Promise<{ id: string; supergraphSdl: string }> { | ||
const headers: { | ||
[key: string]: string; | ||
} = { | ||
'X-Hive-CDN-Key': options.key, | ||
'User-Agent': `hive-client/${version}`, | ||
}; | ||
|
||
if (cacheETag) { | ||
headers['If-None-Match'] = cacheETag; | ||
} | ||
|
||
return http | ||
.get(endpoint, { | ||
headers, | ||
isRequestOk: response => response.status === 304 || response.ok, | ||
retry: { | ||
retries: 10, | ||
maxTimeout: 200, | ||
minTimeout: 1, | ||
}, | ||
logger: options.logger, | ||
}) | ||
.then(async response => { | ||
if (response.ok) { | ||
const supergraphSdl = await response.text(); | ||
const result = { | ||
id: await createHash('SHA-256').update(supergraphSdl).digest('base64'), | ||
supergraphSdl, | ||
}; | ||
|
||
const etag = response.headers.get('etag'); | ||
if (etag) { | ||
cached = result; | ||
cacheETag = etag; | ||
} | ||
|
||
return result; | ||
} | ||
|
||
if (response.status === 304 && cached !== null) { | ||
return cached; | ||
} | ||
|
||
throw new Error( | ||
`Failed to GET ${endpoint}, received: ${response.status} ${response.statusText ?? 'Internal Server Error'}`, | ||
); | ||
}); | ||
}; | ||
} |
This file contains 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 |
---|---|---|
@@ -1,8 +1,14 @@ | ||
export * from './normalize/operation.js'; | ||
export type { HivePluginOptions, HiveClient, CollectUsageCallback } from './client/types.js'; | ||
export type { | ||
HivePluginOptions, | ||
HiveClient, | ||
CollectUsageCallback, | ||
Logger, | ||
} from './client/types.js'; | ||
export { createSchemaFetcher, createServicesFetcher } from './client/gateways.js'; | ||
export { createHive, autoDisposeSymbol } from './client/client.js'; | ||
export { atLeastOnceSampler } from './client/samplers.js'; | ||
export { isHiveClient, isAsyncIterable, createHash, joinUrl } from './client/utils.js'; | ||
export { http, URL } from './client/http-client.js'; | ||
export type { Logger } from './client/types.js'; | ||
export { createSupergraphSDLFetcher } from './client/supergraph.js'; | ||
export type { SupergraphSDLFetcherOptions } from './client/supergraph.js'; |
This file contains 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 |
---|---|---|
@@ -1 +1 @@ | ||
export const version = '0.5.0'; | ||
export const version = '0.7.0'; |
Oops, something went wrong.