Skip to content

Commit

Permalink
fix(api-headless-cms): resolve conflicts
Browse files Browse the repository at this point in the history
  • Loading branch information
brunozoric committed Oct 10, 2023
2 parents db8fb8d + 949052a commit 7abdfbf
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -287,12 +287,14 @@ async function validateReferencedEntries({
/**
* Load all models and use only those used in the input references.
*/
const models = (await context.cms.listModels()).filter(model => {
const entries = referencesByModel.get(model.modelId);
if (!entries || !entries.length) {
return false;
}
return true;
const models = await context.security.withoutAuthorization(async () => {
return (await context.cms.listModels()).filter(model => {
const entries = referencesByModel.get(model.modelId);
if (!entries?.length) {
return false;
}
return true;
});
});

if (!models.length) {
Expand All @@ -302,8 +304,10 @@ async function validateReferencedEntries({
/**
* Load all the entries by their IDs.
*/
const promises = models.map(model => {
return context.cms.getEntriesByIds(model, referencesByModel.get(model.modelId) || []);
const promises = await context.security.withoutAuthorization(async () => {
return models.map(model => {
return context.cms.getEntriesByIds(model, referencesByModel.get(model.modelId) || []);
});
});

const allEntries = await Promise.all(promises).then(res => res.flat());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,33 +83,24 @@ const remapTemplateValue = (value: any, typeName: string) => {
const createResolver = (
endpointType: ApiEndpoint
): CmsModelFieldToGraphQLCreateResolver<CmsModelDynamicZoneField> => {
return ({ model, models, field, fieldTypePlugins, createFieldResolvers, graphQLType }) => {
const templates = getFieldTemplates(field);

if (!templates.length) {
return false;
}

return ({ model, models, field, fieldTypePlugins, createFieldResolvers }) => {
const resolver = (parent: any) => {
const value = parent[field.fieldId];
if (!value) {
return value;
}

const typeName = `${graphQLType}_${createTypeName(field.fieldId)}`;
// const typeName = `${model.singularApiName}_${createTypeName(field.fieldId)}`;
const typeName = `${model.singularApiName}_${createTypeName(field.fieldId)}`;

if (field.multipleValues && Array.isArray(value)) {
const remappedValues = value.map(v => {
return remapTemplateValue(v, typeName);
});

return remappedValues;
return value.map(v => remapTemplateValue(v, typeName));
}

return remapTemplateValue(value, typeName);
};

const templates = getFieldTemplates(field);

const { templateTypes } = createTypeDefsForTemplates({
models,
field,
Expand All @@ -120,24 +111,17 @@ const createResolver = (
templates
});

const replace = new RegExp(`${model.singularApiName}_`, "g");

const typeResolvers = templateTypes
.map(templateType => {
return templateType.replace(replace, `${graphQLType}_`);
})
.reduce<Record<string, Record<string, GraphQLFieldResolver>>>(
(typeResolvers, templateType, index) => {
return {
...typeResolvers,
...createFieldResolvers({
graphQLType: templateType,
fields: field.settings.templates[index].fields
})
};
},
{}
);
const typeResolvers = templateTypes.reduce<
Record<string, Record<string, GraphQLFieldResolver>>
>((typeResolvers, templateType, index) => {
return {
...typeResolvers,
...createFieldResolvers({
graphQLType: templateType,
fields: field.settings.templates[index].fields
})
};
}, {});

return {
resolver,
Expand Down Expand Up @@ -177,10 +161,6 @@ export const createDynamicZoneField =
read: {
createTypeField({ models, model, field, fieldTypePlugins }) {
const templates = getFieldTemplates(field);
if (!templates.length) {
return null;
}

const unionTypeName = createUnionTypeName(model, field);

const { typeDefs, templateTypes } = createTypeDefsForTemplates({
Expand All @@ -207,11 +187,6 @@ export const createDynamicZoneField =
manage: {
createTypeField({ models, model, field, fieldTypePlugins }) {
const templates = getFieldTemplates(field);

if (!templates.length) {
return null;
}

const unionTypeName = createUnionTypeName(model, field);

const { typeDefs, templateTypes } = createTypeDefsForTemplates({
Expand Down Expand Up @@ -244,10 +219,6 @@ export const createDynamicZoneField =
createInputField({ models, model, field, fieldTypePlugins }) {
const templates = getFieldTemplates(field);

if (!templates.length) {
return null;
}

const { typeDefs, templateTypes } = createTypeDefsForTemplates({
models,
field,
Expand Down
1 change: 1 addition & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -16520,6 +16520,7 @@ __metadata:
"@webiny/cli": 0.0.0
"@webiny/project-utils": 0.0.0
find-up: ^5.0.0
lodash: ^4.17.21
rimraf: ^3.0.2
typescript: 4.7.4
languageName: unknown
Expand Down

0 comments on commit 7abdfbf

Please sign in to comment.