Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: merging graphql properties that we already have [TOL-1079] #47

Merged
merged 3 commits into from
Mar 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
129 changes: 129 additions & 0 deletions src/graphql/__tests__/entries.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,71 @@ describe('Update GraphQL Entry', () => {

expect(sendMessageToEditor).not.toHaveBeenCalled();
});

it('graphql properties are not lost with updates', () => {
const testAddingEntryId = '18kDTlnJNnDIJf6PsXE5Mr';
const data = {
__typename: 'Page',
sys: {
id: entry.sys.id,
},
reference: {
sys: {
type: 'Link',
linkType: 'Entry',
id: testAddingEntryId,
},
propertyShouldStay: 'value',
},
};
const testContentTypeId = 'testContentType';
const update = {
sys: {
id: entry.sys.id,
},
fields: {
reference: {
'en-US': {
sys: {
type: 'Link',
linkType: 'Entry',
id: testAddingEntryId,
},
},
},
},
} as unknown as EntryProps<KeyValueMap>;

const entityReferenceMap = new EntryReferenceMap();
entityReferenceMap.set(testAddingEntryId, {
sys: {
contentType: {
sys: {
id: testContentTypeId,
linkType: 'Entry',
},
},
},
} as EntryProps<KeyValueMap>);

// value has not changed, just sends message back to editor
expect(updateFn({ data, update, entityReferenceMap })).toEqual({
__typename: 'Page',
sys: {
id: entry.sys.id,
},
reference: {
// content type has been adjusted to have capital letter at the start
__typename: 'TestContentType',
sys: {
id: testAddingEntryId,
linkType: 'Entry',
type: 'Link',
},
propertyShouldStay: 'value',
},
});
});
});

describe('multi reference fields', () => {
Expand Down Expand Up @@ -398,6 +463,70 @@ describe('Update GraphQL Entry', () => {
},
});
});

it('graphql properties are not lost with updates', () => {
const testAddingEntryId = '3JqLncpMbnZYrCPebujXhK';
const data = {
__typename: 'Page',
sys: {
id: entry.sys.id,
},
referenceManyCollection: {
items: [
{
__typename: 'TestContentTypeForManyRef',
sys: {
type: 'Link',
linkType: 'Entry',
id: testAddingEntryId,
},
propertyShouldStay: 'value',
},
],
},
};

const update = {
sys: {
id: entry.sys.id,
},
fields: {
referenceMany: {
'en-US': [
{
__typename: 'TestContentTypeForManyRef',
sys: {
type: 'Link',
linkType: 'Entry',
id: testAddingEntryId,
},
},
],
},
},
} as unknown as EntryProps<KeyValueMap>;

// value has not changed, just sends message back to editor
expect(updateFn({ data, update })).toEqual({
__typename: 'Page',
sys: {
id: entry.sys.id,
},
referenceManyCollection: {
items: [
{
__typename: 'TestContentTypeForManyRef',
sys: {
id: testAddingEntryId,
linkType: 'Entry',
type: 'Link',
},
propertyShouldStay: 'value',
},
],
},
});
});
});

it('falls back to null for empty fields', () => {
Expand Down
27 changes: 20 additions & 7 deletions src/graphql/entries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,16 +79,21 @@ function getContentTypenameFromEntityReferenceMap(
}

function updateReferenceField(
updatedReference: EntryProps & { __typename?: string },
referenceFromPreviewApp: (EntryProps & { __typename?: string }) | null | undefined,
updatedReference: (EntryProps & { __typename?: string }) | null | undefined,
entityReferenceMap: EntryReferenceMap
) {
// if the reference was deleted return null
if (updatedReference === null) {
if (!updatedReference) {
return null;
}

// it's already in graphql format so we can return
if (updatedReference.__typename) {
if (referenceFromPreviewApp && referenceFromPreviewApp.__typename) {
return referenceFromPreviewApp;
}

if (updatedReference && updatedReference.__typename) {
return updatedReference;
}

Expand All @@ -98,7 +103,7 @@ function updateReferenceField(
);
// if we have the typename of the updated reference, we can return with it
if (entityTypename) {
return { ...updatedReference, __typename: entityTypename };
return { ...referenceFromPreviewApp, ...updatedReference, __typename: entityTypename, };
} else {
// if we don't have the typename we send a message back to the entry editor
// and it will then send the reference back in the entity reference map
Expand All @@ -119,7 +124,11 @@ function updateSingleRefField(
) {
if (name in dataFromPreviewApp) {
const updatedReference = updateFromEntryEditor?.fields?.[name]?.[locale] ?? null;
dataFromPreviewApp[name] = updateReferenceField(updatedReference, entityReferenceMap);
dataFromPreviewApp[name] = updateReferenceField(
dataFromPreviewApp[name] as EntryProps & { __typename?: string },
updatedReference,
entityReferenceMap
);
}
}

Expand All @@ -134,9 +143,13 @@ function updateMultiRefField(
if (fieldName in dataFromPreviewApp) {
const dataFromPreviewAppItems =
updateFromEntryEditor?.fields?.[name]?.[locale]
.map((dataFromPreviewAppItem: any) => {
.map((updatedItem: any) => {
const itemFromPreviewApp = (
dataFromPreviewApp[fieldName] as { items: CollectionItem[] }
).items.find((item) => item.sys.id === updatedItem.sys.id);
return updateReferenceField(
dataFromPreviewAppItem as unknown as EntryProps,
itemFromPreviewApp as unknown as EntryProps & { __typename?: string },
updatedItem as unknown as EntryProps,
entityReferenceMap
);
})
Expand Down