Skip to content

Commit

Permalink
Fix cache policy calculation when a hint has maxAge 0 (#2197)
Browse files Browse the repository at this point in the history
  • Loading branch information
fabsrc authored and glasser committed Jan 22, 2019
1 parent e0c3ae4 commit 06e4907
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
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,
};
const responseSubSubPath: ResponsePath = {
key: 'subSubTest',
prev: responseSubPath,
};

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 });
cacheControlExtension.addHint(responseSubSubPath, { maxAge: 20 });

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);
});
});
});
9 changes: 5 additions & 4 deletions packages/apollo-cache-control/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,10 +156,11 @@ export class CacheControlExtension<TContext = any>
let scope: CacheScope = CacheScope.Public;

for (const hint of this.hints.values()) {
if (hint.maxAge) {
lowestMaxAge = lowestMaxAge
? Math.min(lowestMaxAge, hint.maxAge)
: hint.maxAge;
if (hint.maxAge !== undefined) {
lowestMaxAge =
lowestMaxAge !== undefined
? Math.min(lowestMaxAge, hint.maxAge)
: hint.maxAge;
}
if (hint.scope === CacheScope.Private) {
scope = CacheScope.Private;
Expand Down

0 comments on commit 06e4907

Please sign in to comment.