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

Page builder blocks v2 read operations #2487

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
import WebinyError from "@webiny/error";
import {
PageBlock,
PageBlockStorageOperations,
PageBlockStorageOperationsCreateParams
PageBlockStorageOperationsCreateParams,
PageBlockStorageOperationsGetParams,
PageBlockStorageOperationsListParams
} from "@webiny/api-page-builder/types";
import { Entity } from "dynamodb-toolbox";
import { queryAll, QueryAllParams } from "@webiny/db-dynamodb/utils/query";
import { sortItems } from "@webiny/db-dynamodb/utils/sort";
import { filterItems } from "@webiny/db-dynamodb/utils/filter";
import { PageBlockDataLoader } from "./dataLoader";
import { createListResponse } from "@webiny/db-dynamodb/utils/listResponse";
import { PageBlockDynamoDbFieldPlugin } from "~/plugins/definitions/PageBlockDynamoDbFieldPlugin";
import { PluginsContainer } from "@webiny/plugins";
import { createPartitionKey, createSortKey } from "./keys";

Expand All @@ -17,12 +25,81 @@ export interface CreatePageBlockStorageOperationsParams {
plugins: PluginsContainer;
}
export const createPageBlockStorageOperations = ({
entity
entity,
plugins
}: CreatePageBlockStorageOperationsParams): PageBlockStorageOperations => {
const dataLoader = new PageBlockDataLoader({
entity
});

const get = async (params: PageBlockStorageOperationsGetParams) => {
const { where } = params;

try {
return await dataLoader.getOne(where);
} catch (ex) {
throw new WebinyError(
ex.message || "Could not load page block by given parameters.",
ex.code || "PAGE_BLOCK_GET_ERROR",
{
where
}
);
}
};

const list = async (params: PageBlockStorageOperationsListParams) => {
const { where, sort, limit } = params;

const { tenant, locale, ...restWhere } = where;
const queryAllParams: QueryAllParams = {
entity,
partitionKey: createPartitionKey({ tenant, locale }),
options: {
gt: " "
}
};

let items: PageBlock[] = [];

try {
items = await queryAll<PageBlock>(queryAllParams);
} catch (ex) {
throw new WebinyError(
ex.message || "Could not list page blocks by given parameters.",
ex.code || "PAGE_BLOCK_LIST_ERROR",
{
partitionKey: queryAllParams.partitionKey,
options: queryAllParams.options
}
);
}

const fields = plugins.byType<PageBlockDynamoDbFieldPlugin>(
PageBlockDynamoDbFieldPlugin.type
);

const filteredItems = filterItems<PageBlock>({
plugins,
where: restWhere,
items,
fields
});

const sortedItems = sortItems<PageBlock>({
items: filteredItems,
sort,
fields
});

return createListResponse({
items: sortedItems,
limit: limit || 100000,
totalCount: filteredItems.length,
after: null
});
};

const create = async (params: PageBlockStorageOperationsCreateParams) => {
const { pageBlock } = params;

Expand Down Expand Up @@ -58,6 +135,8 @@ export const createPageBlockStorageOperations = ({
};

return {
get,
list,
create
};
};
83 changes: 81 additions & 2 deletions packages/api-page-builder-so-ddb/src/operations/pageBlock/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
import WebinyError from "@webiny/error";
import {
PageBlock,
PageBlockStorageOperations,
PageBlockStorageOperationsCreateParams
PageBlockStorageOperationsCreateParams,
PageBlockStorageOperationsGetParams,
PageBlockStorageOperationsListParams
} from "@webiny/api-page-builder/types";
import { Entity } from "dynamodb-toolbox";
import { queryAll, QueryAllParams } from "@webiny/db-dynamodb/utils/query";
import { sortItems } from "@webiny/db-dynamodb/utils/sort";
import { filterItems } from "@webiny/db-dynamodb/utils/filter";
import { PageBlockDataLoader } from "./dataLoader";
import { createListResponse } from "@webiny/db-dynamodb/utils/listResponse";
import { PageBlockDynamoDbFieldPlugin } from "~/plugins/definitions/PageBlockDynamoDbFieldPlugin";
import { PluginsContainer } from "@webiny/plugins";
import { createPartitionKey, createSortKey } from "./keys";

Expand All @@ -17,12 +25,81 @@ export interface CreatePageBlockStorageOperationsParams {
plugins: PluginsContainer;
}
export const createPageBlockStorageOperations = ({
entity
entity,
plugins
}: CreatePageBlockStorageOperationsParams): PageBlockStorageOperations => {
const dataLoader = new PageBlockDataLoader({
entity
});

const get = async (params: PageBlockStorageOperationsGetParams) => {
const { where } = params;

try {
return await dataLoader.getOne(where);
} catch (ex) {
throw new WebinyError(
ex.message || "Could not load page block by given parameters.",
ex.code || "PAGE_BLOCK_GET_ERROR",
{
where
}
);
}
};

const list = async (params: PageBlockStorageOperationsListParams) => {
const { where, sort, limit } = params;

const { tenant, locale, ...restWhere } = where;
const queryAllParams: QueryAllParams = {
entity,
partitionKey: createPartitionKey({ tenant, locale }),
options: {
gt: " "
}
};

let items: PageBlock[] = [];

try {
items = await queryAll<PageBlock>(queryAllParams);
} catch (ex) {
throw new WebinyError(
ex.message || "Could not list page blocks by given parameters.",
ex.code || "PAGE_BLOCK_LIST_ERROR",
{
partitionKey: queryAllParams.partitionKey,
options: queryAllParams.options
}
);
}

const fields = plugins.byType<PageBlockDynamoDbFieldPlugin>(
PageBlockDynamoDbFieldPlugin.type
);

const filteredItems = filterItems<PageBlock>({
plugins,
where: restWhere,
items,
fields
});

const sortedItems = sortItems<PageBlock>({
items: filteredItems,
sort,
fields
});

return createListResponse({
items: sortedItems,
limit: limit || 100000,
totalCount: filteredItems.length,
after: null
});
};

const create = async (params: PageBlockStorageOperationsCreateParams) => {
const { pageBlock } = params;

Expand Down Expand Up @@ -58,6 +135,8 @@ export const createPageBlockStorageOperations = ({
};

return {
get,
list,
create
};
};
22 changes: 22 additions & 0 deletions packages/api-page-builder/__tests__/graphql/graphql/pageBlocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,25 @@ export const CREATE_PAGE_BLOCK = /* GraphQL */ `
}
}
`;

export const LIST_PAGE_BLOCKS = /* GraphQL */ `
query ListPageBlocks {
pageBuilder {
listPageBlocks {
data ${DATA_FIELD}
error ${ERROR_FIELD}
}
}
}
`;

export const GET_PAGE_BLOCK = /* GraphQL */ `
query GetPageBlock($id: ID!) {
pageBuilder {
getPageBlock(id: $id) {
data ${DATA_FIELD}
error ${ERROR_FIELD}
}
}
}
`;
101 changes: 95 additions & 6 deletions packages/api-page-builder/__tests__/graphql/pageBlocks.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@ import { ErrorOptions } from "@webiny/error";
jest.setTimeout(100000);

describe("Page Blocks Test", () => {
const { createPageBlock, createBlockCategory } = useGqlHandler();
const { createPageBlock, getPageBlock, listPageBlocks, createBlockCategory } = useGqlHandler();

test("create, read page blocks", async () => {
const ids = [];
const prefixes = ["page-block-one-", "page-block-two-", "page-block-three-"];

test("create page blocks", async () => {
// Create block category
await createBlockCategory({
data: {
Expand All @@ -16,12 +19,13 @@ describe("Page Blocks Test", () => {
}
});

// Test creating, getting three page blocks.
for (let i = 0; i < 3; i++) {
const prefix = `page-block-${i}-`;
const prefix = prefixes[i];
const data = {
name: `${prefix}name`,
blockCategory: "block-category",
preview: { src: `https://test.com/${prefix}/src.jpg` },
blockCategory: `block-category`,
preview: { src: `https://test.com/${prefix}name/src.jpg` },
content: { some: `${prefix}content` }
};

Expand All @@ -40,10 +44,77 @@ describe("Page Blocks Test", () => {
}
}
});

ids.push(createPageBlockResponse.data.pageBuilder.createPageBlock.data.id);

const [getPageBlockResponse] = await getPageBlock({ id: ids[i] });
expect(getPageBlockResponse).toMatchObject({
data: {
pageBuilder: {
getPageBlock: {
data,
error: null
}
}
}
});
}

// List should show three page blocks.
const [listPageBlocksResponse] = await listPageBlocks();
expect(listPageBlocksResponse).toMatchObject({
data: {
pageBuilder: {
listPageBlocks: {
data: [
{
blockCategory: "block-category",
content: {
some: "page-block-one-content"
},
createdBy: defaultIdentity,
createdOn: /^20/,
id: ids[0],
name: "page-block-one-name",
preview: {
src: "https://test.com/page-block-one-name/src.jpg"
}
},
{
blockCategory: "block-category",
content: {
some: "page-block-two-content"
},
createdBy: defaultIdentity,
createdOn: /^20/,
id: ids[1],
name: "page-block-two-name",
preview: {
src: "https://test.com/page-block-two-name/src.jpg"
}
},
{
blockCategory: "block-category",
content: {
some: "page-block-three-content"
},
createdBy: defaultIdentity,
createdOn: /^20/,
id: ids[2],
name: "page-block-three-name",
preview: {
src: "https://test.com/page-block-three-name/src.jpg"
}
}
],
error: null
}
}
}
});
});

test("cannot create page block if no such block category", async () => {
test("cannot create page block with empty or missing block category", async () => {
const [createPageBlockEmptyCategoryResponse] = await createPageBlock({
data: {
name: "name",
Expand Down Expand Up @@ -102,4 +173,22 @@ describe("Page Blocks Test", () => {
}
});
});

test("cannot get a page block by empty id", async () => {
const [getPageBlockEmptyIdResponse] = await getPageBlock({ id: "" });
expect(getPageBlockEmptyIdResponse).toMatchObject({
data: {
pageBuilder: {
getPageBlock: {
data: null,
error: {
code: "GET_PAGE_BLOCK_ERROR",
data: null,
message: "Could not load page block by empty id."
}
}
}
}
});
});
});
Loading