Build and Deploy a GraphQL API to the Edge with MySQL and PlanetScale — Part 10

Build and Deploy a GraphQL API to the Edge with MySQL and PlanetScale — Part 10

Enable edge caching to reduce database round trips

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