When calculateHttpHeaders is set to true for each request the lowest max age should be caculated for the Cache-Control header. However this is not the case if the @cacheControl directive is set to 0 in the schema.
For example, if the defaultMaxAge is set to 100 and in the schema there is a @cacheControl(maxAge: 0) directive, the Cache-Control header should not be set. However with the current implementation it will be set to max-age=100, public.
The same is the case if defaultMaxAge is set to 0 and the @cacheControl directive of a field should override the one on type level. Here is an example implementation for this:
const { ApolloServer, gql } = require('apollo-server');
const books = [
{
title: 'Harry Potter and the Chamber of Secrets',
author: 'J.K. Rowling',
}
];
const typeDefs = gql`
type Book @cacheControl(maxAge: 120) {
title: String @cacheControl(maxAge: 0)
author: String
}
type Query {
books: [Book]
}
`;
const resolvers = {
Query: {
books: () => books,
},
};
const server = new ApolloServer({
typeDefs,
resolvers,
cacheControl: {
calculateHttpHeaders: true,
defaultMaxAge: 0,
stripFormattedExtensions: false
}
});
server.listen().then(({ url }) => console.log(`🚀 Server ready at ${url}`));
Query with: curl -sv 'http://localhost:4000/?query=%7Bbooks%7Btitle%7D%7D'
Expected: no cache-control header
Actual: cache-control: max-age=120, public
The bug seems to be caused by this conditional, which evaluates to false if the checked variable is 0:
When
calculateHttpHeadersis set totruefor each request the lowest max age should be caculated for theCache-Controlheader. However this is not the case if the@cacheControldirective is set to0in the schema.For example, if the
defaultMaxAgeis set to100and in the schema there is a@cacheControl(maxAge: 0)directive, theCache-Controlheader should not be set. However with the current implementation it will be set tomax-age=100, public.The same is the case if
defaultMaxAgeis set to0and the@cacheControldirective of a field should override the one on type level. Here is an example implementation for this:Query with:
curl -sv 'http://localhost:4000/?query=%7Bbooks%7Btitle%7D%7D'Expected: no
cache-controlheaderActual:
cache-control: max-age=120, publicThe bug seems to be caused by this conditional, which evaluates to
falseif the checked variable is0: