Skip to content

Commit

Permalink
feat(product): create injection tokens with factory (graycoreio#3255)
Browse files Browse the repository at this point in the history
  • Loading branch information
griest024 authored and gracetxgao committed Oct 24, 2024
1 parent b8416b9 commit fef6e0e
Show file tree
Hide file tree
Showing 23 changed files with 394 additions and 456 deletions.
Original file line number Diff line number Diff line change
@@ -1,40 +1,29 @@
import {
InjectionToken,
Provider,
} from '@angular/core';

import { createMultiInjectionToken } from '@daffodil/core';
// workaround https://github.com/graycoreio/daffodil/issues/1667
import { DaffProduct } from '@daffodil/product';
import { DaffProductDriverResponse } from '@daffodil/product/driver';

import { DaffInMemoryProductResponseExtraTransform } from '../../../interfaces/public_api';

/**
* A multi-provider injection token for providing extra transform logic in the Product In-Memory driver.
* It is run after the standard transforms and passed both the current transformed Daffodil response and the In-Memory product response.
*/
export const DAFF_PRODUCT_IN_MEMORY_EXTRA_PRODUCT_RESPONSE_TRANSFORMS = new InjectionToken<DaffInMemoryProductResponseExtraTransform[]>(
'DAFF_PRODUCT_IN_MEMORY_EXTRA_PRODUCT_RESPONSE_TRANSFORMS',
{ factory: () => []},
);
export const {
/**
* A multi-provider injection token for providing extra transform logic in the Product In-Memory driver.
* It is run after the standard transforms and passed both the current transformed Daffodil response and the In-Memory product response.
*/
token: DAFF_PRODUCT_IN_MEMORY_EXTRA_PRODUCT_RESPONSE_TRANSFORMS,

/**
* Provides extra product transforms for the In-Memory product driver.
*
* See {@link DAFF_PRODUCT_IN_MEMORY_EXTRA_PRODUCT_RESPONSE_TRANSFORMS}.
*
* ```ts
* providers: [
* ...daffProvideProductInMemoryExtraProductResponseTransforms(
* myExtraProductTransform
* )
* ]
* ```
*/
export function daffProvideProductInMemoryExtraProductResponseTransforms(...transforms: DaffInMemoryProductResponseExtraTransform[]): Provider[] {
return transforms.map(transform => ({
provide: DAFF_PRODUCT_IN_MEMORY_EXTRA_PRODUCT_RESPONSE_TRANSFORMS,
useValue: transform,
multi: true,
}));
}
/**
* Provides extra product transforms for the In-Memory product driver.
*
* See {@link DAFF_PRODUCT_IN_MEMORY_EXTRA_PRODUCT_RESPONSE_TRANSFORMS}.
*
* ```ts
* providers: [
* ...daffProvideProductInMemoryExtraProductResponseTransforms(
* myExtraProductTransform
* )
* ]
* ```
*/
provider: daffProvideProductInMemoryExtraProductResponseTransforms,
} = createMultiInjectionToken<DaffInMemoryProductResponseExtraTransform>('DAFF_PRODUCT_IN_MEMORY_EXTRA_PRODUCT_RESPONSE_TRANSFORMS');
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
import {
InjectionToken,
inject,
} from '@angular/core';
import { inject } from '@angular/core';

import { createSingleInjectionToken } from '@daffodil/core';
import { DaffProduct } from '@daffodil/product';
// workaround https://github.com/graycoreio/daffodil/issues/1667
import { DaffProductDriverResponse } from '@daffodil/product/driver';

import { DAFF_PRODUCT_IN_MEMORY_EXTRA_PRODUCT_RESPONSE_TRANSFORMS } from './response-extra.token';
import { DaffInMemoryProductResponseTransform } from '../../../interfaces/public_api';

/**
* An internal token to combine the In-Memory response transform with the injected transforms.
*/
export const DAFF_PRODUCT_IN_MEMORY_PRODUCT_RESPONSE_TRANSFORM = new InjectionToken<DaffInMemoryProductResponseTransform>(
export const {
/**
* An internal token to combine the In-Memory response transform with the injected transforms.
*/
token: DAFF_PRODUCT_IN_MEMORY_PRODUCT_RESPONSE_TRANSFORM,
provider: daffProvideProductInMemoryProductResponseTransform,
} = createSingleInjectionToken<DaffInMemoryProductResponseTransform>(
'DAFF_PRODUCT_IN_MEMORY_PRODUCT_RESPONSE_TRANSFORM',
{
factory: () => {
Expand Down
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 product 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_PRODUCT_MAGENTO_EXTRA_PRODUCT_PAGE_FRAGMENTS = new InjectionToken<DocumentNode[]>(
'DAFF_PRODUCT_MAGENTO_EXTRA_PRODUCT_PAGE_FRAGMENTS',
{ factory: () => []},
);
import { createMultiInjectionToken } from '@daffodil/core';

/**
* Provides extra GraphQL fragments for the Magento product driver.
*
* See {@link DAFF_PRODUCT_MAGENTO_EXTRA_PRODUCT_PAGE_FRAGMENTS}.
*
* ```ts
* providers: [
* ...daffProvideProductMagentoExtraProductPageFragments(
* myExtraProductFragment
* )
* ]
* ```
*/
export function daffProvideProductMagentoExtraProductPageFragments(...fragments: DocumentNode[]): Provider[] {
return fragments.map(fragment => ({
provide: DAFF_PRODUCT_MAGENTO_EXTRA_PRODUCT_PAGE_FRAGMENTS,
useValue: fragment,
multi: true,
}));
}
export const {
/**
* A multi-provider injection token for providing extra GraphQL fragments that will be spread into product 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_PRODUCT_MAGENTO_EXTRA_PRODUCT_PAGE_FRAGMENTS,

/**
* Provides extra GraphQL fragments for the Magento product driver.
*
* See {@link DAFF_PRODUCT_MAGENTO_EXTRA_PRODUCT_PAGE_FRAGMENTS}.
*
* ```ts
* providers: [
* ...daffProvideProductMagentoExtraProductPageFragments(
* myExtraProductFragment
* )
* ]
* ```
*/
provider: daffProvideProductMagentoExtraProductPageFragments,
} = createMultiInjectionToken<DocumentNode>('DAFF_PRODUCT_MAGENTO_EXTRA_PRODUCT_PAGE_FRAGMENTS');
Original file line number Diff line number Diff line change
@@ -1,46 +1,37 @@
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 product preview queries.
* This can be used to retrieve additional data that is not covered by the standard Daffodil interfaces.
* Fragments provided here will also be included in the {@link DAFF_PRODUCT_MAGENTO_EXTRA_PRODUCT_FRAGMENTS}.
* The same fragment should not be provided for both of these tokens.
*
* See {@link MagentoProductPreview} for more info.
*
* 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_PRODUCT_MAGENTO_EXTRA_PRODUCT_PREVIEW_FRAGMENTS = new InjectionToken<DocumentNode[]>(
'DAFF_PRODUCT_MAGENTO_EXTRA_PRODUCT_PREVIEW_FRAGMENTS',
{ factory: () => []},
);
import { createMultiInjectionToken } from '@daffodil/core';

/**
* Provides extra GraphQL fragments for the Magento product preview query.
*
* See {@link DAFF_PRODUCT_MAGENTO_EXTRA_PRODUCT_PREVIEW_FRAGMENTS}.
*
* ```ts
* providers: [
* ...daffProvideProductMagentoExtraProductPreviewFragments(
* myExtraProductPreviewFragment
* )
* ]
* ```
*/
export function daffProvideProductMagentoExtraProductPreviewFragments(...fragments: DocumentNode[]): Provider[] {
return fragments.map(fragment => ({
provide: DAFF_PRODUCT_MAGENTO_EXTRA_PRODUCT_PREVIEW_FRAGMENTS,
useValue: fragment,
multi: true,
}));
}
export const {
/**
* A multi-provider injection token for providing extra GraphQL fragments that will be spread into product preview queries.
* This can be used to retrieve additional data that is not covered by the standard Daffodil interfaces.
* Fragments provided here will also be included in the {@link DAFF_PRODUCT_MAGENTO_EXTRA_PRODUCT_FRAGMENTS}.
* The same fragment should not be provided for both of these tokens.
*
* See {@link MagentoProductPreview} for more info.
*
* 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_PRODUCT_MAGENTO_EXTRA_PRODUCT_PREVIEW_FRAGMENTS,

/**
* Provides extra GraphQL fragments for the Magento product preview query.
*
* See {@link DAFF_PRODUCT_MAGENTO_EXTRA_PRODUCT_PREVIEW_FRAGMENTS}.
*
* ```ts
* providers: [
* ...daffProvideProductMagentoExtraProductPreviewFragments(
* myExtraProductPreviewFragment
* )
* ]
* ```
*/
provider: daffProvideProductMagentoExtraProductPreviewFragments,
} = createMultiInjectionToken<DocumentNode>('DAFF_PRODUCT_MAGENTO_EXTRA_PRODUCT_PREVIEW_FRAGMENTS');
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 product 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_PRODUCT_MAGENTO_EXTRA_PRODUCT_FRAGMENTS = new InjectionToken<DocumentNode[]>(
'DAFF_PRODUCT_MAGENTO_EXTRA_PRODUCT_FRAGMENTS',
{ factory: () => []},
);
import { createMultiInjectionToken } from '@daffodil/core';

/**
* Provides extra GraphQL fragments for the Magento product driver.
*
* See {@link DAFF_PRODUCT_MAGENTO_EXTRA_PRODUCT_FRAGMENTS}.
*
* ```ts
* providers: [
* ...daffProvideProductMagentoExtraProductFragments(
* myExtraProductFragment
* )
* ]
* ```
*/
export function daffProvideProductMagentoExtraProductFragments(...fragments: DocumentNode[]): Provider[] {
return fragments.map(fragment => ({
provide: DAFF_PRODUCT_MAGENTO_EXTRA_PRODUCT_FRAGMENTS,
useValue: fragment,
multi: true,
}));
}
export const {
/**
* A multi-provider injection token for providing extra GraphQL fragments that will be spread into product 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_PRODUCT_MAGENTO_EXTRA_PRODUCT_FRAGMENTS,

/**
* Provides extra GraphQL fragments for the Magento product driver.
*
* See {@link DAFF_PRODUCT_MAGENTO_EXTRA_PRODUCT_FRAGMENTS}.
*
* ```ts
* providers: [
* ...daffProvideProductMagentoExtraProductFragments(
* myExtraProductFragment
* )
* ]
* ```
*/
provider: daffProvideProductMagentoExtraProductFragments,
} = createMultiInjectionToken<DocumentNode>('DAFF_PRODUCT_MAGENTO_EXTRA_PRODUCT_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';

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

import { DaffMagentoProductExtraTransform } from '../../../interfaces/product-preview-extra-transform.type';
import { MagentoProduct } from '../../../models/public_api';

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

/**
* Provides extra product preview transforms for the Magento product driver.
Expand All @@ -34,9 +32,5 @@ export const DAFF_PRODUCT_MAGENTO_EXTRA_PRODUCT_PREVIEW_TRANSFORMS = new Injecti
* ```
*/
export function daffProvideProductMagentoExtraProductPreviewTransforms<T extends MagentoProduct = MagentoProduct, V extends DaffProduct = DaffProduct>(...transforms: DaffMagentoProductExtraTransform<T, V>[]): Provider[] {
return transforms.map(transform => ({
provide: DAFF_PRODUCT_MAGENTO_EXTRA_PRODUCT_PREVIEW_TRANSFORMS,
useValue: transform,
multi: true,
}));
return provider<DaffMagentoProductExtraTransform<T, V>>(...transforms);
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import {
InjectionToken,
inject,
} from '@angular/core';
import { inject } from '@angular/core';

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

Expand All @@ -11,10 +9,13 @@ import { DaffMagentoProductTransform } from '../../../interfaces/product-preview
import { MagentoProduct } from '../../../models/magento-product';
import { transformMagentoProductPreview } from '../../../transforms/product-preview';

/**
* An internal token to combine the Magento preview transform with the injected transforms.
*/
export const DAFF_PRODUCT_MAGENTO_PRODUCT_PREVIEW_TRANSFORM = new InjectionToken<DaffMagentoProductTransform>(
export const {
/**
* An internal token to combine the Magento preview transform with the injected transforms.
*/
token: DAFF_PRODUCT_MAGENTO_PRODUCT_PREVIEW_TRANSFORM,
provider: daffProvideProductMagentoProductPreviewTransform,
} = createSingleInjectionToken<DaffMagentoProductTransform>(
'DAFF_PRODUCT_MAGENTO_PRODUCT_PREVIEW_TRANSFORM',
{
factory: () => {
Expand Down
Loading

0 comments on commit fef6e0e

Please sign in to comment.