-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
Copy pathmutation.ts
703 lines (623 loc) · 19.8 KB
/
mutation.ts
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
import { GraphQLResult } from '@aws-amplify/api';
import { InternalAPI } from '@aws-amplify/api/internals';
import {
BackgroundProcessManager,
Category,
CustomUserAgentDetails,
DataStoreAction,
GraphQLAuthMode,
NonRetryableError,
jitteredBackoff,
retry,
} from '@aws-amplify/core/internals/utils';
import { Observable, Observer } from 'rxjs';
import { ConsoleLogger } from '@aws-amplify/core';
import { MutationEvent } from '../';
import { ModelInstanceCreator } from '../../datastore/datastore';
import { ExclusiveStorage as Storage } from '../../storage/storage';
import {
AmplifyContext,
AuthModeStrategy,
ConflictHandler,
DISCARD,
ErrorHandler,
GraphQLCondition,
InternalSchema,
ModelInstanceMetadata,
OpType,
PersistentModel,
PersistentModelConstructor,
ProcessName,
SchemaModel,
TypeConstructorMap,
isModelFieldType,
isTargetNameAssociation,
} from '../../types';
import { ID, USER, extractTargetNamesFromSrc } from '../../util';
import { MutationEventOutbox } from '../outbox';
import {
TransformerMutationType,
buildGraphQLOperation,
createMutationInstanceFromModelOperation,
getModelAuthModes,
getTokenForCustomAuth,
} from '../utils';
import { getMutationErrorType } from './errorMaps';
const MAX_ATTEMPTS = 10;
const logger = new ConsoleLogger('DataStore');
interface MutationProcessorEvent {
operation: TransformerMutationType;
modelDefinition: SchemaModel;
model: PersistentModel;
hasMore: boolean;
}
class MutationProcessor {
/**
* The observer that receives messages when mutations are successfully completed
* against cloud storage.
*
* A value of `undefined` signals that the sync has either been stopped or has not
* yet started. In this case, `isReady()` will be `false` and `resume()` will exit
* early.
*/
private observer?: Observer<MutationProcessorEvent>;
private readonly typeQuery = new WeakMap<
SchemaModel,
[TransformerMutationType, string, string][]
>();
private processing = false;
private runningProcesses = new BackgroundProcessManager();
constructor(
private readonly schema: InternalSchema,
private readonly storage: Storage,
private readonly userClasses: TypeConstructorMap,
private readonly outbox: MutationEventOutbox,
private readonly modelInstanceCreator: ModelInstanceCreator,
private readonly _MutationEvent: PersistentModelConstructor<MutationEvent>,
private readonly amplifyConfig: Record<string, any> = {},
private readonly authModeStrategy: AuthModeStrategy,
private readonly errorHandler: ErrorHandler,
private readonly conflictHandler: ConflictHandler,
private readonly amplifyContext: AmplifyContext,
) {
this.amplifyContext.InternalAPI =
this.amplifyContext.InternalAPI || InternalAPI;
this.generateQueries();
}
private generateQueries() {
Object.values(this.schema.namespaces).forEach(namespace => {
Object.values(namespace.models)
.filter(({ syncable }) => syncable)
.forEach(model => {
const [createMutation] = buildGraphQLOperation(
namespace,
model,
'CREATE',
);
const [updateMutation] = buildGraphQLOperation(
namespace,
model,
'UPDATE',
);
const [deleteMutation] = buildGraphQLOperation(
namespace,
model,
'DELETE',
);
this.typeQuery.set(model, [
createMutation,
updateMutation,
deleteMutation,
]);
});
});
}
private isReady() {
return this.observer !== undefined;
}
public start(): Observable<MutationProcessorEvent> {
this.runningProcesses = new BackgroundProcessManager();
const observable = new Observable<MutationProcessorEvent>(observer => {
this.observer = observer;
try {
this.resume();
} catch (error) {
logger.error('mutations processor start error', error);
throw error;
}
return this.runningProcesses.addCleaner(async () => {
// The observer has unsubscribed and/or `stop()` has been called.
this.removeObserver();
this.pause();
});
});
return observable;
}
public async stop() {
this.removeObserver();
await this.runningProcesses.close();
await this.runningProcesses.open();
}
public removeObserver() {
this.observer?.complete?.();
this.observer = undefined;
}
public async resume(): Promise<void> {
if (this.runningProcesses.isOpen) {
await this.runningProcesses.add(async onTerminate => {
if (
this.processing ||
!this.isReady() ||
!this.runningProcesses.isOpen
) {
return;
}
this.processing = true;
let head: MutationEvent;
const namespaceName = USER;
// start to drain outbox
while (
this.processing &&
this.runningProcesses.isOpen &&
(head = await this.outbox.peek(this.storage)) !== undefined
) {
const { model, operation, data, condition } = head;
const modelConstructor = this.userClasses[
model
] as PersistentModelConstructor<MutationEvent>;
let result: GraphQLResult<Record<string, PersistentModel>> =
undefined!;
let opName: string = undefined!;
let modelDefinition: SchemaModel = undefined!;
try {
const modelAuthModes = await getModelAuthModes({
authModeStrategy: this.authModeStrategy,
defaultAuthMode:
this.amplifyConfig.aws_appsync_authenticationType,
modelName: model,
schema: this.schema,
});
const operationAuthModes = modelAuthModes[operation.toUpperCase()];
let authModeAttempts = 0;
const authModeRetry = async () => {
try {
logger.debug(
`Attempting mutation with authMode: ${operationAuthModes[authModeAttempts]}`,
);
const response = await this.jitteredRetry(
namespaceName,
model,
operation,
data,
condition,
modelConstructor,
this._MutationEvent,
head,
operationAuthModes[authModeAttempts],
onTerminate,
);
logger.debug(
`Mutation sent successfully with authMode: ${operationAuthModes[authModeAttempts]}`,
);
return response;
} catch (error) {
authModeAttempts++;
if (authModeAttempts >= operationAuthModes.length) {
logger.debug(
`Mutation failed with authMode: ${
operationAuthModes[authModeAttempts - 1]
}`,
);
try {
// eslint-disable-next-line @typescript-eslint/no-confusing-void-expression
await this.errorHandler({
recoverySuggestion:
'Ensure app code is up to date, auth directives exist and are correct on each model, and that server-side data has not been invalidated by a schema change. If the problem persists, search for or create an issue: https://github.com/aws-amplify/amplify-js/issues',
localModel: null!,
message: error.message,
model: modelConstructor.name,
operation: opName,
errorType: getMutationErrorType(error),
process: ProcessName.sync,
remoteModel: null!,
cause: error,
});
} catch (e) {
logger.error('Mutation error handler failed with:', e);
}
throw error;
}
logger.debug(
`Mutation failed with authMode: ${
operationAuthModes[authModeAttempts - 1]
}. Retrying with authMode: ${
operationAuthModes[authModeAttempts]
}`,
);
return authModeRetry();
}
};
[result, opName, modelDefinition] = await authModeRetry();
} catch (error) {
if (
error.message === 'Offline' ||
error.message === 'RetryMutation'
) {
continue;
}
}
if (result === undefined) {
logger.debug('done retrying');
await this.storage.runExclusive(async storage => {
await this.outbox.dequeue(storage);
});
continue;
}
const record = result.data![opName!];
let hasMore = false;
await this.storage.runExclusive(async storage => {
// using runExclusive to prevent possible race condition
// when another record gets enqueued between dequeue and peek
await this.outbox.dequeue(storage, record, operation);
hasMore = (await this.outbox.peek(storage)) !== undefined;
});
this.observer?.next?.({
operation,
modelDefinition,
model: record,
hasMore,
});
}
// pauses itself
this.pause();
}, 'mutation resume loop');
}
}
private async jitteredRetry(
namespaceName: string,
model: string,
operation: TransformerMutationType,
data: string,
condition: string,
modelConstructor: PersistentModelConstructor<PersistentModel>,
MutationEventCtor: PersistentModelConstructor<MutationEvent>,
mutationEvent: MutationEvent,
authMode: GraphQLAuthMode,
onTerminate: Promise<void>,
): Promise<
[GraphQLResult<Record<string, PersistentModel>>, string, SchemaModel]
> {
return retry(
async (
retriedModel: string,
retriedOperation: TransformerMutationType,
retriedData: string,
retriedCondition: string,
retriedModelConstructor: PersistentModelConstructor<PersistentModel>,
retiredMutationEventCtor: PersistentModelConstructor<MutationEvent>,
retiredMutationEvent: MutationEvent,
) => {
const [query, variables, graphQLCondition, opName, modelDefinition] =
this.createQueryVariables(
namespaceName,
retriedModel,
retriedOperation,
retriedData,
retriedCondition,
);
const authToken = await getTokenForCustomAuth(
authMode,
this.amplifyConfig,
);
const tryWith = {
query,
variables,
authMode,
authToken,
};
let attempt = 0;
const opType = this.opTypeFromTransformerOperation(retriedOperation);
const customUserAgentDetails: CustomUserAgentDetails = {
category: Category.DataStore,
action: DataStoreAction.GraphQl,
};
do {
try {
const result = (await this.amplifyContext.InternalAPI.graphql(
tryWith,
undefined,
customUserAgentDetails,
)) as GraphQLResult<Record<string, PersistentModel>>;
// Use `as any` because TypeScript doesn't seem to like passing tuples
// through generic params.
return [result, opName, modelDefinition] as any;
} catch (err) {
if (err.errors && err.errors.length > 0) {
const [error] = err.errors;
const { originalError: { code = null } = {} } = error;
if (error.errorType === 'Unauthorized') {
throw new NonRetryableError('Unauthorized');
}
if (
error.message === 'Network Error' ||
code === 'ERR_NETWORK' // refers to axios timeout error caused by device's bad network condition
) {
if (!this.processing) {
throw new NonRetryableError('Offline');
}
// TODO: Check errors on different env (react-native or other browsers)
throw new Error('Network Error');
}
if (error.errorType === 'ConflictUnhandled') {
// TODO: add on ConflictConditionalCheck error query last from server
attempt++;
let retryWith: PersistentModel | typeof DISCARD;
if (attempt > MAX_ATTEMPTS) {
retryWith = DISCARD;
} else {
try {
retryWith = await this.conflictHandler!({
modelConstructor: retriedModelConstructor,
localModel: this.modelInstanceCreator(
retriedModelConstructor,
variables.input,
),
remoteModel: this.modelInstanceCreator(
retriedModelConstructor,
error.data,
),
operation: opType,
attempts: attempt,
});
} catch (caughtErr) {
logger.warn('conflict trycatch', caughtErr);
continue;
}
}
if (retryWith === DISCARD) {
// Query latest from server and notify merger
const [[, builtOpName, builtQuery]] = buildGraphQLOperation(
this.schema.namespaces[namespaceName],
modelDefinition,
'GET',
);
const newAuthToken = await getTokenForCustomAuth(
authMode,
this.amplifyConfig,
);
const serverData =
(await this.amplifyContext.InternalAPI.graphql(
{
query: builtQuery,
variables: { id: variables.input.id },
authMode,
authToken: newAuthToken,
},
undefined,
customUserAgentDetails,
)) as GraphQLResult<Record<string, PersistentModel>>;
// onTerminate cancel graphql()
return [serverData, builtOpName, modelDefinition];
}
const namespace = this.schema.namespaces[namespaceName];
// convert retry with to tryWith
const updatedMutation =
createMutationInstanceFromModelOperation(
namespace.relationships!,
modelDefinition,
opType,
retriedModelConstructor,
retryWith,
graphQLCondition,
retiredMutationEventCtor,
this.modelInstanceCreator,
retiredMutationEvent.id,
);
await this.storage.save(updatedMutation);
throw new NonRetryableError('RetryMutation');
} else {
try {
this.errorHandler({
recoverySuggestion:
'Ensure app code is up to date, auth directives exist and are correct on each model, and that server-side data has not been invalidated by a schema change. If the problem persists, search for or create an issue: https://github.com/aws-amplify/amplify-js/issues',
localModel: variables.input,
message: error.message,
operation: retriedOperation,
errorType: getMutationErrorType(error),
errorInfo: error.errorInfo,
process: ProcessName.mutate,
cause: error,
remoteModel: error.data
? this.modelInstanceCreator(
retriedModelConstructor,
error.data,
)
: null!,
});
} catch (caughtErr) {
logger.warn('Mutation error handler failed with:', caughtErr);
} finally {
// Return empty tuple, dequeues the mutation
// eslint-disable-next-line no-unsafe-finally
return error.data
? [
{ data: { [opName]: error.data } },
opName,
modelDefinition,
]
: [];
}
}
} else {
// Catch-all for client-side errors that don't come back in the `GraphQLError` format.
// These errors should not be retried.
throw new NonRetryableError(err);
}
}
// eslint-disable-next-line no-unmodified-loop-condition
} while (tryWith);
},
[
model,
operation,
data,
condition,
modelConstructor,
MutationEventCtor,
mutationEvent,
],
safeJitteredBackoff,
onTerminate,
);
}
private createQueryVariables(
namespaceName: string,
model: string,
operation: TransformerMutationType,
data: string,
condition: string,
): [string, Record<string, any>, GraphQLCondition, string, SchemaModel] {
const modelDefinition = this.schema.namespaces[namespaceName].models[model];
const { primaryKey } = this.schema.namespaces[namespaceName].keys![model];
const auth = modelDefinition.attributes?.find(a => a.type === 'auth');
const ownerFields: string[] = auth?.properties?.rules
.map(rule => rule.ownerField)
.filter(f => f) || ['owner'];
const queriesTuples = this.typeQuery.get(modelDefinition);
const [, opName, query] = queriesTuples!.find(
([transformerMutationType]) => transformerMutationType === operation,
)!;
const { _version, ...parsedData } = JSON.parse(
data,
) as ModelInstanceMetadata;
// include all the fields that comprise a custom PK if one is specified
const deleteInput = {};
if (primaryKey && primaryKey.length) {
for (const pkField of primaryKey) {
deleteInput[pkField] = parsedData[pkField];
}
} else {
deleteInput[ID] = (parsedData as any).id;
}
let mutationInput;
if (operation === TransformerMutationType.DELETE) {
// For DELETE mutations, only the key(s) are included in the input
mutationInput = deleteInput as ModelInstanceMetadata;
} else {
// Otherwise, we construct the mutation input with the following logic
mutationInput = {};
const modelFields = Object.values(modelDefinition.fields);
for (const { name, type, association, isReadOnly } of modelFields) {
// omit readonly fields. cloud storage doesn't need them and won't take them!
if (isReadOnly) {
continue;
}
// omit owner fields if it's `null`. cloud storage doesn't allow it.
if (ownerFields.includes(name) && parsedData[name] === null) {
continue;
}
// model fields should be stripped out from the input
if (isModelFieldType(type)) {
// except for belongs to relations - we need to replace them with the correct foreign key(s)
if (
isTargetNameAssociation(association) &&
association.connectionType === 'BELONGS_TO'
) {
const targetNames: string[] | undefined =
extractTargetNamesFromSrc(association);
if (targetNames) {
// instead of including the connected model itself, we add its key(s) to the mutation input
for (const targetName of targetNames) {
mutationInput[targetName] = parsedData[targetName];
}
}
}
continue;
}
// scalar fields / non-model types
if (operation === TransformerMutationType.UPDATE) {
if (!Object.prototype.hasOwnProperty.call(parsedData, name)) {
// for update mutations - strip out a field if it's unchanged
continue;
}
}
// all other fields are added to the input object
mutationInput[name] = parsedData[name];
}
}
// Build mutation variables input object
const input: ModelInstanceMetadata = {
...mutationInput,
_version,
};
const graphQLCondition = JSON.parse(condition) as GraphQLCondition;
const variables = {
input,
...(operation === TransformerMutationType.CREATE
? {}
: {
condition:
Object.keys(graphQLCondition).length > 0
? graphQLCondition
: null,
}),
};
return [query, variables, graphQLCondition, opName, modelDefinition];
}
private opTypeFromTransformerOperation(
operation: TransformerMutationType,
): OpType {
switch (operation) {
case TransformerMutationType.CREATE:
return OpType.INSERT;
case TransformerMutationType.DELETE:
return OpType.DELETE;
case TransformerMutationType.UPDATE:
return OpType.UPDATE;
case TransformerMutationType.GET: // Intentionally blank
break;
default:
throw new Error(`Invalid operation ${operation}`);
}
// because it makes TS happy ...
return undefined!;
}
public pause() {
this.processing = false;
}
}
const MAX_RETRY_DELAY_MS = 5 * 60 * 1000;
const originalJitteredBackoff = jitteredBackoff(MAX_RETRY_DELAY_MS);
/**
* @private
* Internal use of Amplify only.
*
* Wraps the jittered backoff calculation to retry Network Errors indefinitely.
* Backs off according to original jittered retry logic until the original retry
* logic hits its max. After this occurs, if the error is a Network Error, we
* ignore the attempt count and return MAX_RETRY_DELAY_MS to retry forever (until
* the request succeeds).
*
* @param attempt ignored
* @param _args ignored
* @param error tested to see if `.message` is 'Network Error'
* @returns number | false :
*/
export const safeJitteredBackoff: typeof originalJitteredBackoff = (
attempt,
_args,
error,
) => {
const attemptResult = originalJitteredBackoff(attempt);
// If this is the last attempt and it is a network error, we retry indefinitively every 5 minutes
if (
attemptResult === false &&
((error || {}) as any).message === 'Network Error'
) {
return MAX_RETRY_DELAY_MS;
}
return attemptResult;
};
export { MutationProcessor };