Skip to content

Commit 8c605ca

Browse files
captbaritonefacebook-github-bot
authored andcommitted
Process abstract type key on resolver fragments
Differential Revision: D80972077
1 parent 6610f72 commit 8c605ca

File tree

2 files changed

+36
-40
lines changed

2 files changed

+36
-40
lines changed

packages/react-relay/__tests__/RelayResolvers-abstractTypeRootFragment-test.js

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -86,15 +86,6 @@ test('Can read a resolver with a rootFragment on an abstract type', async () =>
8686
jest.runAllImmediates();
8787
});
8888

89-
// Incorrect! We provided `__isNode`!
90-
expect(logEvents).toEqual([
91-
{
92-
fieldPath: '<abstract-type-hint>',
93-
kind: 'missing_expected_data.log',
94-
owner: 'NodeResolversGreeting',
95-
uiContext: undefined,
96-
},
97-
]);
98-
// Incorrect! Should be the greeting.
99-
expect(renderer?.toJSON()).toEqual(null);
89+
expect(logEvents).toEqual([]);
90+
expect(renderer?.toJSON()).toEqual('Hello Node with id 4!');
10091
});

packages/relay-runtime/store/RelayResponseNormalizer.js

Lines changed: 34 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import type {PayloadData, PayloadError} from '../network/RelayNetworkTypes';
1616
import type {
1717
NormalizationActorChange,
1818
NormalizationDefer,
19+
NormalizationInlineFragment,
1920
NormalizationLinkedField,
2021
NormalizationLiveResolverField,
2122
NormalizationModuleImport,
@@ -243,34 +244,7 @@ class RelayResponseNormalizer {
243244
break;
244245
}
245246
case 'InlineFragment': {
246-
const {abstractKey} = selection;
247-
if (abstractKey == null) {
248-
const typeName = RelayModernRecord.getType(record);
249-
if (typeName === selection.type) {
250-
this._traverseSelections(selection, record, data);
251-
}
252-
} else {
253-
// $FlowFixMe[method-unbinding] - data could be prototype less
254-
const implementsInterface = Object.prototype.hasOwnProperty.call(
255-
data,
256-
abstractKey,
257-
);
258-
const typeName = RelayModernRecord.getType(record);
259-
const typeID = generateTypeID(typeName);
260-
let typeRecord = this._recordSource.get(typeID);
261-
if (typeRecord == null) {
262-
typeRecord = RelayModernRecord.create(typeID, TYPE_SCHEMA_TYPE);
263-
this._recordSource.set(typeID, typeRecord);
264-
}
265-
RelayModernRecord.setValue(
266-
typeRecord,
267-
abstractKey,
268-
implementsInterface,
269-
);
270-
if (implementsInterface) {
271-
this._traverseSelections(selection, record, data);
272-
}
273-
}
247+
this._normalizeInlineFragment(selection, record, data);
274248
break;
275249
}
276250
case 'TypeDiscriminator': {
@@ -358,13 +332,44 @@ class RelayResponseNormalizer {
358332
}
359333
}
360334

335+
_normalizeInlineFragment(
336+
selection: NormalizationInlineFragment,
337+
record: Record,
338+
data: PayloadData,
339+
) {
340+
const {abstractKey} = selection;
341+
if (abstractKey == null) {
342+
const typeName = RelayModernRecord.getType(record);
343+
if (typeName === selection.type) {
344+
this._traverseSelections(selection, record, data);
345+
}
346+
} else {
347+
// $FlowFixMe[method-unbinding] - data could be prototype less
348+
const implementsInterface = Object.prototype.hasOwnProperty.call(
349+
data,
350+
abstractKey,
351+
);
352+
const typeName = RelayModernRecord.getType(record);
353+
const typeID = generateTypeID(typeName);
354+
let typeRecord = this._recordSource.get(typeID);
355+
if (typeRecord == null) {
356+
typeRecord = RelayModernRecord.create(typeID, TYPE_SCHEMA_TYPE);
357+
this._recordSource.set(typeID, typeRecord);
358+
}
359+
RelayModernRecord.setValue(typeRecord, abstractKey, implementsInterface);
360+
if (implementsInterface) {
361+
this._traverseSelections(selection, record, data);
362+
}
363+
}
364+
}
365+
361366
_normalizeResolver(
362367
resolver: NormalizationResolverField | NormalizationLiveResolverField,
363368
record: Record,
364369
data: PayloadData,
365370
) {
366371
if (resolver.fragment != null) {
367-
this._traverseSelections(resolver.fragment, record, data);
372+
this._normalizeInlineFragment(resolver.fragment, record, data);
368373
}
369374
}
370375

0 commit comments

Comments
 (0)