Skip to content
This repository has been archived by the owner on May 26, 2023. It is now read-only.

Cache-Control header is not correctly overridden for 0 value #11

Closed
fabsrc opened this issue Jan 18, 2019 · 0 comments
Closed

Cache-Control header is not correctly overridden for 0 value #11

fabsrc opened this issue Jan 18, 2019 · 0 comments

Comments

@fabsrc
Copy link

fabsrc commented Jan 18, 2019

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:

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant