Skip to content

Commit

Permalink
fix(api-headless-cms): skip invalid DZ values instead of throwing (#4202
Browse files Browse the repository at this point in the history
)
  • Loading branch information
Pavel910 authored Jul 12, 2024
1 parent 11be6e7 commit cf2c192
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 63 deletions.
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

0 comments on commit cf2c192

Please sign in to comment.