Skip to content

Commit

Permalink
feat(customer-order): create injection tokens with factory (#3263)
Browse files Browse the repository at this point in the history
  • Loading branch information
griest024 authored Oct 14, 2024
1 parent e3a6cc1 commit 87ff316
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 43 deletions.
Original file line number Diff line number Diff line change
@@ -1,24 +1,23 @@
import {
InjectionToken,
Provider,
} from '@angular/core';
import { Provider } from '@angular/core';
import { DocumentNode } from 'graphql';

/**
* A multi-provider injection token for providing extra GraphQL fragments that will be spread into order queries.
* This can be used to retrieve additional data that is not covered by the standard Daffodil interfaces.
*
* Fragment structure is platform-specific and this feature should be used with care.
*
* This can increase query complexity above the Magento default of 300.
* Daffodil only guarantees that stock queries don't exceed the limit of 300.
* Apps using this token should therefore increase the query complexity limit
* to accommodate the extra complexity contributed by the provided fragments.
*/
export const DAFF_CUSTOMER_ORDER_MAGENTO_EXTRA_ORDER_FRAGMENTS = new InjectionToken<DocumentNode[]>(
'DAFF_CUSTOMER_ORDER_MAGENTO_EXTRA_ORDER_FRAGMENTS',
{ factory: () => []},
);
import { createMultiInjectionToken } from '@daffodil/core';

const {
/**
* A multi-provider injection token for providing extra GraphQL fragments that will be spread into order queries.
* This can be used to retrieve additional data that is not covered by the standard Daffodil interfaces.
*
* Fragment structure is platform-specific and this feature should be used with care.
*
* This can increase query complexity above the Magento default of 300.
* Daffodil only guarantees that stock queries don't exceed the limit of 300.
* Apps using this token should therefore increase the query complexity limit
* to accommodate the extra complexity contributed by the provided fragments.
*/
token: DAFF_CUSTOMER_ORDER_MAGENTO_EXTRA_ORDER_FRAGMENTS,
provider,
} = createMultiInjectionToken<DocumentNode>('DAFF_CUSTOMER_ORDER_MAGENTO_EXTRA_ORDER_FRAGMENTS');

/**
* Provides extra GraphQL fragments for the Magento order driver.
Expand All @@ -34,9 +33,7 @@ export const DAFF_CUSTOMER_ORDER_MAGENTO_EXTRA_ORDER_FRAGMENTS = new InjectionTo
* ```
*/
export function daffProvideCustomerOrderMagentoExtraOrderFragments(...fragments: DocumentNode[]): Provider[] {
return fragments.map(fragment => ({
provide: DAFF_CUSTOMER_ORDER_MAGENTO_EXTRA_ORDER_FRAGMENTS,
useValue: fragment,
multi: true,
}));
return provider(...fragments);
}

export { DAFF_CUSTOMER_ORDER_MAGENTO_EXTRA_ORDER_FRAGMENTS };
Original file line number Diff line number Diff line change
@@ -1,24 +1,22 @@
import {
InjectionToken,
Provider,
} from '@angular/core';
import { Provider } from '@angular/core';

// workaround https://github.com/graycoreio/daffodil/issues/1667
import { createMultiInjectionToken } from '@daffodil/core';
import { DaffOrder } from '@daffodil/order';

import { DaffMagentoCustomerOrderExtraTransform } from '../../interfaces/public_api';
import { MagentoCustomerOrder } from '../../models/public_api';

/**
* A multi-provider injection token for providing extra transform logic in the Order Magento driver.
* It is run after the standard transforms for each customer order preview and passed both the current transformed Daffodil customer order and the Magento customer order.
*
* See {@link MagentoCustomerOrder} for more info.
*/
export const DAFF_CUSTOMER_ORDER_MAGENTO_EXTRA_ORDER_TRANSFORMS = new InjectionToken<DaffMagentoCustomerOrderExtraTransform[]>(
'DAFF_CUSTOMER_ORDER_MAGENTO_EXTRA_ORDER_TRANSFORMS',
{ factory: () => []},
);
const {
/**
* A multi-provider injection token for providing extra transform logic in the Order Magento driver.
* It is run after the standard transforms for each customer order preview and passed both the current transformed Daffodil customer order and the Magento customer order.
*
* See {@link MagentoCustomerOrder} for more info.
*/
token: DAFF_CUSTOMER_ORDER_MAGENTO_EXTRA_ORDER_TRANSFORMS,
provider,
} = createMultiInjectionToken<DaffMagentoCustomerOrderExtraTransform>('DAFF_CUSTOMER_ORDER_MAGENTO_EXTRA_ORDER_TRANSFORMS');

/**
* Provides extra customer order preview transforms for the Magento customer order driver.
Expand All @@ -34,9 +32,7 @@ export const DAFF_CUSTOMER_ORDER_MAGENTO_EXTRA_ORDER_TRANSFORMS = new InjectionT
* ```
*/
export function daffProvideCustomerOrderMagentoExtraOrderTransforms<T extends MagentoCustomerOrder = MagentoCustomerOrder, V extends DaffOrder = DaffOrder>(...transforms: DaffMagentoCustomerOrderExtraTransform<T, V>[]): Provider[] {
return transforms.map(transform => ({
provide: DAFF_CUSTOMER_ORDER_MAGENTO_EXTRA_ORDER_TRANSFORMS,
useValue: transform,
multi: true,
}));
return provider(...transforms);
}

export { DAFF_CUSTOMER_ORDER_MAGENTO_EXTRA_ORDER_TRANSFORMS };

0 comments on commit 87ff316

Please sign in to comment.