Skip to content

Commit 9d31dfb

Browse files
tyao1facebook-github-bot
authored andcommittedDec 15, 2020
Avoid throwing in the DeclarativeMutationHandler when the server returns null
Reviewed By: jstejada Differential Revision: D25553388 fbshipit-source-id: 63e92372bbccf439703478d017bdf72817d16f07
1 parent 2a6b28a commit 9d31dfb

File tree

2 files changed

+40
-4
lines changed

2 files changed

+40
-4
lines changed
 

‎packages/relay-runtime/handlers/connection/MutationHandlers.js

+11-4
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,13 @@ function edgeUpdater(
105105
'MutationHandlers: Expected connection IDs to be specified.',
106106
);
107107
const serverEdge = record.getLinkedRecord(payload.fieldKey, payload.args);
108+
if (serverEdge == null) {
109+
warning(
110+
false,
111+
'MutationHandlers: Expected the server edge to be non-null',
112+
);
113+
return;
114+
}
108115
for (const connectionID of connections) {
109116
const connection = store.get(connectionID);
110117
if (connection == null) {
@@ -155,10 +162,10 @@ function nodeUpdater(
155162
serverNodes = record.getLinkedRecords(payload.fieldKey, payload.args);
156163
} catch {}
157164
}
158-
invariant(
159-
singleServerNode != null || serverNodes != null,
160-
'MutationHandlers: Expected target node to exist.',
161-
);
165+
if (singleServerNode == null && serverNodes == null) {
166+
warning(false, 'MutationHandlers: Expected target node to exist.');
167+
return;
168+
}
162169
const serverNodeList = serverNodes ?? [singleServerNode];
163170
for (const serverNode of serverNodeList) {
164171
if (serverNode == null) {

‎packages/relay-runtime/store/__tests__/RelayModernEnvironment-ExecuteMutationWithDeclarativeMutation-test.js

+29
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ const RelayNetwork = require('../../network/RelayNetwork');
1919
const RelayObservable = require('../../network/RelayObservable');
2020
const RelayRecordSource = require('../RelayRecordSource');
2121

22+
const warning = require('warning');
23+
2224
const {
2325
createOperationDescriptor,
2426
} = require('../RelayModernOperationDescriptor');
@@ -1743,4 +1745,31 @@ describe('connection node mutations', () => {
17431745
},
17441746
]);
17451747
});
1748+
1749+
it('warns when the server returns an null for the node', () => {
1750+
const snapshot = environment.lookup(operation.fragment);
1751+
const callback = jest.fn();
1752+
environment.subscribe(snapshot, callback);
1753+
1754+
environment
1755+
.executeMutation({
1756+
operation: appendWithLiteralEdgeOperation,
1757+
})
1758+
.subscribe(callbacks);
1759+
1760+
callback.mockClear();
1761+
subject.next({
1762+
data: {
1763+
commentCreate: {
1764+
comment: null,
1765+
},
1766+
},
1767+
});
1768+
subject.complete();
1769+
expect(callback.mock.calls.length).toBe(0);
1770+
expect(warning).toBeCalledWith(
1771+
false,
1772+
'MutationHandlers: Expected target node to exist.',
1773+
);
1774+
});
17461775
});

0 commit comments

Comments
 (0)
Please sign in to comment.