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(api-headless-cms): skip invalid DZ values instead of throwing #4202

Merged
merged 2 commits into from
Jul 12, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -39,91 +39,52 @@ export class CmsModelDynamicZoneFieldConverterPlugin extends CmsModelFieldConver
}

if (field.multipleValues) {
const arrayValue = Array.isArray(value) ? value : [];
if (Array.isArray(value)) {
return {
[field.storageId]: value.map(item => {
return this.processToStorage({
templates,
converterCollection,
value: item
});
})
};
}

return {
[field.storageId]: arrayValue.map(item => {
return this.processToStorage({
templates,
converterCollection,
value: item
});
})
};
// If a multi-value dynamic zone receives anything other than an array, ignore the value.
return {};
}

// If a single-value dynamic zone receives an array, ignore the value.
if (Array.isArray(value)) {
throw new WebinyError(
`Dynamic zone field "${field.fieldId}" is expecting a non-array value.`,
"DYNAMIC_ZONE_EXPECTING_NON_ARRAY_VALUE",
{
field,
value
}
);
return {};
}

const processedValue = this.processToStorage({
templates,
converterCollection,
value
});

return {
[field.storageId]: processedValue
};
}

private processToStorage(params: ProcessValue) {
const { templates, converterCollection } = params;
let { value } = params;
const { value } = params;
if (value === null || value === undefined) {
return undefined;
}
/**
* There are two ways converter needs to work:
* 1. when there is a _templateId
* 2. when there is a key which identifies which template is the data for
*/

/**
* When we have a template key, everything is simple.
*/

const templateId = value._templateId;
let graphQlName: string | undefined = undefined;
let template: CmsDynamicZoneTemplate | undefined = undefined;
if (templateId) {
template = templates.find(t => {
return templateId === t.id;
});
}
/**
* When we do not have a templateId, then the template identifier is the key of the value object.
* But at that point, values under that key become the value we are working with later on.
*/
//
else {
const keys = Object.keys(value);
if (keys.length === 0) {
return undefined;
} else if (keys.length > 1) {
throw new WebinyError(
"There cannot be more than one dynamic zone template in a single dynamic zone field.",
"DYNAMIC_ZONE_TOO_MANY_TEMPLATES",
{
templates: keys
}
);
}
graphQlName = keys[0] as string;
template = templates.find(t => t.gqlTypeName === graphQlName);
}
const template = templates.find(t => {
return templateId === t.id;
});

if (!template) {
throw new WebinyError("Unknown template - converting to storage.", "UNKNOWN_TEMPLATE", {
templateId,
graphQlName
});
} else if (graphQlName) {
value = value[graphQlName];
return undefined;
}

return template.fields.reduce<Record<string, any>>(
Expand Down Expand Up @@ -166,6 +127,7 @@ export class CmsModelDynamicZoneFieldConverterPlugin extends CmsModelFieldConver
})
};
}

if (Array.isArray(value)) {
throw new WebinyError(
`Dynamic zone field "${field.fieldId}" is expecting a non-array value.`,
Expand All @@ -182,6 +144,7 @@ export class CmsModelDynamicZoneFieldConverterPlugin extends CmsModelFieldConver
converterCollection,
value
});

return {
[field.fieldId]: processedValue
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ const createResolver = (
const resolver = (parent: any) => {
const value = parent[field.fieldId];
if (!value) {
return field.multipleValues ? [] : value;
return value;
}

const typeName = `${graphQLType}_${createTypeName(field.fieldId)}`;
Expand Down
Loading