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

feat(api-page-builder): add pub/sub methods #2136

Merged
merged 11 commits into from
Dec 20, 2021
Merged
19 changes: 13 additions & 6 deletions packages/api-dynamic-pages/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,22 @@ import { DynamicPage } from "~/types";
import { interpolateValue } from "~/interpolateValue";
import { IndexPageDataPlugin } from "@webiny/api-page-builder-so-ddb-es/plugins/definitions/IndexPageDataPlugin";
import { SearchPublishedPagesPlugin } from "@webiny/api-page-builder-so-ddb-es/plugins/definitions/SearchPublishedPagesPlugin";
import { ContextPlugin } from "../../handler/src/plugins/ContextPlugin";
import { PbContext } from "@webiny/api-page-builder/types";

export default () => [
new PagePlugin<DynamicPage>({
// - Store "dynamic" flag into DB if page URL is a pattern
beforeUpdate({ updateData }) {
if (updateData.path && updateData.path.includes("{")) {
updateData.dynamic = true;
new ContextPlugin<PbContext>(async context => {
/**
* Store "dynamic" flag into DB if page URL is a pattern
*/
context.pageBuilder.onBeforePageUpdate.subscribe(async params => {
const { page } = params;
if (page.path && page.path.includes("{")) {
(page as any).dynamic = true;
}
},
});
}),
new PagePlugin<DynamicPage>({
// - Attempt to load dynamic page using patterns
async notFound({ args, context }) {
return await loadDynamicPage(args, context);
Expand Down
4 changes: 2 additions & 2 deletions packages/api-dynamic-pages/src/loadDynamicPage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { DynamicPage } from "~/types";

export const loadDynamicPage = async (args, context: PbContext) => {
// Find all pages that have a pattern instead of an exact slug
const [pages] = await context.pageBuilder.pages.listPublished<DynamicPage>({
const [pages] = await context.pageBuilder.listPublishedPages<DynamicPage>({
where: { dynamic: true },
limit: 100
});
Expand All @@ -22,7 +22,7 @@ export const loadDynamicPage = async (args, context: PbContext) => {
// Try matching
const match = args.path.match(new RegExp(pattern));
if (match) {
const fullPage = await context.pageBuilder.pages.getPublishedById<DynamicPage>({
const fullPage = await context.pageBuilder.getPublishedPageById<DynamicPage>({
id: page.id
});
// Load data sources
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,18 +88,18 @@ export default (
try {
if (revisionType === PageExportRevisionType.PUBLISHED) {
// Get "published" page.
page = await pageBuilder.pages.getPublishedById({ id: pageId });
page = await pageBuilder.pages.getPublishedPageById({ id: pageId });
} else {
// Get "latest" page.
page = await pageBuilder.pages.get(pageId);
page = await pageBuilder.pages.getPage(pageId);
}
} catch (e) {
// If we're looking for "published" page and doesn't found it, get latest page.
if (
revisionType === PageExportRevisionType.PUBLISHED &&
e instanceof NotFoundError
) {
page = await pageBuilder.pages.get(pageId);
page = await pageBuilder.pages.getPage(pageId);
} else {
throw e;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export default new ContextPlugin<PbPageImportExportContext>(context => {
});

// Bail out early if category not found
const category = await context.pageBuilder.categories.get(categorySlug);
const category = await context.pageBuilder.getCategory(categorySlug);
if (!category) {
throw new NotFoundError(`Category with slug "${categorySlug}" not found.`);
}
Expand Down Expand Up @@ -69,7 +69,7 @@ export default new ContextPlugin<PbPageImportExportContext>(context => {
let meta = { hasMoreItems: true, cursor: null };
// Paginate pages
while (meta.hasMoreItems) {
[pages, meta] = await context.pageBuilder.pages.listLatest({
[pages, meta] = await context.pageBuilder.pages.listLatestPages({
after: meta.cursor,
where: where,
sort: sort,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,10 @@ export default (
});

// Create a page
let pbPage = await context.pageBuilder.pages.create(category);
let pbPage = await context.pageBuilder.pages.createPage(category);

// Update page with data
pbPage = await context.pageBuilder.pages.update(pbPage.id, {
pbPage = await context.pageBuilder.pages.updatePage(pbPage.id, {
content: page.content,
title: page.title,
path: page.path,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import {
InstallationParams,
InstallationPlugin
} from "@webiny/api-page-builder/plugins/InstallationPlugin";
import { PbContext } from "@webiny/api-page-builder/types";
import { ContextPlugin } from "@webiny/handler/plugins/ContextPlugin";
import configurations from "~/operations/configurations";

export const installation = () => {
return new InstallationPlugin({
beforeInstall: async (params: InstallationParams) => {
const { context } = params;
export const installation = (): ContextPlugin<PbContext> => {
return new ContextPlugin<PbContext>(async context => {
/**
* Add the elasticsearch index for the page builder.
*/
context.pageBuilder.onBeforeInstall.subscribe(async () => {
const { elasticsearch } = context;

const { index } = configurations.es(context);
Expand Down Expand Up @@ -48,6 +48,6 @@ export const installation = () => {
}
}
});
}
});
});
};
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ type DbRecord<T> = T & {
export const upgradeCategories = async (context: PbContext) => {
const tenant: string = context.tenancy.getCurrentTenant().id;
const localeCodes: string[] = await context.i18n.getLocales().map(locale => locale.code);
// @ts-ignore
const categoryStorageOperations = context.pageBuilder.categories
.storageOperations as CategoryStorageOperationsDdbEs;
if (categoryStorageOperations instanceof CategoryStorageOperationsDdbEs === false) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ type DbRecord<T> = T & {
export const upgradeMenus = async (context: PbContext) => {
const tenant: string = context.tenancy.getCurrentTenant().id;
const localeCodes: string[] = await context.i18n.getLocales().map(locale => locale.code);
// @ts-ignore
const menuStorageOperations = context.pageBuilder.menus
.storageOperations as MenuStorageOperationsDdbEs;
if (menuStorageOperations instanceof MenuStorageOperationsDdbEs === false) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ type DbRecord<T> = T & {
export const upgradePageElements = async (context: PbContext) => {
const tenant: string = context.tenancy.getCurrentTenant().id;
const localeCodes: string[] = await context.i18n.getLocales().map(locale => locale.code);
// @ts-ignore
const pageElementsStorageOperations = context.pageBuilder.pageElements
.storageOperations as PageElementStorageOperationsDdbEs;
if (pageElementsStorageOperations instanceof PageElementStorageOperationsDdbEs === false) {
Expand Down
131 changes: 106 additions & 25 deletions packages/api-page-builder/src/graphql/crud.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,30 @@
import menus from "./crud/menus.crud";
import categories from "./crud/categories.crud";
import pages from "./crud/pages.crud";
import pageValidation from "./crud/pages.validation";
import pageElements from "./crud/pageElements.crud";
import settings from "./crud/settings.crud";
import system from "./crud/system.crud";
import { createMenuCrud } from "./crud/menus.crud";
import { createCategoriesCrud } from "./crud/categories.crud";
import { createPageCrud } from "./crud/pages.crud";
import { createPageValidation } from "./crud/pages.validation";
import { createPageElementsCrud } from "./crud/pageElements.crud";
import { createSettingsCrud } from "./crud/settings.crud";
import { createSystemCrud } from "./crud/system.crud";
import { ContextPlugin } from "@webiny/handler/plugins/ContextPlugin";
import { PbContext } from "~/graphql/types";
import WebinyError from "@webiny/error";
import { JsonpackContentCompressionPlugin } from "~/plugins/JsonpackContentCompressionPlugin";
import { createTopic } from "@webiny/pubsub";
import { createStorageOperations } from "~/graphql/crud/storageOperations";
import {
CategoryStorageOperations,
MenuStorageOperations,
PageElementStorageOperations,
PageStorageOperations,
SettingsStorageOperations,
SystemStorageOperations
} from "~/types";
import { SystemStorageOperationsProviderPlugin } from "~/plugins/SystemStorageOperationsProviderPlugin";
import { SettingsStorageOperationsProviderPlugin } from "~/plugins/SettingsStorageOperationsProviderPlugin";
import { MenuStorageOperationsProviderPlugin } from "~/plugins/MenuStorageOperationsProviderPlugin";
import { PageElementStorageOperationsProviderPlugin } from "~/plugins/PageElementStorageOperationsProviderPlugin";
import { PageStorageOperationsProviderPlugin } from "~/plugins/PageStorageOperationsProviderPlugin";
import { CategoryStorageOperationsProviderPlugin } from "~/plugins/CategoryStorageOperationsProviderPlugin";

// This setup (using the `createPageBuilder` factory function) is just a starting point.
// The rest of the Page Builder application should be rewritten and use the same approach as well.
Expand Down Expand Up @@ -38,30 +53,96 @@ const createPageBuilder = () => {
};

const setup = () => {
return new ContextPlugin<PbContext>(context => {
return new ContextPlugin<PbContext>(async context => {
if (context.pageBuilder) {
throw new WebinyError("PbContext setup must be first loaded.", "CONTEXT_SETUP_ERROR");
}

const systemStorageOperations = await createStorageOperations<SystemStorageOperations>(
context,
SystemStorageOperationsProviderPlugin.type
);

const system = await createSystemCrud({
context,
storageOperations: systemStorageOperations
});

const settingsStorageOperations = await createStorageOperations<SettingsStorageOperations>(
context,
SettingsStorageOperationsProviderPlugin.type
);

const settings = createSettingsCrud({
context,
storageOperations: settingsStorageOperations
});

const menusStorageOperations = await createStorageOperations<MenuStorageOperations>(
context,
MenuStorageOperationsProviderPlugin.type
);

const menus = createMenuCrud({
context,
storageOperations: menusStorageOperations
});

const categoriesStorageOperations =
await createStorageOperations<CategoryStorageOperations>(
context,
CategoryStorageOperationsProviderPlugin.type
);

const categories = createCategoriesCrud({
context,
storageOperations: categoriesStorageOperations
});

const pageElementsStorageOperations =
await createStorageOperations<PageElementStorageOperations>(
context,
PageElementStorageOperationsProviderPlugin.type
);

const pageElements = createPageElementsCrud({
context,
storageOperations: pageElementsStorageOperations
});

const pageStorageOperations = await createStorageOperations<PageStorageOperations>(
context,
PageStorageOperationsProviderPlugin.type
);

const pages = createPageCrud({
context,
storageOperations: pageStorageOperations
});

context.pageBuilder = {
...context.pageBuilder,
...createPageBuilder()
...createPageBuilder(),
...system,
...settings,
...menus,
...pages,
...pageElements,
...categories
};
});
};

export default [
setup(),
/**
* We must have default compression in the page builder.
* Maybe figure out some other way of registering the plugins.
*/
new JsonpackContentCompressionPlugin(),
menus,
categories,
pages,
pageValidation,
pageElements,
settings,
system
];
export const createCrud = () => {
return [
new JsonpackContentCompressionPlugin(),
setup(),
/**
* We must have default compression in the page builder.
* Maybe figure out some other way of registering the plugins.
*/
/**
* Add validation
*/
createPageValidation()
];
};
Loading