-
-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(driver): store and set Magento cache ID header (#2743)
- Loading branch information
Showing
3 changed files
with
131 additions
and
0 deletions.
There are no files selected for viewing
90 changes: 90 additions & 0 deletions
90
libs/driver/magento/src/graphql/cache-header/cache-header-apollo-link.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
import { TestBed } from '@angular/core/testing'; | ||
import { | ||
ApolloLink, | ||
gql, | ||
NextLink, | ||
Observable, | ||
Operation, | ||
} from '@apollo/client/core'; | ||
import { Apollo } from 'apollo-angular'; | ||
import { | ||
ApolloTestingController, | ||
ApolloTestingModule, | ||
} from 'apollo-angular/testing'; | ||
|
||
import { | ||
DaffMagentoCacheHeaderApolloLinkGenerator, | ||
MAGENTO_CUSTOMER_CACHE_ID_HEADER, | ||
} from './cache-header-apollo-link'; | ||
|
||
describe('@daffodil/driver/magento | DaffMagentoCacheHeaderApolloLinkGenerator', () => { | ||
let service: DaffMagentoCacheHeaderApolloLinkGenerator; | ||
let operation: Operation; | ||
let apollo: Apollo; | ||
let controller: ApolloTestingController; | ||
let link: ApolloLink; | ||
let linkSpy: jasmine.Spy<NextLink>; | ||
let cacheId: string; | ||
const query = gql`{ Operation(test: string) { name }}`; | ||
|
||
beforeEach(() => { | ||
TestBed.configureTestingModule({ | ||
imports: [ | ||
ApolloTestingModule, | ||
], | ||
}); | ||
|
||
controller = TestBed.inject(ApolloTestingController); | ||
apollo = TestBed.inject(Apollo); | ||
service = TestBed.inject(DaffMagentoCacheHeaderApolloLinkGenerator); | ||
|
||
link = service.getLink(); | ||
linkSpy = jasmine.createSpy(); | ||
linkSpy.and.returnValue(Observable.of()); | ||
cacheId = 'cacheId'; | ||
}); | ||
|
||
describe('getLink', () => { | ||
it('should not set the cache header when it was not included with a previous response', () => { | ||
apollo.query({ query }).subscribe(); | ||
operation = controller.expectOne(query).operation; | ||
link.request(operation, linkSpy); | ||
|
||
expect(operation.getContext().headers?.[MAGENTO_CUSTOMER_CACHE_ID_HEADER]).toBeFalsy(); | ||
}); | ||
|
||
it('should set the cache header when it was included with a previous response', () => { | ||
apollo.query({ query }).subscribe(); | ||
operation = controller.expectOne(query).operation; | ||
operation.setContext({ | ||
headers: { | ||
[MAGENTO_CUSTOMER_CACHE_ID_HEADER]: cacheId, | ||
}, | ||
}); | ||
link.request(operation, linkSpy); | ||
|
||
expect(operation.getContext().headers?.[MAGENTO_CUSTOMER_CACHE_ID_HEADER]).toEqual(cacheId); | ||
}); | ||
|
||
it('should set the cache header to the new value when the cache ID changes', () => { | ||
const newCacheId = 'newCacheId'; | ||
apollo.query({ query }).subscribe(); | ||
operation = controller.expectOne(query).operation; | ||
operation.setContext({ | ||
headers: { | ||
[MAGENTO_CUSTOMER_CACHE_ID_HEADER]: cacheId, | ||
}, | ||
}); | ||
link.request(operation, linkSpy); | ||
|
||
operation.setContext({ | ||
headers: { | ||
[MAGENTO_CUSTOMER_CACHE_ID_HEADER]: newCacheId, | ||
}, | ||
}); | ||
link.request(operation, linkSpy); | ||
|
||
expect(operation.getContext().headers?.[MAGENTO_CUSTOMER_CACHE_ID_HEADER]).toEqual(newCacheId); | ||
}); | ||
}); | ||
}); |
40 changes: 40 additions & 0 deletions
40
libs/driver/magento/src/graphql/cache-header/cache-header-apollo-link.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { Injectable } from '@angular/core'; | ||
import { ApolloLink } from '@apollo/client/core'; | ||
|
||
import { DaffApolloLinkGenerator } from '@daffodil/core/graphql'; | ||
|
||
export const MAGENTO_CUSTOMER_CACHE_ID_HEADER = 'X-Magento-Cache-Id'; | ||
|
||
/** | ||
* Stores and sets the Magento cache ID header. | ||
* This will set the `X-Magento-Cache-Id` header from the most recent value encountered | ||
* on a response, if there was a response with that header set. | ||
* | ||
* @inheritdoc | ||
*/ | ||
@Injectable({ | ||
providedIn: 'root', | ||
}) | ||
export class DaffMagentoCacheHeaderApolloLinkGenerator implements DaffApolloLinkGenerator { | ||
private _cacheHeader?: string; | ||
|
||
getLink(): ApolloLink { | ||
return new ApolloLink((operation, forward) => { | ||
if (this._cacheHeader) { | ||
operation.setContext({ | ||
headers: { | ||
...operation.getContext().headers, | ||
[MAGENTO_CUSTOMER_CACHE_ID_HEADER]: this._cacheHeader, | ||
}, | ||
}); | ||
} | ||
return forward(operation).map((response) => { | ||
const { headers } = operation.getContext().response; | ||
if (headers.get(MAGENTO_CUSTOMER_CACHE_ID_HEADER)) { | ||
this._cacheHeader = headers.get(MAGENTO_CUSTOMER_CACHE_ID_HEADER); | ||
} | ||
return response; | ||
}); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters