Repository navigation
Expand file tree
/
Copy pathresilientGraphqlClient.ts
More file actions
148 lines (128 loc) · 4.41 KB
/
Copy pathresilientGraphqlClient.ts
File metadata and controls
148 lines (128 loc) · 4.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
// src/libs/strapi/resilientGraphqlClient.ts
/**
* Strapi GraphQL client with quiet transport-failure handling.
*
* Mirrors @datum-cloud/strapi-revalidate's GraphQLStrapiClient but logs a single
* warning (no stack trace) when Strapi is unreachable so build/dev can fall back
* to stale cache without noisy errors.
*/
import type { GraphQLClientOptions } from '@datum-cloud/strapi-revalidate';
interface GraphQLResponse<T> {
data: T;
errors?: Array<{ message: string }>;
}
let unavailableLogged = false;
function logStrapiUnavailable(url: string, reason: string): void {
if (unavailableLogged) return;
unavailableLogged = true;
console.warn(
`[strapi-runtime] Strapi not available at ${url} (${reason}), using fallback cache where available`
);
}
function getTransportErrorReason(err: unknown): string {
if (err instanceof Error) {
if (err.name === 'AbortError') return 'timeout';
const cause = err.cause;
if (cause instanceof Error) {
const errno = cause as NodeJS.ErrnoException;
if (errno.code) return errno.code;
return cause.message;
}
return err.message;
}
return 'unknown error';
}
function isTransportError(err: unknown): boolean {
if (!(err instanceof Error)) return false;
if (err.name === 'AbortError') return true;
if (err.message === 'fetch failed') return true;
if (/^Strapi 5\d{2} /.test(err.message)) return true;
return false;
}
function sleep(ms: number): Promise<void> {
return new Promise((resolve) => setTimeout(resolve, ms));
}
function backoffMs(attempt: number): number {
return Math.min(200 * 2 ** attempt, 2000);
}
export class ResilientGraphQLStrapiClient {
private readonly options: GraphQLClientOptions;
constructor(options: GraphQLClientOptions) {
this.options = options;
}
async query<T>(query: string, variables: Record<string, unknown> = {}): Promise<T | null> {
const { retry } = this.options;
let lastError: unknown = null;
for (let attempt = 0; attempt <= retry; attempt++) {
try {
const result = await this.executeOnce<T>(query, variables);
if (result !== null) return result;
return null;
} catch (err) {
lastError = err;
if (this.options.debug) {
console.debug(
`[strapi-runtime] graphql attempt ${attempt + 1}/${retry + 1} failed:`,
getTransportErrorReason(err)
);
}
if (attempt < retry) {
await sleep(backoffMs(attempt));
}
}
}
if (lastError && isTransportError(lastError)) {
logStrapiUnavailable(this.options.url, getTransportErrorReason(lastError));
} else if (lastError instanceof Error && lastError.name === 'AbortError') {
logStrapiUnavailable(this.options.url, 'timeout');
} else if (lastError) {
console.error('Error fetching from Strapi:', lastError);
}
return null;
}
private async executeOnce<T>(
query: string,
variables: Record<string, unknown>
): Promise<T | null> {
const controller = new AbortController();
const timeoutId = setTimeout(() => controller.abort(), this.options.timeout);
try {
const response = await fetch(`${this.options.url.replace(/\/$/, '')}/graphql`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
...(this.options.token ? { Authorization: `Bearer ${this.options.token}` } : {}),
},
body: JSON.stringify({ query, variables }),
signal: controller.signal,
});
if (!response.ok) {
const text = await response.text();
if (response.status >= 500) {
throw new Error(`Strapi ${response.status} ${response.statusText}`);
}
console.error(`Strapi GraphQL error: ${response.status} ${response.statusText}`, text);
return null;
}
const result = (await response.json()) as GraphQLResponse<T>;
if (result.errors && result.errors.length > 0) {
console.error('Strapi GraphQL errors:', result.errors);
return null;
}
return result.data;
} finally {
clearTimeout(timeoutId);
}
}
}
export function createResilientStrapiClient(
config: Pick<GraphQLClientOptions, 'url' | 'token' | 'timeout' | 'retry' | 'debug'>
): ResilientGraphQLStrapiClient {
return new ResilientGraphQLStrapiClient({
url: config.url,
token: config.token,
timeout: config.timeout,
retry: config.retry,
debug: config.debug,
});
}