Skip to content

Commit

Permalink
feat(category): add injectable fragments and transform support to mag…
Browse files Browse the repository at this point in the history
…ento driver (#2828)
  • Loading branch information
griest024 committed Jun 5, 2024
1 parent 47b7345 commit 0968515
Show file tree
Hide file tree
Showing 9 changed files with 88 additions and 6 deletions.
6 changes: 4 additions & 2 deletions libs/category/driver/magento/src/category.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import {
DAFF_PRODUCT_MAGENTO_EXTRA_PRODUCT_PREVIEW_FRAGMENTS,
} from '@daffodil/product/driver/magento';

import { MAGENTO_CATEGORY_EXTRA_FRAGMENTS } from './fragments/public_api';
import {
MAGENTO_CATEGORY_CONFIG_TOKEN,
DaffCategoryMagentoDriverConfig,
Expand Down Expand Up @@ -81,6 +82,7 @@ export class DaffMagentoCategoryService implements DaffCategoryServiceInterface
@Inject(MAGENTO_PRODUCT_CONFIG_TOKEN) private productConfig: DaffProductMagentoDriverConfig,
@Inject(DAFF_PRODUCT_MAGENTO_EXTRA_PRODUCT_FRAGMENTS) private extraProductFragments: DocumentNode[],
@Inject(DAFF_PRODUCT_MAGENTO_EXTRA_PRODUCT_PREVIEW_FRAGMENTS) private extraProductPreviewFragments: DocumentNode[],
@Inject(MAGENTO_CATEGORY_EXTRA_FRAGMENTS) private extraCategoryFragments: DocumentNode[],
) {}

get(categoryRequest: DaffCategoryIdRequest): Observable<DaffGetCategoryResponse> {
Expand All @@ -89,7 +91,7 @@ export class DaffMagentoCategoryService implements DaffCategoryServiceInterface
query: MagentoProductGetFilterTypes,
}),
this.apollo.query<MagentoGetCategoryAndProductsResponse>({
query: MagentoGetCategoryAndProductsQuery([
query: MagentoGetCategoryAndProductsQuery(this.extraCategoryFragments, [
...this.extraProductFragments,
...this.extraProductPreviewFragments,
]),
Expand Down Expand Up @@ -123,7 +125,7 @@ export class DaffMagentoCategoryService implements DaffCategoryServiceInterface
category,
filterTypes,
]) => this.apollo.query<MagentoGetCategoryAndProductsResponse>({
query: MagentoGetCategoryAndProductsQuery([
query: MagentoGetCategoryAndProductsQuery(this.extraCategoryFragments, [
...this.extraProductFragments,
...this.extraProductPreviewFragments,
]),
Expand Down
1 change: 1 addition & 0 deletions libs/category/driver/magento/src/fragments/public_api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './token';
26 changes: 26 additions & 0 deletions libs/category/driver/magento/src/fragments/token.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { DocumentNode } from '@apollo/client/core';

import { createMultiInjectionToken } from '@daffodil/core';


export const {
/**
* A multi-provider injection token for providing category tree transform logic in the Magento driver.
* It is run after the standard transforms and passed both the current transformed Daffodil category and the Magento category.
*/
token: MAGENTO_CATEGORY_EXTRA_FRAGMENTS,
/**
* Provides category tree transforms for the Magento category driver.
*
* See {@link MAGENTO_CATEGORY_EXTRA_FRAGMENTS}.
*
* ```ts
* providers: [
* ...magentoProvideCategoryExtraFragments(
* myCategoryExtraFragment
* )
* ]
* ```
*/
provider: magentoProvideCategoryExtraFragments,
} = createMultiInjectionToken<DocumentNode>('MAGENTO_CATEGORY_EXTRA_FRAGMENTS');
2 changes: 2 additions & 0 deletions libs/category/driver/magento/src/public_api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@ export * from './models/public_api';
export * from './queries/public_api';
export * from './transformers/public_api';
export * from './interfaces/public_api';
export * from './transforms/public_api';
export * from './fragments/public_api';
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@ import { magentoCategoryTreeFragment } from './fragments/public_api';

export const DAFF_MAGENTO_GET_CATEGORY_AND_PRODUCTS_QUERY_NAME = 'MagentoGetCategoryAndProducts';

export const MagentoGetCategoryAndProductsQuery = (extraProductFragments: DocumentNode[] = []) => gql`
export const MagentoGetCategoryAndProductsQuery = (extraCategoryFragments: DocumentNode[] = [], extraProductFragments: DocumentNode[] = []) => gql`
query ${DAFF_MAGENTO_GET_CATEGORY_AND_PRODUCTS_QUERY_NAME}($categoryFilters: CategoryFilterInput, $productFilter: ProductAttributeFilterInput!, $search: String, $pageSize: Int, $currentPage: Int, $sort: ProductAttributeSortInput)
{
categoryList(filters: $categoryFilters) {
...categoryTree
${daffBuildFragmentNameSpread(...extraCategoryFragments)}
}
products(filter: $productFilter, search: $search, pageSize: $pageSize, currentPage: $currentPage, sort: $sort)
{
Expand All @@ -45,5 +46,6 @@ ${magentoProductFragment}
${magentoSearchResultPageInfoFragment}
${magentoProductSortFieldsFragment}
${magentoProductAggregationsFragment}
${daffBuildFragmentDefinition(...extraCategoryFragments)}
${daffBuildFragmentDefinition(...extraProductFragments)}
`;
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { Injectable } from '@angular/core';
import {
Inject,
Injectable,
} from '@angular/core';

import {
DaffCategory,
Expand All @@ -10,14 +13,21 @@ import {
MagentoBreadcrumb,
MagentoCategory,
} from '../models/public_api';
import {
MAGENTO_CATEGORY_EXTRA_TRANSFORMS,
MagentoCategoryTreeTransform,
} from '../transforms/public_api';

@Injectable({
providedIn: 'root',
})
export class DaffMagentoCategoryTransformerService {
constructor(
@Inject(MAGENTO_CATEGORY_EXTRA_TRANSFORMS) private extraTransforms: Array<MagentoCategoryTreeTransform>,
) {}

transform(category: MagentoCategory, products: MagentoProduct[]): DaffCategory {
return {
return this.extraTransforms.reduce<DaffCategory>((acc, transform) => transform(acc, category, products), {
id: category.uid,
url: `/${category.url_path}${category.url_suffix}`,
canonicalUrl: category.canonical_url,
Expand All @@ -31,7 +41,7 @@ export class DaffMagentoCategoryTransformerService {
.sort((a, b) => a.level - b.level) || null,
product_ids: products.map(product => product.sku),
total_products: products.length,
};
});
}

private transformBreadcrumb(breadcrumb: MagentoBreadcrumb, category: MagentoCategory): DaffCategoryBreadcrumb {
Expand Down
2 changes: 2 additions & 0 deletions libs/category/driver/magento/src/transforms/public_api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './token';
export * from './type';
27 changes: 27 additions & 0 deletions libs/category/driver/magento/src/transforms/token.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// workaround https://github.com/graycoreio/daffodil/issues/1667
import { DaffCategory } from '@daffodil/category';
import { createMultiInjectionToken } from '@daffodil/core';

import { MagentoCategoryTreeTransform } from './type';

export const {
/**
* A multi-provider injection token for providing category transform logic in the Magento driver.
* It is run after the standard transforms and passed both the current transformed Daffodil category and the Magento category.
*/
token: MAGENTO_CATEGORY_EXTRA_TRANSFORMS,
/**
* Provides category transforms for the Magento category driver.
*
* See {@link MAGENTO_CATEGORY_EXTRA_TRANSFORMS}.
*
* ```ts
* providers: [
* ...magentoProvideCategoryExtraTransforms(
* myCategoryExtraTransform
* )
* ]
* ```
*/
provider: magentoProvideCategoryExtraTransforms,
} = createMultiInjectionToken<MagentoCategoryTreeTransform>('MAGENTO_CATEGORY_EXTRA_TRANSFORMS');
10 changes: 10 additions & 0 deletions libs/category/driver/magento/src/transforms/type.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { DaffCategory } from '@daffodil/category';
import { MagentoProduct } from '@daffodil/product/driver/magento';

import { MagentoCategory } from '../models/public_api';

/**
* A transform for the Magento driver that can add extra fields or otherwise modify the category driver response.
*/
export type MagentoCategoryTreeTransform<T extends MagentoCategory = MagentoCategory, V extends DaffCategory = DaffCategory> =
(daffCategory: DaffCategory, magentoCategory: T, products: MagentoProduct[]) => V;

0 comments on commit 0968515

Please sign in to comment.