Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix cache policy calculation #2197

Merged
merged 2 commits into from Jan 22, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -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);
});
});
});
2 changes: 1 addition & 1 deletion packages/apollo-cache-control/src/index.ts
Expand Up @@ -156,7 +156,7 @@ export class CacheControlExtension<TContext = any>
let scope: CacheScope = CacheScope.Public;

for (const hint of this.hints.values()) {
if (hint.maxAge) {
if (hint.maxAge !== undefined) {
lowestMaxAge = lowestMaxAge
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should the check here (before the ?) also be against undefined?

Copy link
Contributor Author

@fabsrc fabsrc Jan 22, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, you are right. Didn't check that case in the tests. 😕
Just fixed it and adjusted the test.

? Math.min(lowestMaxAge, hint.maxAge)
: hint.maxAge;
Expand Down