Skip to content

Commit

Permalink
fix: extract plugin into a separate file
Browse files Browse the repository at this point in the history
  • Loading branch information
adrians5j committed Jul 1, 2020
1 parent 52342c0 commit fb6ebe0
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 42 deletions.
2 changes: 2 additions & 0 deletions packages/api-headless-cms/src/content/plugins/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import graphqlFields from "./graphqlFields";
import graphql from "./graphql";
import { TypeValueEmitter } from "./utils/TypeValueEmitter";
import addRefFieldHooks from "./modelFields/refField/addRefFieldHooks";
import checkRefFieldsBeforeSave from "./modelFields/refField/checkRefFieldsBeforeSave";

type HeadlessPluginsOptions = {
type: string;
Expand Down Expand Up @@ -38,6 +39,7 @@ export default (
}
}
} as ContextPlugin,
checkRefFieldsBeforeSave(),
addRefFieldHooks(),
models(),
{
Expand Down
43 changes: 1 addition & 42 deletions packages/api-headless-cms/src/content/plugins/modelFields/ref.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,45 +200,4 @@ const lockedFieldPlugin: CmsModelLockedFieldPlugin = {
}
};

const checkRefFieldsBeforeSave = {
name: "context-cms-model-ref-field-check-referenced-model",
type: "context",
apply(context) {
const { CmsContentModel } = context.models;
withHooks({
async beforeSave() {
const refFields = this.fields.filter(field => {
if (field.type !== "ref") {
return false;
}

const isLockedField = this.lockedFields.find(
item => item.fieldId === field.fieldId
);
return !isLockedField;
});

// Now that we have non-locked "ref" fields, let's check if the actual model that is referenced
// is ready to be selected. In other words, we don't want to allow models without a title field,
// because basically, all search inputs in the UI will stop working. And not only that, with this
// check, we ensure that the referenced model contains at least one field. Otherwise, the GraphQL
// schema that would be generated after saving this content model, would be invalid, and the
// GraphQL server wouldn't be able to start.
for (let i = 0; i < refFields.length; i++) {
const refField = refFields[i];
const contentModel = await CmsContentModel.findOne({
modelId: refField.settings.modelId
});

if (!contentModel.titleFieldId) {
throw new Error(
`Cannot save content model because the ref field "${refField.fieldId}" references a content model (${refField.settings.modelId}) that has no title field assigned.`
);
}
}
}
})(CmsContentModel);
}
};

export default [plugin, lockedFieldPlugin, checkRefFieldsBeforeSave];
export default [plugin, lockedFieldPlugin];
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { withHooks } from "@webiny/commodo";

const checkRefFieldsBeforeSave = () => ({
type: "context-before-content-models",
name: "context-before-content-models-ref-field-check-referenced-model",
apply(context) {
const { CmsContentModel } = context.models;
withHooks({
async beforeSave() {
const refFields = this.fields.filter(field => {
if (field.type !== "ref") {
return false;
}

const isLockedField = this.lockedFields.find(
item => item.fieldId === field.fieldId
);
return !isLockedField;
});

// Now that we have non-locked "ref" fields, let's check if the actual model that is referenced
// is ready to be selected. In other words, we don't want to allow models without a title field,
// because basically, all search inputs in the UI will stop working. And not only that, with this
// check, we ensure that the referenced model contains at least one field. Otherwise, the GraphQL
// schema that would be generated after saving this content model, would be invalid, and the
// GraphQL server wouldn't be able to start.
for (let i = 0; i < refFields.length; i++) {
const refField = refFields[i];
const contentModel = await CmsContentModel.findOne({
modelId: refField.settings.modelId
});

if (!contentModel.titleFieldId) {
throw new Error(
`Cannot save content model because the ref field "${refField.fieldId}" references a content model (${refField.settings.modelId}) that has no title field assigned.`
);
}
}
}
})(CmsContentModel);
}
});

export default checkRefFieldsBeforeSave;

0 comments on commit fb6ebe0

Please sign in to comment.