Skip to content

Commit

Permalink
feat(customer-order): create injection tokens with factory (#3242)
Browse files Browse the repository at this point in the history
  • Loading branch information
griest024 authored Oct 14, 2024
1 parent bd0421a commit 31af2df
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 90 deletions.
Original file line number Diff line number Diff line change
@@ -1,42 +1,33 @@
import {
InjectionToken,
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';

/**
* Provides extra GraphQL fragments for the Magento order driver.
*
* See {@link DAFF_CUSTOMER_ORDER_MAGENTO_EXTRA_ORDER_FRAGMENTS}.
*
* ```ts
* providers: [
* ...daffProvideCustomerOrderMagentoExtraOrderFragments(
* myExtraOrderFragment
* )
* ]
* ```
*/
export function daffProvideCustomerOrderMagentoExtraOrderFragments(...fragments: DocumentNode[]): Provider[] {
return fragments.map(fragment => ({
provide: DAFF_CUSTOMER_ORDER_MAGENTO_EXTRA_ORDER_FRAGMENTS,
useValue: fragment,
multi: true,
}));
}
export 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,

/**
* Provides extra GraphQL fragments for the Magento order driver.
*
* See {@link DAFF_CUSTOMER_ORDER_MAGENTO_EXTRA_ORDER_FRAGMENTS}.
*
* ```ts
* providers: [
* ...daffProvideCustomerOrderMagentoExtraOrderFragments(
* myExtraOrderFragment
* )
* ]
* ```
*/
provider: daffProvideCustomerOrderMagentoExtraOrderFragments,
} = createMultiInjectionToken<DocumentNode>('DAFF_CUSTOMER_ORDER_MAGENTO_EXTRA_ORDER_FRAGMENTS');
Original file line number Diff line number Diff line change
@@ -1,42 +1,31 @@
import {
InjectionToken,
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: () => []},
);
export 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,

/**
* Provides extra customer order preview transforms for the Magento customer order driver.
*
* See {@link DAFF_CUSTOMER_ORDER_MAGENTO_EXTRA_ORDER_TRANSFORMS}.
*
* ```ts
* providers: [
* ...daffProvideCustomerOrderMagentoExtraOrderTransforms(
* myExtraOrderTransform
* )
* ]
* ```
*/
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,
}));
}
/**
* Provides extra customer order preview transforms for the Magento customer order driver.
*
* See {@link DAFF_CUSTOMER_ORDER_MAGENTO_EXTRA_ORDER_TRANSFORMS}.
*
* ```ts
* providers: [
* ...daffProvideCustomerOrderMagentoExtraOrderTransforms(
* myExtraOrderTransform
* )
* ]
* ```
*/
provider: daffProvideCustomerOrderMagentoExtraOrderTransforms,
} = createMultiInjectionToken<DaffMagentoCustomerOrderExtraTransform>('DAFF_CUSTOMER_ORDER_MAGENTO_EXTRA_ORDER_TRANSFORMS');
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
import {
InjectionToken,
inject,
} from '@angular/core';
import { inject } from '@angular/core';

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

import { DAFF_CUSTOMER_ORDER_MAGENTO_EXTRA_ORDER_TRANSFORMS } from './extra.token';
import { DaffMagentoCustomerOrderTransform } from '../../interfaces/public_api';
import { MagentoCustomerOrder } from '../../models/public_api';
import { daffMagentoCustomerOrderTransformOrder } from '../../transforms/responses/order';

/**
* An internal token to combine the Magento preview transform with the injected transforms.
*/
export const DAFF_CUSTOMER_ORDER_MAGENTO_ORDER_TRANSFORM = new InjectionToken<DaffMagentoCustomerOrderTransform>(
export const {
/**
* An internal token to combine the Magento preview transform with the injected transforms.
*/
token: DAFF_CUSTOMER_ORDER_MAGENTO_ORDER_TRANSFORM,
provider: daffProvideCustomerOrderMagentoOrderTransform,
} = createSingleInjectionToken<DaffMagentoCustomerOrderTransform>(
'DAFF_CUSTOMER_ORDER_MAGENTO_ORDER_TRANSFORM',
{
factory: () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import { InjectionToken } from '@angular/core';

import { createSingleInjectionToken } from '@daffodil/core';
import { daffTransformErrorToStateError } from '@daffodil/core/state';

/**
* Transforms `DaffError`s into `DaffStateError`s before they are serialized into state.
* Can be used to further refine Daffodil errors into more specific app errors.
*/
export const DAFF_CUSTOMER_ORDER_ERROR_MATCHER = new InjectionToken<typeof daffTransformErrorToStateError>(
export const {
/**
* Transforms `DaffError`s into `DaffStateError`s before they are serialized into state.
* Can be used to further refine Daffodil errors into more specific app errors.
*/
token: DAFF_CUSTOMER_ORDER_ERROR_MATCHER,
provider: daffProvideCustomerOrderErrorMatcher,
} = createSingleInjectionToken<typeof daffTransformErrorToStateError>(
'DAFF_CUSTOMER_ORDER_ERROR_MATCHER',
{ factory: () => daffTransformErrorToStateError },
);
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export { DAFF_CUSTOMER_ORDER_ERROR_MATCHER } from './error-matcher.token';
export * from './error-matcher.token';

0 comments on commit 31af2df

Please sign in to comment.