-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
Copy pathwriteRelayUpdatePayload.js
648 lines (603 loc) · 17.8 KB
/
writeRelayUpdatePayload.js
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
/**
* Copyright (c) 2013-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
* @providesModule writeRelayUpdatePayload
* @flow
* @typechecks
*/
'use strict';
const GraphQLMutatorConstants = require('GraphQLMutatorConstants');
const RelayConnectionInterface = require('RelayConnectionInterface');
import type {
Call,
DataID,
RangeBehaviors,
UpdateOptions,
} from 'RelayInternalTypes';
const RelayMutationTracker = require('RelayMutationTracker');
const RelayMutationType = require('RelayMutationType');
const RelayNodeInterface = require('RelayNodeInterface');
const RelayQuery = require('RelayQuery');
const RelayQueryPath = require('RelayQueryPath');
import type RelayQueryWriter from 'RelayQueryWriter';
const RelayProfiler = require('RelayProfiler');
const RelayRecordState = require('RelayRecordState');
import type RelayRecordStore from 'RelayRecordStore';
const generateClientEdgeID = require('generateClientEdgeID');
const generateClientID = require('generateClientID');
const invariant = require('invariant');
const serializeRelayQueryCall = require('serializeRelayQueryCall');
const warning = require('warning');
// TODO: Replace with enumeration for possible config types.
/* OperationConfig was originally typed such that each property had the type
* mixed. Mixed is safer than any, but that safety comes from Flow forcing you
* to inspect a mixed value at runtime before using it. However these mixeds
* are ending up everywhere and are not being inspected */
type OperationConfig = {[key: string]: $FlowFixMe};
type Payload = mixed | PayloadObject | PayloadArray;
type PayloadArray = Array<Payload>;
type PayloadObject = {[key: string]: Payload};
const {CLIENT_MUTATION_ID, EDGES} = RelayConnectionInterface;
const {ANY_TYPE, ID, NODE} = RelayNodeInterface;
const {APPEND, IGNORE, PREPEND, REFETCH, REMOVE} = GraphQLMutatorConstants;
const EDGES_FIELD = RelayQuery.Field.build({
fieldName: EDGES,
type: ANY_TYPE,
metadata: {
canHaveSubselections: true,
isPlural: true,
},
});
const IGNORED_KEYS = {
error: true,
[CLIENT_MUTATION_ID]: true,
};
const STUB_CURSOR_ID = 'client:cursor';
/**
* @internal
*
* Applies the results of an update operation (mutation/subscription) to the
* store.
*/
function writeRelayUpdatePayload(
writer: RelayQueryWriter,
operation: RelayQuery.Operation,
payload: PayloadObject,
{configs, isOptimisticUpdate}: UpdateOptions
): void {
configs.forEach(config => {
switch (config.type) {
case RelayMutationType.NODE_DELETE:
handleNodeDelete(writer, payload, config);
break;
case RelayMutationType.RANGE_ADD:
handleRangeAdd(
writer,
payload,
operation,
config,
isOptimisticUpdate
);
break;
case RelayMutationType.RANGE_DELETE:
handleRangeDelete(writer, payload, config);
break;
case RelayMutationType.FIELDS_CHANGE:
case RelayMutationType.REQUIRED_CHILDREN:
break;
default:
console.error(
'Expected a valid mutation handler type, got `%s`.',
config.type
);
}
});
handleMerge(writer, payload, operation);
}
/**
* Handles the payload for a node deletion mutation, reading the ID of the node
* to delete from the payload based on the config and then deleting references
* to the node.
*/
function handleNodeDelete(
writer: RelayQueryWriter,
payload: PayloadObject,
config: OperationConfig
): void {
const recordIDs = payload[config.deletedIDFieldName];
if (!recordIDs) {
// for some mutations, deletions don't always occur so if there's no field
// in the payload, carry on
return;
}
if (Array.isArray(recordIDs)) {
recordIDs.forEach(id => {
deleteRecord(writer, id);
});
} else {
deleteRecord(writer, recordIDs);
}
}
/**
* Deletes the record from the store, also removing any references to the node
* from any ranges that contain it (along with the containing edges).
*/
function deleteRecord(
writer: RelayQueryWriter,
recordID: DataID
): void {
const store = writer.getRecordStore();
const recordWriter = writer.getRecordWriter();
// skip if already deleted
const status = store.getRecordState(recordID);
if (status === RelayRecordState.NONEXISTENT) {
return;
}
// Delete the node from any ranges it may be a part of
const connectionIDs = store.getConnectionIDsForRecord(recordID);
if (connectionIDs) {
connectionIDs.forEach(connectionID => {
const edgeID = generateClientEdgeID(connectionID, recordID);
recordWriter.applyRangeUpdate(connectionID, edgeID, REMOVE);
writer.recordUpdate(edgeID);
writer.recordUpdate(connectionID);
// edges are never nodes, so this will not infinitely recurse
deleteRecord(writer, edgeID);
});
}
// delete the node
recordWriter.deleteRecord(recordID);
writer.recordUpdate(recordID);
}
/**
* Handles merging the results of the mutation/subscription into the store,
* updating each top-level field in the data according the fetched
* fields/fragments.
*/
function handleMerge(
writer: RelayQueryWriter,
payload: PayloadObject,
operation: RelayQuery.Operation
): void {
const store = writer.getRecordStore();
// because optimistic payloads may not contain all fields, we loop over
// the data that is present and then have to recurse the query to find
// the matching fields.
//
// TODO #7167718: more efficient mutation/subscription writes
for (const fieldName in payload) {
if (!payload.hasOwnProperty(fieldName)) {
continue;
}
const payloadData = (payload[fieldName]: $FlowIssue); // #9357395
if (typeof payloadData !== 'object' || payloadData == null) {
continue;
}
// if the field is an argument-less root call, determine the corresponding
// root record ID
const rootID = store.getDataID(fieldName);
// check for valid data (has an ID or is an array) and write the field
if (
ID in payloadData ||
rootID ||
Array.isArray(payloadData)
) {
mergeField(
writer,
fieldName,
payloadData,
operation
);
}
}
}
/**
* Merges the results of a single top-level field into the store.
*/
function mergeField(
writer: RelayQueryWriter,
fieldName: string,
payload: PayloadObject | PayloadArray,
operation: RelayQuery.Operation
): void {
// don't write mutation/subscription metadata fields
if (fieldName in IGNORED_KEYS) {
return;
}
if (Array.isArray(payload)) {
payload.forEach(item => {
if (typeof item === 'object' && item != null && !Array.isArray(item)) {
if (getString(item, ID)) {
mergeField(writer, fieldName, item, operation);
}
}
});
return;
}
// reassign to preserve type information in below closure
const payloadData = payload;
const store = writer.getRecordStore();
let recordID = getString(payloadData, ID);
let path;
if (recordID != null) {
path = RelayQueryPath.createForID(recordID, 'writeRelayUpdatePayload');
} else {
recordID = store.getDataID(fieldName);
// Root fields that do not accept arguments
path = RelayQueryPath.create(RelayQuery.Root.build(
'writeRelayUpdatePayload',
fieldName,
null,
null,
{
identifyingArgName: null,
identifyingArgType: null,
isAbstract: true,
isDeferred: false,
isPlural: false,
},
ANY_TYPE
));
}
invariant(
recordID,
'writeRelayUpdatePayload(): Expected a record ID in the response payload ' +
'supplied to update the store.'
);
// write the results for only the current field, for every instance of that
// field in any subfield/fragment in the query.
const handleNode = node => {
node.getChildren().forEach(child => {
if (child instanceof RelayQuery.Fragment) {
handleNode(child);
} else if (
child instanceof RelayQuery.Field &&
child.getSerializationKey() === fieldName
) {
// for flow: types are lost in closures
if (path && recordID) {
// ensure the record exists and then update it
writer.createRecordIfMissing(
child,
recordID,
path,
payloadData
);
writer.writePayload(
child,
recordID,
payloadData,
path
);
}
}
});
};
handleNode(operation);
}
/**
* Handles the payload for a range addition. The configuration specifies:
* - which field in the payload contains data for the new edge
* - the list of fetched ranges to which the edge should be added
* - whether to append/prepend to each of those ranges
*/
function handleRangeAdd(
writer: RelayQueryWriter,
payload: PayloadObject,
operation: RelayQuery.Operation,
config: OperationConfig,
isOptimisticUpdate: boolean
): void {
const clientMutationID = getString(payload, CLIENT_MUTATION_ID);
invariant(
clientMutationID,
'writeRelayUpdatePayload(): Expected operation `%s` to have a `%s`.',
operation.getName(),
CLIENT_MUTATION_ID
);
const store = writer.getRecordStore();
// Extracts the new edge from the payload
const edge = getObject(payload, config.edgeName);
const edgeNode = edge && getObject(edge, NODE);
if (!edge || !edgeNode) {
return;
}
// Extract the id of the node with the connection that we are adding to.
let connectionParentID = config.parentID;
if (!connectionParentID) {
const edgeSource = getObject(edge, 'source');
if (edgeSource) {
connectionParentID = getString(edgeSource, ID);
}
}
invariant(
connectionParentID,
'writeRelayUpdatePayload(): Cannot insert edge without a configured ' +
'`parentID` or a `%s.source.id` field.',
config.edgeName
);
const nodeID = getString(edgeNode, ID) || generateClientID();
const cursor = edge.cursor || STUB_CURSOR_ID;
const edgeData = {
...edge,
cursor: cursor,
node: {
...edgeNode,
id: nodeID,
},
};
// add the node to every connection for this field
const connectionIDs =
store.getConnectionIDsForField(connectionParentID, config.connectionName);
if (connectionIDs) {
connectionIDs.forEach(connectionID => addRangeNode(
writer,
operation,
config,
connectionID,
nodeID,
edgeData
));
}
if (isOptimisticUpdate) {
// optimistic updates need to record the generated client ID for
// a to-be-created node
RelayMutationTracker.putClientIDForMutation(
nodeID,
clientMutationID
);
} else {
// non-optimistic updates check for the existence of a generated client
// ID (from the above `if` clause) and link the client ID to the actual
// server ID.
const clientNodeID =
RelayMutationTracker.getClientIDForMutation(clientMutationID);
if (clientNodeID) {
RelayMutationTracker.updateClientServerIDMap(
clientNodeID,
nodeID
);
RelayMutationTracker.deleteClientIDForMutation(clientMutationID);
}
}
}
/**
* Writes the node data for the given field to the store and prepends/appends
* the node to the given connection.
*/
function addRangeNode(
writer: RelayQueryWriter,
operation: RelayQuery.Operation,
config: OperationConfig,
connectionID: DataID,
nodeID: DataID,
edgeData: any
) {
const store = writer.getRecordStore();
const recordWriter = writer.getRecordWriter();
const filterCalls = store.getRangeFilterCalls(connectionID);
const rangeBehavior = filterCalls ?
getRangeBehavior(config.rangeBehaviors, filterCalls) :
null;
// no range behavior specified for this combination of filter calls
if (!rangeBehavior || rangeBehavior === IGNORE) {
warning(
rangeBehavior,
'Using `null` as a rangeBehavior value is deprecated. Use `ignore` to avoid ' +
'refetching a range.'
);
return;
}
const edgeID = generateClientEdgeID(connectionID, nodeID);
let path = store.getPathToRecord(connectionID);
invariant(
path,
'writeRelayUpdatePayload(): Expected a path for connection record, `%s`.',
connectionID
);
path = RelayQueryPath.getPath(path, EDGES_FIELD, edgeID);
// create the edge record
writer.createRecordIfMissing(EDGES_FIELD, edgeID, path, edgeData);
// write data for all `edges` fields
// TODO #7167718: more efficient mutation/subscription writes
let hasEdgeField = false;
const handleNode = node => {
node.getChildren().forEach(child => {
if (child instanceof RelayQuery.Fragment) {
handleNode(child);
} else if (
child instanceof RelayQuery.Field &&
child.getSchemaName() === config.edgeName
) {
hasEdgeField = true;
if (path) {
writer.writePayload(
child,
edgeID,
edgeData,
path
);
}
}
});
};
handleNode(operation);
invariant(
hasEdgeField,
'writeRelayUpdatePayload(): Expected mutation query to include the ' +
'relevant edge field, `%s`.',
config.edgeName
);
// append/prepend the item to the range.
if (rangeBehavior in GraphQLMutatorConstants.RANGE_OPERATIONS) {
recordWriter.applyRangeUpdate(connectionID, edgeID, (rangeBehavior: any));
if (writer.hasChangeToRecord(edgeID)) {
writer.recordUpdate(connectionID);
}
} else {
console.error(
'writeRelayUpdatePayload(): invalid range operation `%s`, valid ' +
'options are `%s` or `%s`.',
rangeBehavior,
APPEND,
PREPEND
);
}
}
/**
* Handles the payload for a range edge deletion, which removes the edge from
* a specified range but does not delete the node for that edge. The config
* specifies the path within the payload that contains the connection ID.
*/
function handleRangeDelete(
writer: RelayQueryWriter,
payload: PayloadObject,
config: OperationConfig
): void {
const store = writer.getRecordStore();
const recordID =
Array.isArray(config.deletedIDFieldName) ?
getIDFromPath(store, config.deletedIDFieldName, payload) :
getString(payload, config.deletedIDFieldName);
invariant(
recordID != null,
'writeRelayUpdatePayload(): Missing ID for deleted record at field `%s`.',
config.deletedIDFieldName
);
// Extract the id of the node with the connection that we are deleting from.
const connectionName = config.pathToConnection.pop();
const connectionParentID =
getIDFromPath(store, config.pathToConnection, payload);
// Restore pathToConnection to its original state
config.pathToConnection.push(connectionName);
if (!connectionParentID) {
return;
}
const connectionIDs = store.getConnectionIDsForField(
connectionParentID,
connectionName
);
if (connectionIDs) {
connectionIDs.forEach(connectionID => {
deleteRangeEdge(writer, connectionID, recordID);
});
}
}
/**
* Removes an edge from a connection without modifying the node data.
*/
function deleteRangeEdge(
writer: RelayQueryWriter,
connectionID: DataID,
nodeID: DataID
): void {
const recordWriter = writer.getRecordWriter();
const edgeID = generateClientEdgeID(connectionID, nodeID);
recordWriter.applyRangeUpdate(connectionID, edgeID, REMOVE);
deleteRecord(writer, edgeID);
if (writer.hasChangeToRecord(edgeID)) {
writer.recordUpdate(connectionID);
}
}
/**
* Return the action (prepend/append) to use when adding an item to
* the range with the specified calls.
*
* Ex:
* rangeBehaviors: `{'orderby(recent)': 'append'}`
* calls: `[{name: 'orderby', value: 'recent'}]`
*
* Returns `'append'`
*/
function getRangeBehavior(
rangeBehaviors: RangeBehaviors,
calls: Array<Call>
): ?string {
const call = calls.map(serializeRelayQueryCall).sort().join('').slice(1);
return rangeBehaviors[call] || null;
}
/**
* Given a payload of data and a path of fields, extracts the `id` of the node
* specified by the path.
*
* Example:
* path: ['root', 'field']
* data: {root: {field: {id: 'xyz'}}}
*
* Returns:
* 'xyz'
*/
function getIDFromPath(
store: RelayRecordStore,
path: Array<string>,
payload: PayloadObject
): ?string {
// We have a special case for the path for root nodes without ids like
// ['viewer']. We try to match it up with something in the root call mapping
// first.
if (path.length === 1) {
const rootCallID = store.getDataID(path[0]);
if (rootCallID) {
return rootCallID;
}
}
const payloadItem = path.reduce((item, step) => {
return item ? getObject(item, step) : null;
}, payload);
if (payloadItem) {
const id = getString(payloadItem, ID);
invariant(
id != null,
'writeRelayUpdatePayload(): Expected `%s.id` to be a string.',
path.join('.')
);
return id;
}
return null;
}
function getString(
payload: PayloadObject,
field: string
): ?string {
let value = payload[field];
// Coerce numbers to strings for backwards compatibility.
if (typeof value === 'number') {
warning(
false,
'writeRelayUpdatePayload(): Expected `%s` to be a string, got the ' +
'number `%s`.',
field,
value
);
value = '' + value;
}
invariant(
value == null || typeof value === 'string',
'writeRelayUpdatePayload(): Expected `%s` to be a string, got `%s`.',
field,
JSON.stringify(value)
);
return value;
}
function getObject(
payload: PayloadObject,
field: string
): ?PayloadObject {
const value = payload[field];
invariant(
value == null || (typeof value === 'object' && !Array.isArray(value)),
'writeRelayUpdatePayload(): Expected `%s` to be an object, got `%s`.',
field,
JSON.stringify(value)
);
return value;
}
module.exports = RelayProfiler.instrument(
'writeRelayUpdatePayload',
writeRelayUpdatePayload
);