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

refactor(api-apw): remove unnecessary cms code #2530

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 3 additions & 8 deletions apps/api/graphql/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,7 @@ import logsPlugins from "@webiny/handler-logs";
import fileManagerS3 from "@webiny/api-file-manager-s3";
import { createFormBuilder } from "@webiny/api-form-builder";
import { createFormBuilderStorageOperations } from "@webiny/api-form-builder-so-ddb";
import {
createHeadlessCmsGraphQL,
createHeadlessCmsContext,
createGraphQLFields
} from "@webiny/api-headless-cms";
import { createHeadlessCmsGraphQL, createHeadlessCmsContext } from "@webiny/api-headless-cms";
import { createStorageOperations as createHeadlessCmsStorageOperations } from "@webiny/api-headless-cms-ddb";
import securityPlugins from "./security";
import tenantManager from "@webiny/api-tenant-manager";
Expand Down Expand Up @@ -79,13 +75,12 @@ export const handler = createHandler({
documentClient
})
}),
createHeadlessCmsGraphQL(),
createHeadlessCmsContext({
storageOperations: createHeadlessCmsStorageOperations({
documentClient,
modelFieldToGraphQLPlugins: createGraphQLFields()
documentClient
})
}),
createHeadlessCmsGraphQL(),
createApwGraphQL(),
createApwPageBuilderContext({
storageOperations: createApwSaStorageOperations({ documentClient })
Expand Down
9 changes: 2 additions & 7 deletions apps/api/headlessCMS/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,7 @@ import dbPlugins from "@webiny/handler-db";
import { DynamoDbDriver } from "@webiny/db-dynamodb";
import dynamoDbPlugins from "@webiny/db-dynamodb/plugins";
import securityPlugins from "./security";
import {
createHeadlessCmsContext,
createHeadlessCmsGraphQL,
createGraphQLFields
} from "@webiny/api-headless-cms";
import { createHeadlessCmsContext, createHeadlessCmsGraphQL } from "@webiny/api-headless-cms";
import { createStorageOperations as createHeadlessCmsStorageOperations } from "@webiny/api-headless-cms-ddb";
import logsPlugins from "@webiny/handler-logs";
/**
Expand Down Expand Up @@ -44,8 +40,7 @@ export const handler = createHandler({
i18nDynamoDbStorageOperations(),
createHeadlessCmsContext({
storageOperations: createHeadlessCmsStorageOperations({
documentClient,
modelFieldToGraphQLPlugins: createGraphQLFields()
documentClient
})
}),
createHeadlessCmsGraphQL({ debug }),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,7 @@ import {
import { Plugin, PluginCollection } from "@webiny/plugins/types";
import { createApwHeadlessCmsContext, createApwGraphQL } from "~/index";
import { createStorageOperations as createHeadlessCmsStorageOperations } from "@webiny/api-headless-cms-ddb";
import {
createGraphQLFields,
createHeadlessCmsContext,
createHeadlessCmsGraphQL
} from "@webiny/api-headless-cms";
import { createHeadlessCmsContext, createHeadlessCmsGraphQL } from "@webiny/api-headless-cms";
/**
* Unfortunately at we need to import the api-i18n-ddb package manually
*/
Expand Down Expand Up @@ -111,8 +107,7 @@ export const createHeadlessCmsGQLHandler = (params: CreateHeadlessCmsGQLHandlerP
*/
const headlessCmsApp = createHeadlessCmsContext({
storageOperations: createHeadlessCmsStorageOperations({
documentClient,
modelFieldToGraphQLPlugins: createGraphQLFields()
documentClient
})
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,7 @@ import {
import { Plugin, PluginCollection } from "@webiny/plugins/types";
import { createApwPageBuilderContext, createApwGraphQL } from "~/index";
import { createStorageOperations as createHeadlessCmsStorageOperations } from "@webiny/api-headless-cms-ddb";
import {
createGraphQLFields,
createHeadlessCmsContext,
createHeadlessCmsGraphQL
} from "@webiny/api-headless-cms";
import { createHeadlessCmsContext, createHeadlessCmsGraphQL } from "@webiny/api-headless-cms";
/**
* Unfortunately at we need to import the api-i18n-ddb package manually
*/
Expand Down Expand Up @@ -111,8 +107,7 @@ export const createPageBuilderGQLHandler = (params: GQLHandlerCallableParams) =>
*/
const headlessCmsApp = createHeadlessCmsContext({
storageOperations: createHeadlessCmsStorageOperations({
documentClient,
modelFieldToGraphQLPlugins: createGraphQLFields()
documentClient
})
});

Expand Down
13 changes: 2 additions & 11 deletions packages/api-apw/src/plugins/context.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import apwHooks from "./hooks";
import WebinyError from "@webiny/error";
import { createHeadlessCmsContext } from "@webiny/api-headless-cms";
import { ContextPlugin } from "@webiny/handler/plugins/ContextPlugin";
import { ApwContext } from "~/types";
import { createApw } from "~/crud";
import { apwPageBuilderHooks } from "./pageBuilder";
import { createStorageOperations } from "~/storageOperations";
import { createManageCMSPlugin } from "~/plugins/createManageCMSPlugin";
import { SecurityPermission } from "@webiny/api-security/types";
import { Tenant } from "@webiny/api-tenancy/types";
import { CreateApwContextParams } from "~/scheduler/types";
Expand All @@ -16,6 +14,7 @@ import { isInstallationPending } from "./utils";
import { extendPbPageSettingsSchema } from "~/plugins/pageBuilder/extendPbPageSettingsSchema";
import { apwContentPagePlugins } from "~/plugins/pageBuilder/apwContentPagePlugins";
import { apwCmsHooks } from "~/plugins/cms";
import { I18NLocale } from "@webiny/api-i18n/types";

const setupApwContext = (params: CreateApwContextParams) =>
new ContextPlugin<ApwContext>(async context => {
Expand All @@ -25,15 +24,7 @@ const setupApwContext = (params: CreateApwContextParams) =>
return;
}

const contentHeadlessCmsContextPlugins = createHeadlessCmsContext({
storageOperations: context.cms.storageOperations
});
/**
* Register cms plugins required by `api-apw` package.
*/
context.plugins.register([createManageCMSPlugin(), ...contentHeadlessCmsContextPlugins]);

const getLocale = () => {
const getLocale = (): I18NLocale => {
const locale = i18n.getContentLocale();
if (!locale) {
throw new WebinyError(
Expand Down
19 changes: 0 additions & 19 deletions packages/api-apw/src/plugins/createManageCMSPlugin.ts

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ const {
const {
createElasticsearchClient
} = require("@webiny/project-utils/testing/elasticsearch/createClient");

const { createGraphQLFields } = require("@webiny/api-headless-cms");
/**
* For this to work it must load plugins that have already been built
*/
Expand Down Expand Up @@ -129,7 +127,6 @@ class CmsTestEnvironment extends NodeEnvironment {
return createStorageOperations({
documentClient,
elasticsearch: elasticsearchClient,
modelFieldToGraphQLPlugins: createGraphQLFields(),
table: table => ({ ...table, name: process.env.DB_TABLE }),
esTable: table => ({ ...table, name: process.env.DB_TABLE_ELASTICSEARCH }),
plugins: testPlugins.concat([
Expand Down
24 changes: 14 additions & 10 deletions packages/api-headless-cms-ddb-es/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { getElasticsearchOperators } from "@webiny/api-elasticsearch/operators";
import { elasticsearchFields as cmsEntryElasticsearchFields } from "~/operations/entry/elasticsearchFields";
import { elasticsearchIndexPlugins } from "./elasticsearch/indices";
import { deleteElasticsearchIndex } from "./elasticsearch/deleteElasticsearchIndex";
import { CmsModelFieldToGraphQLPlugin } from "@webiny/api-headless-cms/types";

export const createStorageOperations: StorageOperationsFactory = params => {
const {
Expand All @@ -29,8 +30,7 @@ export const createStorageOperations: StorageOperationsFactory = params => {
esTable,
documentClient,
elasticsearch,
plugins: userPlugins,
modelFieldToGraphQLPlugins
plugins: userPlugins
} = params;

const tableInstance = createTable({
Expand Down Expand Up @@ -83,7 +83,6 @@ export const createStorageOperations: StorageOperationsFactory = params => {
/**
* Plugins of type CmsModelFieldToGraphQLPlugin.
*/
modelFieldToGraphQLPlugins,
/**
* Elasticsearch field definitions for the entry record.
*/
Expand Down Expand Up @@ -112,13 +111,18 @@ export const createStorageOperations: StorageOperationsFactory = params => {

return {
beforeInit: async context => {
context.plugins.register([
/**
* Field plugins for DynamoDB.
* We must pass them to the base application.
*/
dynamoDbPlugins()
]);
/**
* Collect all required plugins from parent context.
*/
const fieldPlugins = context.plugins.byType<CmsModelFieldToGraphQLPlugin>(
"cms-model-field-to-graphql"
);
plugins.register(fieldPlugins);

/**
* Pass the plugins to the parent context.
*/
context.plugins.register([dynamoDbPlugins()]);
},
init: async context => {
/**
Expand Down
1 change: 0 additions & 1 deletion packages/api-headless-cms-ddb-es/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,6 @@ export interface StorageOperationsFactoryParams {
elasticsearch: Client;
table?: TableModifier;
esTable?: TableModifier;
modelFieldToGraphQLPlugins: CmsModelFieldToGraphQLPlugin[];
attributes?: Record<ENTITIES, Attributes>;
plugins?: PluginCollection;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ const dbPlugins = require("@webiny/handler-db").default;
const { DynamoDbDriver } = require("@webiny/db-dynamodb");
const { DocumentClient } = require("aws-sdk/clients/dynamodb");
const NodeEnvironment = require("jest-environment-node");

const { createGraphQLFields } = require("@webiny/api-headless-cms");
/**
* For this to work it must load plugins that have already been built
*/
Expand Down Expand Up @@ -47,7 +45,6 @@ class CmsTestEnvironment extends NodeEnvironment {
const { plugins: testPlugins = [] } = params;
return createStorageOperations({
documentClient,
modelFieldToGraphQLPlugins: createGraphQLFields(),
table: table => ({ ...table, name: process.env.DB_TABLE }),
plugins: testPlugins
});
Expand Down
32 changes: 14 additions & 18 deletions packages/api-headless-cms-ddb/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,10 @@ import { createSettingsStorageOperations } from "~/operations/settings";
import { createGroupsStorageOperations } from "~/operations/group";
import { createModelsStorageOperations } from "~/operations/model";
import { createEntriesStorageOperations } from "./operations/entry";
import { CmsModelFieldToGraphQLPlugin } from "@webiny/api-headless-cms/types";

export const createStorageOperations: StorageOperationsFactory = params => {
const {
attributes,
table,
documentClient,
plugins: userPlugins,
modelFieldToGraphQLPlugins
} = params;
const { attributes, table, documentClient, plugins: userPlugins } = params;

const tableInstance = createTable({
table,
Expand Down Expand Up @@ -61,10 +56,6 @@ export const createStorageOperations: StorageOperationsFactory = params => {
* User defined custom plugins.
*/
...(userPlugins || []),
/**
* Plugins of type CmsModelFieldToGraphQLPlugin.
*/
modelFieldToGraphQLPlugins,
/**
* DynamoDB filter plugins for the where conditions.
*/
Expand All @@ -77,13 +68,18 @@ export const createStorageOperations: StorageOperationsFactory = params => {

return {
beforeInit: async context => {
context.plugins.register([
/**
* Field plugins for DynamoDB.
* We must pass them to the base application.
*/
dynamoDbPlugins()
]);
/**
* Collect all required plugins from parent context.
*/
const fieldPlugins = context.plugins.byType<CmsModelFieldToGraphQLPlugin>(
"cms-model-field-to-graphql"
);
plugins.register(fieldPlugins);

/**
* Pass the plugins to the parent context.
*/
context.plugins.register([dynamoDbPlugins()]);
},
getEntities: () => entities,
getTable: () => tableInstance,
Expand Down
2 changes: 0 additions & 2 deletions packages/api-headless-cms-ddb/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { Plugin } from "@webiny/plugins/types";
import {
CmsModelField,
CmsModelFieldToGraphQLPlugin,
HeadlessCmsStorageOperations as BaseHeadlessCmsStorageOperations
} from "@webiny/api-headless-cms/types";
import { DynamoDBTypes, TableConstructor } from "dynamodb-toolbox/dist/classes/Table";
Expand Down Expand Up @@ -54,7 +53,6 @@ export interface TableModifier {
export interface StorageOperationsFactoryParams {
documentClient: DocumentClient;
table?: TableModifier;
modelFieldToGraphQLPlugins: CmsModelFieldToGraphQLPlugin[];
attributes?: Record<ENTITIES, Attributes>;
plugins?: Plugin[] | Plugin[][];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import { useCategoryReadHandler } from "../utils/useCategoryReadHandler";
import mdbid from "mdbid";
import { useProductReadHandler } from "../utils/useProductReadHandler";
import { useProductManageHandler } from "../utils/useProductManageHandler";
import { PluginsContainer } from "@webiny/plugins";
import { createGraphQLFields } from "~/graphqlFields";
const cliPackageJson = require("@webiny/cli/package.json");
const webinyVersion = cliPackageJson.version;

Expand Down Expand Up @@ -361,6 +363,12 @@ describe("Republish entries", () => {

const { storageOperations } = useCategoryManageHandler(manageOpts);

if (storageOperations.beforeInit) {
await storageOperations.beforeInit({
plugins: new PluginsContainer(createGraphQLFields())
} as any);
}

const { entry: galaEntry } = createEntry(productModel, {
title: "Gala",
category: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import {
} from "./helpers";
import { CmsModel, HeadlessCmsStorageOperations } from "~/types";
import { useGraphQLHandler } from "../utils/useGraphQLHandler";
import { createGraphQLFields } from "~/graphqlFields";
import { PluginsContainer } from "@webiny/plugins";

jest.setTimeout(60000);

Expand Down Expand Up @@ -47,6 +49,20 @@ describe("Entries storage operations", () => {
path: "manage/en-US"
});

/**
* We must load CMS GraphQL field plugins for the storage operations to work.
* This is specifically for DDB and DDB+ES storage operations.
* Some others might not need them...
*/
beforeAll(async () => {
if (!storageOperations.beforeInit) {
return;
}
await storageOperations.beforeInit({
plugins: new PluginsContainer(createGraphQLFields())
} as any);
});

beforeEach(async () => {
await deletePersonModel({
storageOperations
Expand Down
2 changes: 1 addition & 1 deletion packages/api-headless-cms/src/graphql/system.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ const plugin: GraphQLSchemaPlugin<CmsContext> = {
*/
export const createSystemSchemaPlugin = (): ContextPlugin<CmsContext> => {
return new ContextPlugin<CmsContext>(async context => {
if (context.cms.type) {
if (context.cms?.type) {
return;
}
context.plugins.register(plugin);
Expand Down