Skip to content

Commit f9c012d

Browse files
authored
fix(driver): provideMagentoDriver crashes when options is an InjectionToken (#4421)
1 parent 2d4789e commit f9c012d

3 files changed

Lines changed: 39 additions & 26 deletions

File tree

libs/driver/magento/src/features/transfer-state.ts

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,8 @@ import {
66
type EnvironmentProviders,
77
type StateKey,
88
} from '@angular/core';
9-
import {
10-
ApolloCache,
11-
NormalizedCacheObject,
12-
} from '@apollo/client/cache';
9+
import { NormalizedCacheObject } from '@apollo/client/cache';
10+
import { Apollo } from 'apollo-angular';
1311

1412
import { createSingleInjectionToken } from '@daffodil/core';
1513

@@ -41,24 +39,20 @@ export const withDaffDriverMagentoTransferState = <T extends StateKey<any> = Sta
4139
* Holds the logic for hydrating the Apollo cache with the server's transferred state.
4240
* It is recommended to use {@link withDaffDriverMagentoTransferState} with {@link provideMagentoDriver}
4341
* to provide this functionality automatically.
44-
*
45-
* @param cache The Apollo cache instance.
4642
*/
47-
export const provideDaffDriverMagentoTransferState = (cache: ApolloCache<NormalizedCacheObject>): EnvironmentProviders => makeEnvironmentProviders([
43+
export const provideDaffDriverMagentoTransferState = (): EnvironmentProviders => makeEnvironmentProviders([
4844
provideAppInitializer(() => {
45+
const cache = inject(Apollo).client.cache;
4946
const transferState = inject(TransferState);
5047
const stateKey = inject(DAFF_DRIVER_MAGENTO_TRANSFER_STATE_KEY);
5148
const hasStateKey = transferState.hasKey(stateKey);
5249
if (hasStateKey) {
53-
// reads the serialized cache
5450
const state = transferState.get<NormalizedCacheObject>(
5551
stateKey,
5652
null,
5753
);
58-
// and puts it in the Apollo
5954
cache.restore(state);
6055
} else {
61-
// serializes the cache and puts it under a key
6256
transferState.onSerialize(stateKey, () => cache.extract());
6357
}
6458
}),
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { InjectionToken } from '@angular/core';
2+
3+
import {
4+
DaffMagentoDriverConfig,
5+
provideMagentoDriver,
6+
} from './provider';
7+
8+
describe('@daffodil/driver/magento | provideMagentoDriver', () => {
9+
describe('when options is an InjectionToken', () => {
10+
const CONFIG_TOKEN = new InjectionToken<DaffMagentoDriverConfig>('test config');
11+
12+
it('should not throw when registering providers', () => {
13+
expect(() => {
14+
provideMagentoDriver(CONFIG_TOKEN);
15+
}).not.toThrow();
16+
});
17+
});
18+
});

libs/driver/magento/src/provider.ts

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -48,23 +48,24 @@ export interface DaffMagentoDriverConfig extends HttpOptions {
4848
* @param endpoint - The Magento store domain (e.g. "https://www.my-store.com/graphql") or an injection token for a string or function that returns a string
4949
*/
5050
export function provideMagentoDriver(options: DaffMagentoDriverConfig | InjectionToken<DaffMagentoDriverConfig>, ...features: Array<MagentoDriverFeature>): EnvironmentProviders {
51-
const opts: DaffMagentoDriverConfig = {
52-
possibleTypes: MAGENTO_POSSIBLE_TYPES.possibleTypes,
53-
typePolicies,
54-
...(options instanceof InjectionToken ? inject(options) : options),
55-
};
56-
const cache = new InMemoryCache({ typePolicies: opts.typePolicies, possibleTypes: opts.possibleTypes });
5751
const providers = [
5852
...features.flatMap(({ ɵproviders }) => ɵproviders),
59-
provideApollo(() => ({
60-
...inject(DAFF_DRIVER_MAGENTO_EXTRA_APOLLO_OPTIONS),
61-
link: from([
62-
...inject(DAFF_APOLLO_REQUEST_HANDLERS),
63-
onError(inject(DAFF_DRIVER_MAGENTO_ERROR_HANDLER)),
64-
inject(HttpLink).create(opts),
65-
]),
66-
cache,
67-
})),
53+
provideApollo(() => {
54+
const opts: DaffMagentoDriverConfig = {
55+
possibleTypes: MAGENTO_POSSIBLE_TYPES.possibleTypes,
56+
typePolicies,
57+
...(options instanceof InjectionToken ? inject(options) : options),
58+
};
59+
return {
60+
...inject(DAFF_DRIVER_MAGENTO_EXTRA_APOLLO_OPTIONS),
61+
link: from([
62+
...inject(DAFF_APOLLO_REQUEST_HANDLERS),
63+
onError(inject(DAFF_DRIVER_MAGENTO_ERROR_HANDLER)),
64+
inject(HttpLink).create(opts),
65+
]),
66+
cache: new InMemoryCache({ typePolicies: opts.typePolicies, possibleTypes: opts.possibleTypes }),
67+
};
68+
}),
6869
provideDaffDriverHttpClientCacheService(DaffDriverHttpClientCacheMagentoService),
6970

7071
// enable caching by default
@@ -73,7 +74,7 @@ export function provideMagentoDriver(options: DaffMagentoDriverConfig | Injectio
7374
];
7475

7576
if (features.find(({ ɵkind }) => ɵkind === MagentoDriverFeatureKind.TransferState)) {
76-
providers.push(provideDaffDriverMagentoTransferState(cache));
77+
providers.push(provideDaffDriverMagentoTransferState());
7778
}
7879

7980
return makeEnvironmentProviders(providers);

0 commit comments

Comments
 (0)