Skip to content

Commit 47cbef7

Browse files
committed
chore(datastore): clean up code to follow Amplify patterns
- Remove unnecessary comments - Simplify code structure - Follow minimal Amplify style - Clean up test files
1 parent cf0397b commit 47cbef7

3 files changed

Lines changed: 1 addition & 38 deletions

File tree

packages/datastore/__tests__/subscription-variables-edge-cases.test.ts

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ describe('Subscription Variables - Edge Cases & Safety', () => {
4444
const schema = createTestSchema();
4545
const sharedObject = { storeId: 'initial' };
4646

47-
// First call
4847
const cache = new WeakMap();
4948
const result1 = processSubscriptionVariables(
5049
schema.namespaces.user.models.Todo,
@@ -53,29 +52,24 @@ describe('Subscription Variables - Edge Cases & Safety', () => {
5352
cache,
5453
);
5554

56-
// Mutate the shared object
5755
sharedObject.storeId = 'mutated';
5856

59-
// Second call - should get cached value, not mutated
6057
const result2 = processSubscriptionVariables(
6158
schema.namespaces.user.models.Todo,
6259
TransformerMutationType.CREATE,
6360
sharedObject,
6461
cache,
6562
);
6663

67-
// Results should be equal (same cached object)
6864
expect(result1).toEqual(result2);
69-
// But changing the original shouldn't affect results
7065
expect(result2?.storeId).not.toBe('mutated');
7166
});
7267

7368
it('should handle circular references gracefully', () => {
7469
const schema = createTestSchema();
7570
const circular: any = { storeId: 'test' };
76-
circular.self = circular; // Create circular reference
71+
circular.self = circular;
7772

78-
// Should handle circular reference without crashing
7973
const cache = new WeakMap();
8074
const result = processSubscriptionVariables(
8175
schema.namespaces.user.models.Todo,
@@ -84,7 +78,6 @@ describe('Subscription Variables - Edge Cases & Safety', () => {
8478
cache,
8579
);
8680

87-
// Should return the object but not cache it (due to JSON.stringify failure)
8881
expect(result).toBeDefined();
8982
expect(result?.storeId).toBe('test');
9083
});
@@ -134,7 +127,6 @@ describe('Subscription Variables - Edge Cases & Safety', () => {
134127
it('should handle function that throws', () => {
135128
const schema = createTestSchema();
136129

137-
// Should not crash
138130
const cache = new WeakMap();
139131
const mockFn = () => {
140132
throw new Error('Function error');
@@ -180,7 +172,6 @@ describe('Subscription Variables - Edge Cases & Safety', () => {
180172
const schema = createTestSchema();
181173
const mockFn = jest.fn(() => ({ storeId: 'test' }));
182174

183-
// Call multiple times for same operation
184175
const cache = new WeakMap();
185176
for (let i = 0; i < 5; i++) {
186177
processSubscriptionVariables(
@@ -191,19 +182,16 @@ describe('Subscription Variables - Edge Cases & Safety', () => {
191182
);
192183
}
193184

194-
// Should only be called once
195185
expect(mockFn).toHaveBeenCalledTimes(1);
196186
expect(mockFn).toHaveBeenCalledWith(TransformerMutationType.CREATE);
197187

198-
// Call for different operation
199188
processSubscriptionVariables(
200189
schema.namespaces.user.models.Todo,
201190
TransformerMutationType.UPDATE,
202191
mockFn,
203192
cache,
204193
);
205194

206-
// Should be called again for new operation
207195
expect(mockFn).toHaveBeenCalledTimes(2);
208196
expect(mockFn).toHaveBeenCalledWith(TransformerMutationType.UPDATE);
209197
});
@@ -226,7 +214,6 @@ describe('Subscription Variables - Edge Cases & Safety', () => {
226214
},
227215
);
228216

229-
// First call
230217
let cache = new WeakMap();
231218
processSubscriptionVariables(
232219
schema.namespaces.user.models.Todo,
@@ -236,19 +223,16 @@ describe('Subscription Variables - Edge Cases & Safety', () => {
236223
);
237224
expect(mockFn).toHaveBeenCalledTimes(1);
238225

239-
// Stop processor (clears cache) - simulate by creating new cache
240226
await processor.stop();
241227
cache = new WeakMap();
242228

243-
// Call again after stop
244229
processSubscriptionVariables(
245230
schema.namespaces.user.models.Todo,
246231
TransformerMutationType.CREATE,
247232
mockFn,
248233
cache,
249234
);
250235

251-
// Should be called again since cache was cleared
252236
expect(mockFn).toHaveBeenCalledTimes(2);
253237
});
254238
});

packages/datastore/__tests__/subscription-variables.test.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,8 @@ describe('DataStore Subscription Variables', () => {
6565
customVariables,
6666
);
6767

68-
// Verify operation type and name
6968
expect(opType).toBe(TransformerMutationType.CREATE);
7069
expect(opName).toBe('onCreateTodo');
71-
72-
// Verify that custom variables are included in the query
7370
expect(query).toContain('$storeId: String');
7471
expect(query).toContain('$tenantId: String');
7572
expect(query).toContain('storeId: $storeId');
@@ -115,11 +112,8 @@ describe('DataStore Subscription Variables', () => {
115112
false,
116113
);
117114

118-
// Verify operation type and name
119115
expect(opType).toBe(TransformerMutationType.CREATE);
120116
expect(opName).toBe('onCreateTodo');
121-
122-
// Verify that no custom variables are included
123117
expect(query).not.toContain('$storeId');
124118
expect(query).not.toContain('$tenantId');
125119
});

packages/datastore/src/sync/utils.ts

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -339,13 +339,10 @@ export function buildSubscriptionGraphQLOperation(
339339
opArgs.push(`${ownerField}: $${ownerField}`);
340340
}
341341

342-
// Add custom subscription variables
343342
if (customVariables) {
344-
// GraphQL variable name must start with letter or underscore, followed by letters, numbers, or underscores
345343
const VALID_VAR_NAME = /^[_a-zA-Z][_a-zA-Z0-9]*$/;
346344

347345
Object.keys(customVariables).forEach(varName => {
348-
// Validate variable name
349346
if (!VALID_VAR_NAME.test(varName)) {
350347
logger.warn(
351348
`Invalid GraphQL variable name '${varName}' in subscriptionVariables. Skipping.`,
@@ -354,15 +351,13 @@ export function buildSubscriptionGraphQLOperation(
354351
return;
355352
}
356353

357-
// Skip null/undefined values
358354
if (
359355
customVariables[varName] === null ||
360356
customVariables[varName] === undefined
361357
) {
362358
return;
363359
}
364360

365-
// Infer type from value (simplified - could be enhanced)
366361
const varType = Array.isArray(customVariables[varName])
367362
? '[String]'
368363
: 'String';
@@ -995,7 +990,6 @@ export function getIdentifierValue(
995990
return idOrPk;
996991
}
997992

998-
// Reserved GraphQL variable names that should not be used
999993
const RESERVED_SUBSCRIPTION_VARIABLE_NAMES = new Set([
1000994
'input',
1001995
'condition',
@@ -1037,7 +1031,6 @@ export function processSubscriptionVariables(
10371031
return undefined;
10381032
}
10391033

1040-
// Check cache first
10411034
let modelCache = cache.get(model);
10421035
if (!modelCache) {
10431036
modelCache = new Map();
@@ -1050,10 +1043,8 @@ export function processSubscriptionVariables(
10501043
return cached || undefined;
10511044
}
10521045

1053-
// Process the variables
10541046
let vars: Record<string, any>;
10551047

1056-
// Handle function-based variables
10571048
if (typeof modelVariables === 'function') {
10581049
try {
10591050
vars = modelVariables(operation);
@@ -1070,7 +1061,6 @@ export function processSubscriptionVariables(
10701061
vars = modelVariables;
10711062
}
10721063

1073-
// Validate and sanitize
10741064
const sanitized = sanitizeSubscriptionVariables(vars, model.name);
10751065
modelCache.set(operation, sanitized);
10761066

@@ -1081,7 +1071,6 @@ function sanitizeSubscriptionVariables(
10811071
vars: any,
10821072
modelName: string,
10831073
): Record<string, any> | null {
1084-
// Validate the input is an object
10851074
if (vars === null || typeof vars !== 'object' || Array.isArray(vars)) {
10861075
logger.warn(
10871076
`subscriptionVariables must be an object for model ${modelName}`,
@@ -1091,12 +1080,10 @@ function sanitizeSubscriptionVariables(
10911080
}
10921081

10931082
try {
1094-
// Deep clone to prevent mutations
10951083
const cloned = JSON.parse(JSON.stringify(vars));
10961084

10971085
return filterReservedSubscriptionVariableKeys(cloned);
10981086
} catch {
1099-
// Can't clone (e.g., circular reference) - use shallow copy
11001087
return filterReservedSubscriptionVariableKeys({ ...vars });
11011088
}
11021089
}
@@ -1106,7 +1093,6 @@ function filterReservedSubscriptionVariableKeys(
11061093
): Record<string, any> | null {
11071094
const result: Record<string, any> = {};
11081095

1109-
// Safe iteration that handles Object.create(null)
11101096
try {
11111097
Object.entries(vars).forEach(([key, value]) => {
11121098
if (!RESERVED_SUBSCRIPTION_VARIABLE_NAMES.has(key)) {
@@ -1118,7 +1104,6 @@ function filterReservedSubscriptionVariableKeys(
11181104
}
11191105
});
11201106
} catch {
1121-
// Fallback for objects that don't support Object.entries
11221107
for (const key in vars) {
11231108
if (
11241109
Object.prototype.hasOwnProperty.call(vars, key) &&

0 commit comments

Comments
 (0)