From 7d84fc8caa8d0d88d9a5c8e015cc532172c43ad7 Mon Sep 17 00:00:00 2001 From: Fabian Schneider Date: Sat, 19 Jan 2019 00:44:13 +0100 Subject: [PATCH 1/2] Fix cache policy calculation --- .../__tests__/cacheControlExtension.test.ts | 63 +++++++++++++++++++ packages/apollo-cache-control/src/index.ts | 2 +- 2 files changed, 64 insertions(+), 1 deletion(-) create mode 100644 packages/apollo-cache-control/src/__tests__/cacheControlExtension.test.ts diff --git a/packages/apollo-cache-control/src/__tests__/cacheControlExtension.test.ts b/packages/apollo-cache-control/src/__tests__/cacheControlExtension.test.ts new file mode 100644 index 00000000000..957c8c8bab9 --- /dev/null +++ b/packages/apollo-cache-control/src/__tests__/cacheControlExtension.test.ts @@ -0,0 +1,63 @@ +import { ResponsePath } from 'graphql'; +import { CacheControlExtension, CacheScope } from '../'; + +describe('CacheControlExtension', () => { + let cacheControlExtension: CacheControlExtension; + + beforeEach(() => { + cacheControlExtension = new CacheControlExtension(); + }); + + describe('computeOverallCachePolicy', () => { + const responsePath: ResponsePath = { + key: 'test', + prev: undefined, + }; + const responseSubPath: ResponsePath = { + key: 'subTest', + prev: responsePath, + }; + + it('returns undefined without cache hints', () => { + const cachePolicy = cacheControlExtension.computeOverallCachePolicy(); + expect(cachePolicy).toBeUndefined(); + }); + + it('returns lowest max age value', () => { + cacheControlExtension.addHint(responsePath, { maxAge: 10 }); + cacheControlExtension.addHint(responseSubPath, { maxAge: 20 }); + + const cachePolicy = cacheControlExtension.computeOverallCachePolicy(); + expect(cachePolicy).toHaveProperty('maxAge', 10); + }); + + it('returns undefined if any cache hint has a maxAge of 0', () => { + cacheControlExtension.addHint(responsePath, { maxAge: 120 }); + cacheControlExtension.addHint(responseSubPath, { maxAge: 0 }); + + const cachePolicy = cacheControlExtension.computeOverallCachePolicy(); + expect(cachePolicy).toBeUndefined(); + }); + + it('returns PUBLIC scope by default', () => { + cacheControlExtension.addHint(responsePath, { maxAge: 10 }); + + const cachePolicy = cacheControlExtension.computeOverallCachePolicy(); + expect(cachePolicy).toHaveProperty('scope', CacheScope.Public); + }); + + it('returns PRIVATE scope if any cache hint has PRIVATE scope', () => { + cacheControlExtension.addHint(responsePath, { + maxAge: 10, + scope: CacheScope.Public, + }); + cacheControlExtension.addHint(responseSubPath, { + maxAge: 10, + scope: CacheScope.Private, + }); + + const cachePolicy = cacheControlExtension.computeOverallCachePolicy(); + expect(cachePolicy).toHaveProperty('scope', CacheScope.Private); + }); + }); +}); diff --git a/packages/apollo-cache-control/src/index.ts b/packages/apollo-cache-control/src/index.ts index db145f7a15f..f6708e5ab51 100644 --- a/packages/apollo-cache-control/src/index.ts +++ b/packages/apollo-cache-control/src/index.ts @@ -156,7 +156,7 @@ export class CacheControlExtension let scope: CacheScope = CacheScope.Public; for (const hint of this.hints.values()) { - if (hint.maxAge) { + if (hint.maxAge !== undefined) { lowestMaxAge = lowestMaxAge ? Math.min(lowestMaxAge, hint.maxAge) : hint.maxAge; From 4312fe4c50792d2bcc004c0ada0099d76b8454dd Mon Sep 17 00:00:00 2001 From: Fabian Schneider Date: Tue, 22 Jan 2019 08:30:17 +0100 Subject: [PATCH 2/2] Fix cache policy minimum calculation --- .../src/__tests__/cacheControlExtension.test.ts | 5 +++++ packages/apollo-cache-control/src/index.ts | 7 ++++--- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/packages/apollo-cache-control/src/__tests__/cacheControlExtension.test.ts b/packages/apollo-cache-control/src/__tests__/cacheControlExtension.test.ts index 957c8c8bab9..127071f55e9 100644 --- a/packages/apollo-cache-control/src/__tests__/cacheControlExtension.test.ts +++ b/packages/apollo-cache-control/src/__tests__/cacheControlExtension.test.ts @@ -17,6 +17,10 @@ describe('CacheControlExtension', () => { key: 'subTest', prev: responsePath, }; + const responseSubSubPath: ResponsePath = { + key: 'subSubTest', + prev: responseSubPath, + }; it('returns undefined without cache hints', () => { const cachePolicy = cacheControlExtension.computeOverallCachePolicy(); @@ -34,6 +38,7 @@ describe('CacheControlExtension', () => { it('returns undefined if any cache hint has a maxAge of 0', () => { cacheControlExtension.addHint(responsePath, { maxAge: 120 }); cacheControlExtension.addHint(responseSubPath, { maxAge: 0 }); + cacheControlExtension.addHint(responseSubSubPath, { maxAge: 20 }); const cachePolicy = cacheControlExtension.computeOverallCachePolicy(); expect(cachePolicy).toBeUndefined(); diff --git a/packages/apollo-cache-control/src/index.ts b/packages/apollo-cache-control/src/index.ts index f6708e5ab51..80d8cd23913 100644 --- a/packages/apollo-cache-control/src/index.ts +++ b/packages/apollo-cache-control/src/index.ts @@ -157,9 +157,10 @@ export class CacheControlExtension for (const hint of this.hints.values()) { if (hint.maxAge !== undefined) { - lowestMaxAge = lowestMaxAge - ? Math.min(lowestMaxAge, hint.maxAge) - : hint.maxAge; + lowestMaxAge = + lowestMaxAge !== undefined + ? Math.min(lowestMaxAge, hint.maxAge) + : hint.maxAge; } if (hint.scope === CacheScope.Private) { scope = CacheScope.Private;