Build and Deploy a GraphQL API to the Edge with MySQL and PlanetScale โ€” Part 10

Jamie BartonJamie Barton
1 min read

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.

Within our exported config and schema, we introduce an additional key, cache. This cache object includes two queries โ€” products and product, for which we want to enable caching. It also defines maxAge and outlines how cache invalidations should be handled.

We've chosen the entity invalidation option, which implies that id of a Product returned by both queries will serve as tags in the cached responses. Therefore, if a Product is mutated and id matches any cached data tag, the data will be invalidated.

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

With this configuration, we are leveraging Grafbase's edge caching to improve the efficiency of our API operations.

๐Ÿ’ก
Caching is a production feature and will not work using the CLI.

๐Ÿ‘‰ Continue to Part 11

0
Subscribe to my newsletter

Read articles from Jamie Barton directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Jamie Barton
Jamie Barton