Skip to content

Commit 07a8fcf

Browse files
committed
persisted document tests
1 parent c7e7a9b commit 07a8fcf

File tree

2 files changed

+16
-7
lines changed

2 files changed

+16
-7
lines changed

packages/libraries/core/src/client/http-client.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { abortSignalAny } from '@graphql-hive/signal';
44
import { crypto, fetch, URL } from '@whatwg-node/fetch';
55
import type { LegacyLogger } from './types';
66

7-
interface SharedConfig {
7+
export interface HttpCallConfig {
88
headers: Record<string, string>;
99
/**
1010
* timeout in milliseconds (for each single fetch call)
@@ -32,9 +32,9 @@ interface SharedConfig {
3232
*/
3333
type ResponseAssertFunction = (response: Response) => boolean;
3434

35-
type RetryOptions = Parameters<typeof asyncRetry>[1];
35+
export type RetryOptions = Parameters<typeof asyncRetry>[1];
3636

37-
function get(endpoint: string, config: SharedConfig) {
37+
function get(endpoint: string, config: HttpCallConfig) {
3838
return makeFetchCall(endpoint, {
3939
method: 'GET',
4040
headers: config.headers,
@@ -46,7 +46,7 @@ function get(endpoint: string, config: SharedConfig) {
4646
});
4747
}
4848

49-
function post(endpoint: string, data: string | Buffer, config: SharedConfig) {
49+
function post(endpoint: string, data: string | Buffer, config: HttpCallConfig) {
5050
return makeFetchCall(endpoint, {
5151
body: data,
5252
method: 'POST',
@@ -59,7 +59,7 @@ export const http = {
5959
post,
6060
};
6161

62-
function chooseLogger(logger: SharedConfig['logger']): Logger {
62+
function chooseLogger(logger: HttpCallConfig['logger']): Logger {
6363
if (!logger) {
6464
return new Logger({
6565
writers: [{ write() {} }],

packages/libraries/core/src/client/persisted-documents.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import LRU from 'tiny-lru';
33
import { Logger } from '@graphql-hive/logger';
44
import CircuitBreaker from '../circuit-breaker/circuit.js';
55
import { defaultCircuitBreakerConfiguration } from './circuit-breaker.js';
6-
import { http } from './http-client.js';
6+
import { http, HttpCallConfig } from './http-client.js';
77
import type { PersistedDocumentsConfiguration } from './types';
88

99
type HeadersObject = {
@@ -24,6 +24,8 @@ export function createPersistedDocuments(
2424
config: PersistedDocumentsConfiguration & {
2525
logger: Logger;
2626
fetch?: typeof fetch;
27+
retry?: HttpCallConfig['retry'];
28+
timeout?: HttpCallConfig['retry'];
2729
},
2830
): PersistedDocuments {
2931
const persistedDocumentsCache = LRU<string>(config.cache ?? 10_000);
@@ -60,6 +62,7 @@ export function createPersistedDocuments(
6062
isRequestOk,
6163
fetchImplementation: config.fetch,
6264
signal,
65+
retry: config.retry,
6366
})
6467
.then(async response => {
6568
if (response.status !== 200) {
@@ -95,14 +98,20 @@ export function createPersistedDocuments(
9598
.then(async () => {
9699
const cdnDocumentId = documentId.replaceAll('~', '/');
97100

101+
let lastError: unknown = null;
102+
98103
for (const breaker of circuitBreakers) {
99104
try {
100105
return await breaker.fire(cdnDocumentId);
101106
} catch (error: unknown) {
102107
config.logger.debug({ error });
108+
lastError = error;
103109
}
104110
}
105-
throw new Error('Failed to look up artifact.');
111+
if (lastError) {
112+
config.logger.error({ error: lastError });
113+
}
114+
throw new Error('Failed to look up persisted operation.');
106115
})
107116
.then(result => {
108117
persistedDocumentsCache.set(documentId, result);

0 commit comments

Comments
 (0)