-
Notifications
You must be signed in to change notification settings - Fork 617
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: introduce page builder data sources and data bindings (#4469)
- Loading branch information
Showing
293 changed files
with
6,658 additions
and
1,525 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { createZodError } from "@webiny/utils"; | ||
import type { DataLoaderResult, IDataLoader, IDataSource } from "~/dataSources/types"; | ||
import type { DataLoaderRequest } from "~/dataSources/DataLoaderRequest"; | ||
|
||
export class DataLoader implements IDataLoader { | ||
private readonly dataSources: IDataSource[]; | ||
|
||
constructor(dataSources: IDataSource[]) { | ||
this.dataSources = dataSources; | ||
} | ||
|
||
async load(request: DataLoaderRequest): Promise<DataLoaderResult> { | ||
const type = request.getType(); | ||
const dataSource = this.dataSources.find(ds => ds.getType() === type); | ||
|
||
if (!dataSource) { | ||
throw new Error(`Can't find dataSource ${type}`); | ||
} | ||
|
||
const configSchema = dataSource.getConfigSchema(); | ||
const result = await configSchema.safeParseAsync(request.getConfig()); | ||
if (!result.success) { | ||
throw createZodError(result.error); | ||
} | ||
|
||
return dataSource.load(request.withConfig(result.data)); | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
packages/api-page-builder/src/dataSources/DataLoaderRequest.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import type { GenericRecord } from "@webiny/api/types"; | ||
import type { RequestDto } from "~/dataSources/types"; | ||
|
||
export class DataLoaderRequest<TConfig extends GenericRecord = GenericRecord> { | ||
private readonly type: string; | ||
private readonly config: TConfig; | ||
private readonly paths: string[]; | ||
|
||
protected constructor(type: string, config: TConfig, paths: string[]) { | ||
this.type = type; | ||
this.config = config; | ||
this.paths = paths; | ||
} | ||
|
||
static create(params: RequestDto) { | ||
return new DataLoaderRequest(params.type, params.config, params.paths); | ||
} | ||
|
||
getType() { | ||
return this.type; | ||
} | ||
|
||
getConfig() { | ||
return this.config; | ||
} | ||
|
||
getPaths() { | ||
return this.paths || []; | ||
} | ||
|
||
withConfig(config: TConfig) { | ||
return new DataLoaderRequest(this.type, config, this.paths); | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
packages/api-page-builder/src/dataSources/cmsDataSources/CmsEntriesDataSource.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import zod from "zod"; | ||
import type { CmsContext } from "@webiny/api-headless-cms/types"; | ||
import type { DataLoaderRequest, DataLoaderResult, IDataSource } from "~/dataSources"; | ||
import { ModelListQuery } from "~/dataSources/cmsDataSources/ModelListQuery"; | ||
|
||
export interface CmsEntriesDataSourceConfig { | ||
modelId: string; | ||
limit: number; | ||
} | ||
|
||
export class CmsEntriesDataSource implements IDataSource<CmsEntriesDataSourceConfig> { | ||
private cms: CmsContext["cms"]; | ||
|
||
constructor(cms: CmsContext["cms"]) { | ||
this.cms = cms; | ||
} | ||
|
||
getType() { | ||
return "cms.entries"; | ||
} | ||
|
||
getConfigSchema(): zod.Schema { | ||
return zod.object({ | ||
modelId: zod.string(), | ||
limit: zod.number() | ||
}); | ||
} | ||
|
||
async load(request: DataLoaderRequest<CmsEntriesDataSourceConfig>): Promise<DataLoaderResult> { | ||
const requestedPaths = request.getPaths(); | ||
const queryPaths = !requestedPaths || requestedPaths.length === 0 ? ["id"] : requestedPaths; | ||
|
||
const config = request.getConfig(); | ||
const schemaClient = await this.cms.getExecutableSchema("preview"); | ||
const model = await this.cms.getModel(config.modelId); | ||
|
||
const listQuery = new ModelListQuery(); | ||
const query = listQuery.getQuery(model, queryPaths); | ||
|
||
const response = await schemaClient({ | ||
query, | ||
operationName: "ListEntries", | ||
variables: { | ||
limit: config.limit || 10 | ||
} | ||
}); | ||
|
||
// @ts-expect-error Naive return for the time being. | ||
return response.data.entries.data; | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
packages/api-page-builder/src/dataSources/cmsDataSources/CmsEntryDataSource.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import zod from "zod"; | ||
import type { CmsContext } from "@webiny/api-headless-cms/types"; | ||
import type { DataLoaderRequest, DataLoaderResult, IDataSource } from "~/dataSources"; | ||
import { ModelGetQuery } from "~/dataSources/cmsDataSources/ModelGetQuery"; | ||
|
||
export interface CmsEntryDataSourceConfig { | ||
modelId: string; | ||
entryId: string; | ||
} | ||
|
||
export class CmsEntryDataSource implements IDataSource<CmsEntryDataSourceConfig> { | ||
private cms: CmsContext["cms"]; | ||
|
||
constructor(cms: CmsContext["cms"]) { | ||
this.cms = cms; | ||
} | ||
|
||
getType() { | ||
return "cms.entry"; | ||
} | ||
|
||
getConfigSchema(): zod.Schema { | ||
return zod.object({ | ||
modelId: zod.string(), | ||
entryId: zod.string() | ||
}); | ||
} | ||
|
||
async load(request: DataLoaderRequest<CmsEntryDataSourceConfig>): Promise<DataLoaderResult> { | ||
const requestedPaths = request.getPaths(); | ||
const queryPaths = !requestedPaths || requestedPaths.length === 0 ? ["id"] : requestedPaths; | ||
|
||
const config = request.getConfig(); | ||
const schemaClient = await this.cms.getExecutableSchema("preview"); | ||
const model = await this.cms.getModel(config.modelId); | ||
|
||
const listQuery = new ModelGetQuery(); | ||
const query = listQuery.getQuery(model, queryPaths); | ||
|
||
const response = await schemaClient({ | ||
query, | ||
operationName: "GetEntry", | ||
variables: { | ||
entryId: config.entryId | ||
} | ||
}); | ||
|
||
// @ts-expect-error Naive return for the time being. | ||
return response.data.entry.data; | ||
} | ||
} |
Oops, something went wrong.