Build and Deploy a GraphQL API to the Edge with Fauna — Part 9

Build and Deploy a GraphQL API to the Edge with Fauna — Part 9

Enable edge caching to reduce database calls

Grafbase provides support for edge caching, a feature that enhances performance by serving already cached data, thereby eliminating the need to wait for a response from the database.

Inside grafbase/grafbase.config.ts we will update the config export to include a new property: cache.

export default config({
  schema: g,
  cache: {
    rules: [
      {
        maxAge: 60,
        types: [{ name: 'Query', fields: ['products', 'product'] }],
        mutationInvalidation: 'entity'
      }
    ]
  }
})

This configuration instructs Grafbase to set a maxAge to 60 seconds on the queries products and product.

The mutationInvalidation property also tells Grafbase to delete any cached data the entities returned tagged with their id value. This means if a Product is mutated and id matches any cached data tag, the data will be invalidated

💡
Caching is a production feature and will not work using the CLI.

Continue to Part 10 👉