Skip to content

Commit 39cc230

Browse files
fix: merging graphql properties that we already have [TOL-1079] (#47)
* fix: merging graphql properties that we already have [TOL-1079] * chore: update src/graphql/entries.ts [] Co-authored-by: Chris Helgert <chrishelgert@users.noreply.github.com> * fix: adding tests for graphql properties to stay [TOL-1079] --------- Co-authored-by: Chris Helgert <chrishelgert@users.noreply.github.com>
1 parent 7e5f6c7 commit 39cc230

File tree

2 files changed

+149
-7
lines changed

2 files changed

+149
-7
lines changed

src/graphql/__tests__/entries.test.ts

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,71 @@ describe('Update GraphQL Entry', () => {
233233

234234
expect(sendMessageToEditor).not.toHaveBeenCalled();
235235
});
236+
237+
it('graphql properties are not lost with updates', () => {
238+
const testAddingEntryId = '18kDTlnJNnDIJf6PsXE5Mr';
239+
const data = {
240+
__typename: 'Page',
241+
sys: {
242+
id: entry.sys.id,
243+
},
244+
reference: {
245+
sys: {
246+
type: 'Link',
247+
linkType: 'Entry',
248+
id: testAddingEntryId,
249+
},
250+
propertyShouldStay: 'value',
251+
},
252+
};
253+
const testContentTypeId = 'testContentType';
254+
const update = {
255+
sys: {
256+
id: entry.sys.id,
257+
},
258+
fields: {
259+
reference: {
260+
'en-US': {
261+
sys: {
262+
type: 'Link',
263+
linkType: 'Entry',
264+
id: testAddingEntryId,
265+
},
266+
},
267+
},
268+
},
269+
} as unknown as EntryProps<KeyValueMap>;
270+
271+
const entityReferenceMap = new EntryReferenceMap();
272+
entityReferenceMap.set(testAddingEntryId, {
273+
sys: {
274+
contentType: {
275+
sys: {
276+
id: testContentTypeId,
277+
linkType: 'Entry',
278+
},
279+
},
280+
},
281+
} as EntryProps<KeyValueMap>);
282+
283+
// value has not changed, just sends message back to editor
284+
expect(updateFn({ data, update, entityReferenceMap })).toEqual({
285+
__typename: 'Page',
286+
sys: {
287+
id: entry.sys.id,
288+
},
289+
reference: {
290+
// content type has been adjusted to have capital letter at the start
291+
__typename: 'TestContentType',
292+
sys: {
293+
id: testAddingEntryId,
294+
linkType: 'Entry',
295+
type: 'Link',
296+
},
297+
propertyShouldStay: 'value',
298+
},
299+
});
300+
});
236301
});
237302

238303
describe('multi reference fields', () => {
@@ -398,6 +463,70 @@ describe('Update GraphQL Entry', () => {
398463
},
399464
});
400465
});
466+
467+
it('graphql properties are not lost with updates', () => {
468+
const testAddingEntryId = '3JqLncpMbnZYrCPebujXhK';
469+
const data = {
470+
__typename: 'Page',
471+
sys: {
472+
id: entry.sys.id,
473+
},
474+
referenceManyCollection: {
475+
items: [
476+
{
477+
__typename: 'TestContentTypeForManyRef',
478+
sys: {
479+
type: 'Link',
480+
linkType: 'Entry',
481+
id: testAddingEntryId,
482+
},
483+
propertyShouldStay: 'value',
484+
},
485+
],
486+
},
487+
};
488+
489+
const update = {
490+
sys: {
491+
id: entry.sys.id,
492+
},
493+
fields: {
494+
referenceMany: {
495+
'en-US': [
496+
{
497+
__typename: 'TestContentTypeForManyRef',
498+
sys: {
499+
type: 'Link',
500+
linkType: 'Entry',
501+
id: testAddingEntryId,
502+
},
503+
},
504+
],
505+
},
506+
},
507+
} as unknown as EntryProps<KeyValueMap>;
508+
509+
// value has not changed, just sends message back to editor
510+
expect(updateFn({ data, update })).toEqual({
511+
__typename: 'Page',
512+
sys: {
513+
id: entry.sys.id,
514+
},
515+
referenceManyCollection: {
516+
items: [
517+
{
518+
__typename: 'TestContentTypeForManyRef',
519+
sys: {
520+
id: testAddingEntryId,
521+
linkType: 'Entry',
522+
type: 'Link',
523+
},
524+
propertyShouldStay: 'value',
525+
},
526+
],
527+
},
528+
});
529+
});
401530
});
402531

403532
it('falls back to null for empty fields', () => {

src/graphql/entries.ts

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -79,16 +79,21 @@ function getContentTypenameFromEntityReferenceMap(
7979
}
8080

8181
function updateReferenceField(
82-
updatedReference: EntryProps & { __typename?: string },
82+
referenceFromPreviewApp: (EntryProps & { __typename?: string }) | null | undefined,
83+
updatedReference: (EntryProps & { __typename?: string }) | null | undefined,
8384
entityReferenceMap: EntryReferenceMap
8485
) {
8586
// if the reference was deleted return null
86-
if (updatedReference === null) {
87+
if (!updatedReference) {
8788
return null;
8889
}
8990

9091
// it's already in graphql format so we can return
91-
if (updatedReference.__typename) {
92+
if (referenceFromPreviewApp && referenceFromPreviewApp.__typename) {
93+
return referenceFromPreviewApp;
94+
}
95+
96+
if (updatedReference && updatedReference.__typename) {
9297
return updatedReference;
9398
}
9499

@@ -98,7 +103,7 @@ function updateReferenceField(
98103
);
99104
// if we have the typename of the updated reference, we can return with it
100105
if (entityTypename) {
101-
return { ...updatedReference, __typename: entityTypename };
106+
return { ...referenceFromPreviewApp, ...updatedReference, __typename: entityTypename, };
102107
} else {
103108
// if we don't have the typename we send a message back to the entry editor
104109
// and it will then send the reference back in the entity reference map
@@ -119,7 +124,11 @@ function updateSingleRefField(
119124
) {
120125
if (name in dataFromPreviewApp) {
121126
const updatedReference = updateFromEntryEditor?.fields?.[name]?.[locale] ?? null;
122-
dataFromPreviewApp[name] = updateReferenceField(updatedReference, entityReferenceMap);
127+
dataFromPreviewApp[name] = updateReferenceField(
128+
dataFromPreviewApp[name] as EntryProps & { __typename?: string },
129+
updatedReference,
130+
entityReferenceMap
131+
);
123132
}
124133
}
125134

@@ -134,9 +143,13 @@ function updateMultiRefField(
134143
if (fieldName in dataFromPreviewApp) {
135144
const dataFromPreviewAppItems =
136145
updateFromEntryEditor?.fields?.[name]?.[locale]
137-
.map((dataFromPreviewAppItem: any) => {
146+
.map((updatedItem: any) => {
147+
const itemFromPreviewApp = (
148+
dataFromPreviewApp[fieldName] as { items: CollectionItem[] }
149+
).items.find((item) => item.sys.id === updatedItem.sys.id);
138150
return updateReferenceField(
139-
dataFromPreviewAppItem as unknown as EntryProps,
151+
itemFromPreviewApp as unknown as EntryProps & { __typename?: string },
152+
updatedItem as unknown as EntryProps,
140153
entityReferenceMap
141154
);
142155
})

0 commit comments

Comments
 (0)