@jackchuka/gql-ingest 1.1.0 → 1.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +125 -20
- package/bin/cli.js +44 -41
- package/dist/config.d.ts +12 -2
- package/dist/config.d.ts.map +1 -1
- package/dist/graphql-client.d.ts +6 -2
- package/dist/graphql-client.d.ts.map +1 -1
- package/dist/mapper.d.ts +2 -2
- package/dist/mapper.d.ts.map +1 -1
- package/dist/metrics.d.ts +5 -0
- package/dist/metrics.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/cli.ts +24 -22
- package/src/config.test.ts +74 -7
- package/src/config.ts +37 -4
- package/src/graphql-client.test.ts +127 -1
- package/src/graphql-client.ts +132 -32
- package/src/mapper.test.ts +4 -4
- package/src/mapper.ts +13 -8
- package/src/metrics.ts +22 -0
package/src/cli.ts
CHANGED
|
@@ -2,10 +2,20 @@ import { Command } from "commander";
|
|
|
2
2
|
import { GraphQLClientWrapper } from "./graphql-client";
|
|
3
3
|
import { DataMapper } from "./mapper";
|
|
4
4
|
import { MetricsCollector } from "./metrics";
|
|
5
|
-
import { loadConfig, getEntityConfig } from "./config";
|
|
5
|
+
import { loadConfig, getEntityConfig, getRetryConfig } from "./config";
|
|
6
6
|
import { DependencyResolver } from "./dependency-resolver";
|
|
7
7
|
import { basename } from "path";
|
|
8
8
|
|
|
9
|
+
// Utility function to chunk array into smaller arrays
|
|
10
|
+
function chunkArray<T>(array: T[], chunkSize: number): T[][] {
|
|
11
|
+
if (chunkSize <= 0) return [array];
|
|
12
|
+
const chunks: T[][] = [];
|
|
13
|
+
for (let i = 0; i < array.length; i += chunkSize) {
|
|
14
|
+
chunks.push(array.slice(i, i + chunkSize));
|
|
15
|
+
}
|
|
16
|
+
return chunks;
|
|
17
|
+
}
|
|
18
|
+
|
|
9
19
|
const program = new Command();
|
|
10
20
|
|
|
11
21
|
program
|
|
@@ -81,10 +91,10 @@ program
|
|
|
81
91
|
}
|
|
82
92
|
|
|
83
93
|
// Process entities in dependency-aware waves
|
|
84
|
-
if (config.parallelProcessing.
|
|
85
|
-
await processEntitiesInWaves(mappingPaths, resolver, mapper, config);
|
|
86
|
-
} else {
|
|
94
|
+
if (config.parallelProcessing.entityConcurrency === 1) {
|
|
87
95
|
await processEntitiesSequentially(mappingPaths, mapper, config);
|
|
96
|
+
} else {
|
|
97
|
+
await processEntitiesInWaves(mappingPaths, resolver, mapper, config);
|
|
88
98
|
}
|
|
89
99
|
|
|
90
100
|
metrics.finishProcessing();
|
|
@@ -104,7 +114,8 @@ async function processEntitiesSequentially(
|
|
|
104
114
|
try {
|
|
105
115
|
const entityName = basename(configPath, ".json");
|
|
106
116
|
const entityConfig = getEntityConfig(entityName, config);
|
|
107
|
-
|
|
117
|
+
const retryConfig = getRetryConfig(entityName, config);
|
|
118
|
+
await mapper.processEntity(configPath, entityConfig, retryConfig);
|
|
108
119
|
} catch (error) {
|
|
109
120
|
console.warn(`Warning: Could not process ${configPath}:`, error);
|
|
110
121
|
}
|
|
@@ -129,27 +140,18 @@ async function processEntitiesInWaves(
|
|
|
129
140
|
`Wave ${wave.wave + 1}: Processing entities [${wave.entities.join(", ")}]`
|
|
130
141
|
);
|
|
131
142
|
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
const entityConfig = getEntityConfig(entityName, config);
|
|
139
|
-
await mapper.processEntity(configPath, entityConfig);
|
|
140
|
-
} catch (error) {
|
|
141
|
-
console.warn(`Warning: Could not process ${configPath}:`, error);
|
|
142
|
-
}
|
|
143
|
-
}
|
|
144
|
-
}
|
|
145
|
-
} else {
|
|
146
|
-
// Process entities in this wave concurrently
|
|
147
|
-
const entityPromises = wave.entities.map(async (entityName) => {
|
|
143
|
+
// Process entities in controlled batches based on entityConcurrency
|
|
144
|
+
const entityConcurrency = config.parallelProcessing.entityConcurrency;
|
|
145
|
+
const chunks = chunkArray(wave.entities, entityConcurrency);
|
|
146
|
+
|
|
147
|
+
for (const chunk of chunks) {
|
|
148
|
+
const entityPromises = chunk.map(async (entityName) => {
|
|
148
149
|
const configPath = pathMap.get(entityName);
|
|
149
150
|
if (configPath) {
|
|
150
151
|
try {
|
|
151
152
|
const entityConfig = getEntityConfig(entityName, config);
|
|
152
|
-
|
|
153
|
+
const retryConfig = getRetryConfig(entityName, config);
|
|
154
|
+
await mapper.processEntity(configPath, entityConfig, retryConfig);
|
|
153
155
|
} catch (error) {
|
|
154
156
|
console.warn(`Warning: Could not process ${configPath}:`, error);
|
|
155
157
|
}
|
package/src/config.test.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import fs from "fs";
|
|
2
2
|
import path from "path";
|
|
3
|
-
import { loadConfig, getEntityConfig, DEFAULT_CONFIG } from "./config";
|
|
3
|
+
import { loadConfig, getEntityConfig, getRetryConfig, DEFAULT_CONFIG } from "./config";
|
|
4
4
|
|
|
5
5
|
jest.mock("fs");
|
|
6
6
|
const mockFs = fs as jest.Mocked<typeof fs>;
|
|
@@ -32,7 +32,7 @@ describe("Configuration", () => {
|
|
|
32
32
|
const yamlContent = `
|
|
33
33
|
parallelProcessing:
|
|
34
34
|
concurrency: 5
|
|
35
|
-
|
|
35
|
+
entityConcurrency: 3
|
|
36
36
|
|
|
37
37
|
entityConfig:
|
|
38
38
|
users:
|
|
@@ -49,7 +49,7 @@ entityDependencies:
|
|
|
49
49
|
const config = loadConfig(testConfigDir);
|
|
50
50
|
|
|
51
51
|
expect(config.parallelProcessing.concurrency).toBe(5);
|
|
52
|
-
expect(config.parallelProcessing.
|
|
52
|
+
expect(config.parallelProcessing.entityConcurrency).toBe(3);
|
|
53
53
|
expect(config.entityConfig.users.concurrency).toBe(2);
|
|
54
54
|
expect(config.entityConfig.users.preserveRowOrder).toBe(true);
|
|
55
55
|
expect(config.entityDependencies.products).toEqual(["users"]);
|
|
@@ -71,7 +71,7 @@ entityConfig:
|
|
|
71
71
|
|
|
72
72
|
// Should merge with defaults
|
|
73
73
|
expect(config.parallelProcessing.concurrency).toBe(10);
|
|
74
|
-
expect(config.parallelProcessing.
|
|
74
|
+
expect(config.parallelProcessing.entityConcurrency).toBe(1); // default
|
|
75
75
|
expect(config.parallelProcessing.preserveRowOrder).toBe(true); // default
|
|
76
76
|
});
|
|
77
77
|
|
|
@@ -112,11 +112,17 @@ entityConfig:
|
|
|
112
112
|
|
|
113
113
|
describe("getEntityConfig", () => {
|
|
114
114
|
const globalConfig = {
|
|
115
|
+
retry: {
|
|
116
|
+
maxAttempts: 3,
|
|
117
|
+
baseDelay: 1000,
|
|
118
|
+
maxDelay: 30000,
|
|
119
|
+
exponentialBackoff: true,
|
|
120
|
+
retryableStatusCodes: [408, 429, 500, 502, 503, 504],
|
|
121
|
+
},
|
|
115
122
|
parallelProcessing: {
|
|
116
123
|
concurrency: 10,
|
|
117
|
-
|
|
124
|
+
entityConcurrency: 3,
|
|
118
125
|
preserveRowOrder: false,
|
|
119
|
-
preserveEntityOrder: false,
|
|
120
126
|
},
|
|
121
127
|
entityConfig: {
|
|
122
128
|
users: {
|
|
@@ -140,7 +146,7 @@ entityConfig:
|
|
|
140
146
|
const entityConfig = getEntityConfig("products", globalConfig);
|
|
141
147
|
|
|
142
148
|
expect(entityConfig.concurrency).toBe(20); // overridden
|
|
143
|
-
expect(entityConfig.
|
|
149
|
+
expect(entityConfig.entityConcurrency).toBe(3); // from global
|
|
144
150
|
expect(entityConfig.preserveRowOrder).toBe(false); // from global
|
|
145
151
|
});
|
|
146
152
|
|
|
@@ -202,4 +208,65 @@ entityConfig:
|
|
|
202
208
|
consoleSpy.mockRestore();
|
|
203
209
|
});
|
|
204
210
|
});
|
|
211
|
+
|
|
212
|
+
describe("getRetryConfig", () => {
|
|
213
|
+
const globalConfig = {
|
|
214
|
+
retry: {
|
|
215
|
+
maxAttempts: 3,
|
|
216
|
+
baseDelay: 1000,
|
|
217
|
+
maxDelay: 30000,
|
|
218
|
+
exponentialBackoff: true,
|
|
219
|
+
retryableStatusCodes: [408, 429, 500, 502, 503, 504],
|
|
220
|
+
},
|
|
221
|
+
parallelProcessing: {
|
|
222
|
+
concurrency: 10,
|
|
223
|
+
entityConcurrency: 3,
|
|
224
|
+
preserveRowOrder: false,
|
|
225
|
+
},
|
|
226
|
+
entityConfig: {
|
|
227
|
+
important: {
|
|
228
|
+
retry: {
|
|
229
|
+
maxAttempts: 5,
|
|
230
|
+
baseDelay: 500,
|
|
231
|
+
},
|
|
232
|
+
},
|
|
233
|
+
fast: {
|
|
234
|
+
retry: {
|
|
235
|
+
maxAttempts: 1,
|
|
236
|
+
},
|
|
237
|
+
},
|
|
238
|
+
},
|
|
239
|
+
entityDependencies: {},
|
|
240
|
+
};
|
|
241
|
+
|
|
242
|
+
it("should return global retry config for entity without overrides", () => {
|
|
243
|
+
const retryConfig = getRetryConfig("regular", globalConfig);
|
|
244
|
+
|
|
245
|
+
expect(retryConfig).toEqual(globalConfig.retry);
|
|
246
|
+
});
|
|
247
|
+
|
|
248
|
+
it("should merge entity retry overrides with global config", () => {
|
|
249
|
+
const retryConfig = getRetryConfig("important", globalConfig);
|
|
250
|
+
|
|
251
|
+
expect(retryConfig.maxAttempts).toBe(5); // overridden
|
|
252
|
+
expect(retryConfig.baseDelay).toBe(500); // overridden
|
|
253
|
+
expect(retryConfig.maxDelay).toBe(30000); // from global
|
|
254
|
+
expect(retryConfig.exponentialBackoff).toBe(true); // from global
|
|
255
|
+
expect(retryConfig.retryableStatusCodes).toEqual([408, 429, 500, 502, 503, 504]); // from global
|
|
256
|
+
});
|
|
257
|
+
|
|
258
|
+
it("should handle partial retry overrides", () => {
|
|
259
|
+
const retryConfig = getRetryConfig("fast", globalConfig);
|
|
260
|
+
|
|
261
|
+
expect(retryConfig.maxAttempts).toBe(1); // overridden
|
|
262
|
+
expect(retryConfig.baseDelay).toBe(1000); // from global
|
|
263
|
+
expect(retryConfig.maxDelay).toBe(30000); // from global
|
|
264
|
+
});
|
|
265
|
+
|
|
266
|
+
it("should handle entity with no retry config", () => {
|
|
267
|
+
const retryConfig = getRetryConfig("undefined-entity", globalConfig);
|
|
268
|
+
|
|
269
|
+
expect(retryConfig).toEqual(globalConfig.retry);
|
|
270
|
+
});
|
|
271
|
+
});
|
|
205
272
|
});
|
package/src/config.ts
CHANGED
|
@@ -4,17 +4,26 @@ import * as yaml from "js-yaml";
|
|
|
4
4
|
|
|
5
5
|
export interface ParallelProcessingConfig {
|
|
6
6
|
concurrency: number;
|
|
7
|
-
|
|
7
|
+
entityConcurrency: number;
|
|
8
8
|
preserveRowOrder: boolean;
|
|
9
|
-
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export interface RetryConfig {
|
|
12
|
+
maxAttempts: number;
|
|
13
|
+
baseDelay: number;
|
|
14
|
+
maxDelay: number;
|
|
15
|
+
exponentialBackoff: boolean;
|
|
16
|
+
retryableStatusCodes: number[];
|
|
10
17
|
}
|
|
11
18
|
|
|
12
19
|
export interface EntityConfig {
|
|
13
20
|
concurrency?: number;
|
|
14
21
|
preserveRowOrder?: boolean;
|
|
22
|
+
retry?: Partial<RetryConfig>;
|
|
15
23
|
}
|
|
16
24
|
|
|
17
25
|
export interface ProcessingConfig {
|
|
26
|
+
retry: RetryConfig;
|
|
18
27
|
parallelProcessing: ParallelProcessingConfig;
|
|
19
28
|
entityConfig: Record<string, EntityConfig>;
|
|
20
29
|
entityDependencies: Record<string, string[]>;
|
|
@@ -24,14 +33,22 @@ export interface FullConfig extends ProcessingConfig {
|
|
|
24
33
|
// Future: additional config sections can be added here
|
|
25
34
|
}
|
|
26
35
|
|
|
36
|
+
export const DEFAULT_RETRY_CONFIG: RetryConfig = {
|
|
37
|
+
maxAttempts: 3,
|
|
38
|
+
baseDelay: 1000,
|
|
39
|
+
maxDelay: 30000,
|
|
40
|
+
exponentialBackoff: true,
|
|
41
|
+
retryableStatusCodes: [408, 429, 500, 502, 503, 504],
|
|
42
|
+
};
|
|
43
|
+
|
|
27
44
|
export const DEFAULT_PARALLEL_CONFIG: ParallelProcessingConfig = {
|
|
28
45
|
concurrency: 1,
|
|
29
|
-
|
|
46
|
+
entityConcurrency: 1,
|
|
30
47
|
preserveRowOrder: true,
|
|
31
|
-
preserveEntityOrder: true,
|
|
32
48
|
};
|
|
33
49
|
|
|
34
50
|
export const DEFAULT_CONFIG: ProcessingConfig = {
|
|
51
|
+
retry: DEFAULT_RETRY_CONFIG,
|
|
35
52
|
parallelProcessing: DEFAULT_PARALLEL_CONFIG,
|
|
36
53
|
entityConfig: {},
|
|
37
54
|
entityDependencies: {},
|
|
@@ -60,6 +77,10 @@ export function loadConfig(configDir: string): ProcessingConfig {
|
|
|
60
77
|
|
|
61
78
|
function mergeWithDefaults(yamlConfig: Partial<FullConfig>): ProcessingConfig {
|
|
62
79
|
return {
|
|
80
|
+
retry: {
|
|
81
|
+
...DEFAULT_RETRY_CONFIG,
|
|
82
|
+
...(yamlConfig.retry || {}),
|
|
83
|
+
},
|
|
63
84
|
parallelProcessing: {
|
|
64
85
|
...DEFAULT_PARALLEL_CONFIG,
|
|
65
86
|
...(yamlConfig.parallelProcessing || {}),
|
|
@@ -90,3 +111,15 @@ export function getEntityConfig(
|
|
|
90
111
|
|
|
91
112
|
return finalConfig;
|
|
92
113
|
}
|
|
114
|
+
|
|
115
|
+
export function getRetryConfig(
|
|
116
|
+
entityName: string,
|
|
117
|
+
globalConfig: ProcessingConfig
|
|
118
|
+
): RetryConfig {
|
|
119
|
+
const entityOverrides = globalConfig.entityConfig[entityName]?.retry || {};
|
|
120
|
+
|
|
121
|
+
return {
|
|
122
|
+
...globalConfig.retry,
|
|
123
|
+
...entityOverrides,
|
|
124
|
+
};
|
|
125
|
+
}
|
|
@@ -63,7 +63,7 @@ describe("GraphQLClientWrapper", () => {
|
|
|
63
63
|
clientWrapper.executeMutation(mutation, variables)
|
|
64
64
|
).rejects.toThrow("GraphQL error");
|
|
65
65
|
|
|
66
|
-
expect(consoleSpy).toHaveBeenCalledWith("GraphQL mutation failed:", error);
|
|
66
|
+
expect(consoleSpy).toHaveBeenCalledWith("GraphQL mutation failed after 3 attempts:", error);
|
|
67
67
|
|
|
68
68
|
consoleSpy.mockRestore();
|
|
69
69
|
});
|
|
@@ -75,4 +75,130 @@ describe("GraphQLClientWrapper", () => {
|
|
|
75
75
|
|
|
76
76
|
expect(mockSetHeaders).toHaveBeenCalledWith(newHeaders);
|
|
77
77
|
});
|
|
78
|
+
|
|
79
|
+
describe("retry functionality", () => {
|
|
80
|
+
it("should retry on retryable status codes", async () => {
|
|
81
|
+
const mutation = "mutation { createUser(name: $name) { id } }";
|
|
82
|
+
const variables = { name: "John" };
|
|
83
|
+
const retryConfig = {
|
|
84
|
+
maxAttempts: 3,
|
|
85
|
+
baseDelay: 100,
|
|
86
|
+
maxDelay: 1000,
|
|
87
|
+
exponentialBackoff: false,
|
|
88
|
+
retryableStatusCodes: [500],
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
const serverError = new Error("Server Error");
|
|
92
|
+
(serverError as any).response = { status: 500 };
|
|
93
|
+
|
|
94
|
+
mockRequest
|
|
95
|
+
.mockRejectedValueOnce(serverError)
|
|
96
|
+
.mockRejectedValueOnce(serverError)
|
|
97
|
+
.mockResolvedValueOnce({ data: { result: "success" } });
|
|
98
|
+
|
|
99
|
+
const result = await clientWrapper.executeMutation(mutation, variables, retryConfig);
|
|
100
|
+
|
|
101
|
+
expect(mockRequest).toHaveBeenCalledTimes(3);
|
|
102
|
+
expect(result).toEqual({ data: { result: "success" } });
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
it("should not retry on non-retryable status codes", async () => {
|
|
106
|
+
const mutation = "mutation { createUser(name: $name) { id } }";
|
|
107
|
+
const variables = { name: "John" };
|
|
108
|
+
const retryConfig = {
|
|
109
|
+
maxAttempts: 3,
|
|
110
|
+
baseDelay: 100,
|
|
111
|
+
maxDelay: 1000,
|
|
112
|
+
exponentialBackoff: false,
|
|
113
|
+
retryableStatusCodes: [500],
|
|
114
|
+
};
|
|
115
|
+
|
|
116
|
+
const clientError = new Error("Bad Request");
|
|
117
|
+
(clientError as any).response = { status: 400 };
|
|
118
|
+
|
|
119
|
+
mockRequest.mockRejectedValueOnce(clientError);
|
|
120
|
+
|
|
121
|
+
await expect(
|
|
122
|
+
clientWrapper.executeMutation(mutation, variables, retryConfig)
|
|
123
|
+
).rejects.toThrow("Bad Request");
|
|
124
|
+
|
|
125
|
+
expect(mockRequest).toHaveBeenCalledTimes(1);
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
it("should retry on network errors (no response)", async () => {
|
|
129
|
+
const mutation = "mutation { createUser(name: $name) { id } }";
|
|
130
|
+
const variables = { name: "John" };
|
|
131
|
+
const retryConfig = {
|
|
132
|
+
maxAttempts: 2,
|
|
133
|
+
baseDelay: 100,
|
|
134
|
+
maxDelay: 1000,
|
|
135
|
+
exponentialBackoff: false,
|
|
136
|
+
retryableStatusCodes: [500],
|
|
137
|
+
};
|
|
138
|
+
|
|
139
|
+
const networkError = new Error("Network Error");
|
|
140
|
+
// No response property = network error
|
|
141
|
+
|
|
142
|
+
mockRequest
|
|
143
|
+
.mockRejectedValueOnce(networkError)
|
|
144
|
+
.mockResolvedValueOnce({ data: { result: "success" } });
|
|
145
|
+
|
|
146
|
+
const result = await clientWrapper.executeMutation(mutation, variables, retryConfig);
|
|
147
|
+
|
|
148
|
+
expect(mockRequest).toHaveBeenCalledTimes(2);
|
|
149
|
+
expect(result).toEqual({ data: { result: "success" } });
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
it("should fail after max attempts are exhausted", async () => {
|
|
153
|
+
const mutation = "mutation { createUser(name: $name) { id } }";
|
|
154
|
+
const variables = { name: "John" };
|
|
155
|
+
const retryConfig = {
|
|
156
|
+
maxAttempts: 2,
|
|
157
|
+
baseDelay: 100,
|
|
158
|
+
maxDelay: 1000,
|
|
159
|
+
exponentialBackoff: false,
|
|
160
|
+
retryableStatusCodes: [500],
|
|
161
|
+
};
|
|
162
|
+
|
|
163
|
+
const serverError = new Error("Server Error");
|
|
164
|
+
(serverError as any).response = { status: 500 };
|
|
165
|
+
|
|
166
|
+
mockRequest.mockRejectedValue(serverError);
|
|
167
|
+
|
|
168
|
+
await expect(
|
|
169
|
+
clientWrapper.executeMutation(mutation, variables, retryConfig)
|
|
170
|
+
).rejects.toThrow("Server Error");
|
|
171
|
+
|
|
172
|
+
expect(mockRequest).toHaveBeenCalledTimes(2);
|
|
173
|
+
});
|
|
174
|
+
|
|
175
|
+
it("should use exponential backoff when configured", async () => {
|
|
176
|
+
const mutation = "mutation { createUser(name: $name) { id } }";
|
|
177
|
+
const variables = { name: "John" };
|
|
178
|
+
const retryConfig = {
|
|
179
|
+
maxAttempts: 3,
|
|
180
|
+
baseDelay: 100,
|
|
181
|
+
maxDelay: 1000,
|
|
182
|
+
exponentialBackoff: true,
|
|
183
|
+
retryableStatusCodes: [500],
|
|
184
|
+
};
|
|
185
|
+
|
|
186
|
+
const serverError = new Error("Server Error");
|
|
187
|
+
(serverError as any).response = { status: 500 };
|
|
188
|
+
|
|
189
|
+
mockRequest
|
|
190
|
+
.mockRejectedValueOnce(serverError)
|
|
191
|
+
.mockRejectedValueOnce(serverError)
|
|
192
|
+
.mockResolvedValueOnce({ data: { result: "success" } });
|
|
193
|
+
|
|
194
|
+
const startTime = Date.now();
|
|
195
|
+
const result = await clientWrapper.executeMutation(mutation, variables, retryConfig);
|
|
196
|
+
const totalTime = Date.now() - startTime;
|
|
197
|
+
|
|
198
|
+
expect(mockRequest).toHaveBeenCalledTimes(3);
|
|
199
|
+
expect(result).toEqual({ data: { result: "success" } });
|
|
200
|
+
// Should have some delay for retries (base + exponential)
|
|
201
|
+
expect(totalTime).toBeGreaterThan(200); // At least 100ms base + 200ms exponential
|
|
202
|
+
});
|
|
203
|
+
});
|
|
78
204
|
});
|
package/src/graphql-client.ts
CHANGED
|
@@ -1,51 +1,151 @@
|
|
|
1
|
-
import { GraphQLClient } from
|
|
2
|
-
import { MetricsCollector } from
|
|
1
|
+
import { GraphQLClient } from "graphql-request";
|
|
2
|
+
import { MetricsCollector } from "./metrics";
|
|
3
|
+
import { DEFAULT_RETRY_CONFIG, RetryConfig } from "./config";
|
|
3
4
|
|
|
4
5
|
export class GraphQLClientWrapper {
|
|
5
6
|
private client: GraphQLClient;
|
|
6
7
|
private metrics?: MetricsCollector;
|
|
7
8
|
private verbose: boolean;
|
|
8
9
|
|
|
9
|
-
constructor(
|
|
10
|
+
constructor(
|
|
11
|
+
endpoint: string,
|
|
12
|
+
headers?: Record<string, string>,
|
|
13
|
+
metrics?: MetricsCollector,
|
|
14
|
+
verbose: boolean = false
|
|
15
|
+
) {
|
|
10
16
|
this.client = new GraphQLClient(endpoint, {
|
|
11
|
-
headers: headers || {}
|
|
17
|
+
headers: headers || {},
|
|
12
18
|
});
|
|
13
19
|
this.metrics = metrics;
|
|
14
20
|
this.verbose = verbose;
|
|
15
21
|
}
|
|
16
22
|
|
|
17
|
-
async executeMutation(
|
|
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
|
-
|
|
23
|
+
async executeMutation(
|
|
24
|
+
mutation: string,
|
|
25
|
+
variables: Record<string, any>,
|
|
26
|
+
retryConfig?: RetryConfig
|
|
27
|
+
): Promise<any> {
|
|
28
|
+
const config = retryConfig || DEFAULT_RETRY_CONFIG;
|
|
29
|
+
|
|
30
|
+
let lastError: any;
|
|
31
|
+
const totalStartTime = Date.now();
|
|
32
|
+
|
|
33
|
+
for (let attempt = 0; attempt < config.maxAttempts; attempt++) {
|
|
34
|
+
const attemptStartTime = Date.now();
|
|
35
|
+
|
|
36
|
+
try {
|
|
37
|
+
const result = await this.client.request(mutation, variables);
|
|
38
|
+
|
|
39
|
+
if (this.metrics) {
|
|
40
|
+
const duration = Date.now() - attemptStartTime;
|
|
41
|
+
this.metrics.recordRequestDuration(duration);
|
|
42
|
+
if (attempt > 0) {
|
|
43
|
+
this.metrics.recordRetrySuccess(attempt);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
if (this.verbose) {
|
|
48
|
+
const totalDuration = Date.now() - totalStartTime;
|
|
49
|
+
const retryInfo =
|
|
50
|
+
attempt > 0 ? ` (succeeded on attempt ${attempt + 1})` : "";
|
|
51
|
+
console.log(
|
|
52
|
+
`✓ GraphQL request completed in ${totalDuration}ms${retryInfo}:`,
|
|
53
|
+
result
|
|
54
|
+
);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
return result;
|
|
58
|
+
} catch (error: any) {
|
|
59
|
+
lastError = error;
|
|
60
|
+
const duration = Date.now() - attemptStartTime;
|
|
61
|
+
|
|
62
|
+
if (this.metrics) {
|
|
63
|
+
this.metrics.recordRequestDuration(duration);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
// Check if this is the last attempt
|
|
67
|
+
if (attempt === config.maxAttempts - 1) {
|
|
68
|
+
if (this.metrics && attempt > 0) {
|
|
69
|
+
this.metrics.recordRetryFailure(attempt);
|
|
70
|
+
}
|
|
71
|
+
break;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
// Check if error is retryable
|
|
75
|
+
if (!this.isRetryableError(error, config)) {
|
|
76
|
+
if (this.verbose) {
|
|
77
|
+
console.error(
|
|
78
|
+
`✗ GraphQL request failed with non-retryable error in ${duration}ms:`,
|
|
79
|
+
error
|
|
80
|
+
);
|
|
81
|
+
} else {
|
|
82
|
+
console.error("GraphQL mutation failed (non-retryable):", error);
|
|
83
|
+
}
|
|
84
|
+
throw error;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
// Calculate delay
|
|
88
|
+
const delay = this.calculateDelay(attempt, config);
|
|
89
|
+
|
|
90
|
+
if (this.verbose) {
|
|
91
|
+
console.log(
|
|
92
|
+
`⏳ GraphQL request failed (attempt ${attempt + 1}/${
|
|
93
|
+
config.maxAttempts
|
|
94
|
+
}), retrying in ${delay}ms...`
|
|
95
|
+
);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
// Wait before retry
|
|
99
|
+
await this.sleep(delay);
|
|
43
100
|
}
|
|
44
|
-
throw error;
|
|
45
101
|
}
|
|
102
|
+
|
|
103
|
+
// All retries exhausted
|
|
104
|
+
if (this.verbose) {
|
|
105
|
+
const totalDuration = Date.now() - totalStartTime;
|
|
106
|
+
console.error(
|
|
107
|
+
`✗ GraphQL request failed after ${config.maxAttempts} attempts in ${totalDuration}ms:`,
|
|
108
|
+
lastError
|
|
109
|
+
);
|
|
110
|
+
} else {
|
|
111
|
+
console.error(
|
|
112
|
+
`GraphQL mutation failed after ${config.maxAttempts} attempts:`,
|
|
113
|
+
lastError
|
|
114
|
+
);
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
throw lastError;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
private isRetryableError(error: any, config: RetryConfig): boolean {
|
|
121
|
+
// Network errors (no response)
|
|
122
|
+
if (!error.response) {
|
|
123
|
+
return true;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
// Check HTTP status codes
|
|
127
|
+
const status = error.response.status;
|
|
128
|
+
return config.retryableStatusCodes.includes(status);
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
private calculateDelay(attempt: number, config: RetryConfig): number {
|
|
132
|
+
if (!config.exponentialBackoff) {
|
|
133
|
+
return config.baseDelay;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
const exponentialDelay = config.baseDelay * Math.pow(2, attempt);
|
|
137
|
+
const cappedDelay = Math.min(exponentialDelay, config.maxDelay);
|
|
138
|
+
|
|
139
|
+
// Add jitter (±20% randomization)
|
|
140
|
+
const jitter = cappedDelay * 0.2 * (Math.random() - 0.5);
|
|
141
|
+
return Math.max(0, cappedDelay + jitter);
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
private sleep(ms: number): Promise<void> {
|
|
145
|
+
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
46
146
|
}
|
|
47
147
|
|
|
48
148
|
setHeaders(headers: Record<string, string>) {
|
|
49
149
|
this.client.setHeaders(headers);
|
|
50
150
|
}
|
|
51
|
-
}
|
|
151
|
+
}
|
package/src/mapper.test.ts
CHANGED
|
@@ -141,11 +141,11 @@ describe("DataMapper", () => {
|
|
|
141
141
|
expect(mockClient.executeMutation).toHaveBeenCalledWith(mockMutation, {
|
|
142
142
|
name: "John",
|
|
143
143
|
email: "john@example.com",
|
|
144
|
-
});
|
|
144
|
+
}, undefined);
|
|
145
145
|
expect(mockClient.executeMutation).toHaveBeenCalledWith(mockMutation, {
|
|
146
146
|
name: "Jane",
|
|
147
147
|
email: "jane@example.com",
|
|
148
|
-
});
|
|
148
|
+
}, undefined);
|
|
149
149
|
|
|
150
150
|
consoleSpy.mockRestore();
|
|
151
151
|
});
|
|
@@ -223,7 +223,7 @@ describe("DataMapper", () => {
|
|
|
223
223
|
name: "Widget",
|
|
224
224
|
price: "19.99",
|
|
225
225
|
sku: "W001",
|
|
226
|
-
});
|
|
226
|
+
}, undefined);
|
|
227
227
|
});
|
|
228
228
|
|
|
229
229
|
it("should handle missing CSV columns gracefully", async () => {
|
|
@@ -260,7 +260,7 @@ describe("DataMapper", () => {
|
|
|
260
260
|
expect(mockClient.executeMutation).toHaveBeenCalledWith(mockMutation, {
|
|
261
261
|
name: "John",
|
|
262
262
|
email: "john@example.com",
|
|
263
|
-
});
|
|
263
|
+
}, undefined);
|
|
264
264
|
});
|
|
265
265
|
|
|
266
266
|
it("should call metrics methods during successful processing", async () => {
|