Skip to content

Commit

Permalink
feat(navigation): add injectable fragment support to magento driver (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
griest024 committed Jun 19, 2024
1 parent 23ed32b commit 53e45f6
Show file tree
Hide file tree
Showing 10 changed files with 32 additions and 7 deletions.
5 changes: 4 additions & 1 deletion libs/navigation/driver/magento/src/navigation.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
Inject,
} from '@angular/core';
import { Apollo } from 'apollo-angular';
import { DocumentNode } from 'graphql';
import {
Observable,
of,
Expand All @@ -24,6 +25,7 @@ import {
MagentoNavigationDriverConfig,
} from './config/public_api';
import { GetCategoryTreeResponse } from './models/get-category-tree-response';
import { DAFF_NAVIGATION_MAGENTO_DRIVER_EXTRA_FRAGMENTS } from './queries/fragments/public_api';
import { daffMagentoGetCategoryTree } from './queries/get-category-tree';
import { magentoNavigationGetRootCategoryIdQuery } from './queries/get-root-category-id/public_api';

Expand All @@ -38,6 +40,7 @@ export class DaffMagentoNavigationService implements DaffNavigationServiceInterf
constructor(
private apollo: Apollo,
@Inject(DaffNavigationTransformer) private transformer: DaffNavigationTransformerInterface<DaffNavigationTree>,
@Inject(DAFF_NAVIGATION_MAGENTO_DRIVER_EXTRA_FRAGMENTS) private extraFragments: Array<DocumentNode>,
@Inject(MAGENTO_NAVIGATION_DRIVER_CONFIG) private config: MagentoNavigationDriverConfig,
) {}

Expand All @@ -57,7 +60,7 @@ export class DaffMagentoNavigationService implements DaffNavigationServiceInterf

get(categoryId: string): Observable<DaffNavigationTree> {
return this.apollo.query<GetCategoryTreeResponse>({
query: daffMagentoGetCategoryTree(this.config.navigationTreeQueryDepth),
query: daffMagentoGetCategoryTree(this.config.navigationTreeQueryDepth, this.extraFragments),
variables: {
filters: { category_uid: { eq: categoryId }},
},
Expand Down
2 changes: 1 addition & 1 deletion libs/navigation/driver/magento/src/public_api.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export { DaffNavigationMagentoDriverModule } from './navigation-driver.module';
export { DaffMagentoNavigationService } from './navigation.service';
export { DaffMagentoNavigationTransformerService } from './transformers/navigation-transformer';
export * from './queries/get-category-tree';
export * from './queries/public_api';
export * from './config/public_api';
export * from './models/public_api';
export * from './transforms/public_api';
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import { gql } from 'apollo-angular';
import { DocumentNode } from 'graphql';

import {
daffBuildFragmentDefinition,
daffBuildFragmentNameSpread,
} from '@daffodil/core/graphql';


const CATEGORY_NODE_FRAGMENT_NAME = 'categoryNode';

Expand All @@ -9,7 +14,7 @@ const CATEGORY_NODE_FRAGMENT_NAME = 'categoryNode';
*
* @param depth The maximum depth to which category children should be added to the fragment.
*/
export function getCategoryNodeFragment(depth: number = 3): DocumentNode {
export function getCategoryNodeFragment(depth: number = 3, extraFragments: Array<DocumentNode> = []): DocumentNode {
const fragmentBody = new Array(depth).fill(null).reduce(acc => `
...${CATEGORY_NODE_FRAGMENT_NAME}
children_count
Expand All @@ -34,10 +39,12 @@ export function getCategoryNodeFragment(depth: number = 3): DocumentNode {
}
position
product_count
${daffBuildFragmentNameSpread(...extraFragments)}
}
fragment recursiveCategoryNode on CategoryTree {
${fragmentBody}
}
${daffBuildFragmentDefinition(...extraFragments)}
`;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { DocumentNode } from 'graphql';

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

export const {
token: DAFF_NAVIGATION_MAGENTO_DRIVER_EXTRA_FRAGMENTS,
provider: daffNavigationMagentoDriverExtraFragments,
} = createMultiInjectionToken<DocumentNode>('DAFF_NAVIGATION_MAGENTO_DRIVER_EXTRA_FRAGMENTS');
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './category-node';
export * from './extra.token';
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './category-node/public_api';
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { gql } from 'apollo-angular';
import { DocumentNode } from 'graphql';

import { getCategoryNodeFragment } from './fragments/category-node';
import { getCategoryNodeFragment } from './fragments/category-node/category-node';

export const DAFF_MAGENTO_GET_CATEGORY_TREE_QUERY_NAME = 'MagentoGetCategoryTree';

Expand All @@ -9,13 +10,13 @@ export const DAFF_MAGENTO_GET_CATEGORY_TREE_QUERY_NAME = 'MagentoGetCategoryTree
*
* @param depth The maximum depth to which category children should be added to the fragment.
*/
export function daffMagentoGetCategoryTree(depth: number = 3) {
export function daffMagentoGetCategoryTree(depth: number = 3, extraFragments: Array<DocumentNode> = []) {
return gql`
query ${DAFF_MAGENTO_GET_CATEGORY_TREE_QUERY_NAME}($filters: CategoryFilterInput!){
categoryList(filters: $filters) {
...recursiveCategoryNode
}
}
${getCategoryNodeFragment(depth)}
${getCategoryNodeFragment(depth, extraFragments)}
`;
}
3 changes: 3 additions & 0 deletions libs/navigation/driver/magento/src/queries/public_api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export * from './fragments/public_api';
export * from './get-root-category-id/public_api';
export * from './get-category-tree';
2 changes: 1 addition & 1 deletion libs/navigation/testing/src/public_api.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export { DaffNavigationTreeFactory } from './factories/navigation-tree.factory';
export * from './factories/navigation-tree.factory';
export { isNavigation } from './helpers/navigation-helper';

0 comments on commit 53e45f6

Please sign in to comment.