Add GraphQL Edge Caching to Strapi Cloud

Grafbase is the easiest way to extend and cache existing APIs with custom code that lives next to the user at the edge.

If you're using the Headless Content Management System (CMS) Strapi you might be looking for caching options to improve performance and experience for users by reducing API calls to the origin.

Grafbase makes it very easy to connect Strapi's GraphQL API to the Grafbase Edge Gateway and enable GraphQL Edge Caching. If you need to extend the Strapi API queries or add new fields to types, you can do that using your custom resolver code.

Get started locally

Let's begin by creating a new Grafbase project locally using the CLI:

npx grafbase init --template graphql-strapi

Next, open the file grafbase/grafbase.config.ts and make any adjustments. By default, Grafbase will:

  • Adds Strapi as a data source

  • Cache all queries for 60 seconds

  • Enable public access to the Grafbase Edge Gateway

  • Forward Authorization header to Strapi

import { g, connector, config } from '@grafbase/sdk'

const strapi = connector.GraphQL({
  url: g.env('STRAPI_API_URL'),
  headers: (headers) => {
    headers.set('Authorization', { forward: 'Authorization' })
  }
})

g.datasource(strapi)

export default config({
  schema: g,
  cache: {
    rules: [
      {
        types: ['Query'],
        maxAge: 60
      }
    ]
  },
  auth: {
    rules: (rules) => {
      rules.public()
    }
  }
})
💡
If you plan to add other data sources, you should use a namespace to prevent schema conflicts.

If you'd prefer not to pass the Authorization header with requests from the client, you can also set the Authorization to use an environment variable stored by Grafbase:

const strapi = connector.GraphQL({
  url: g.env('STRAPI_API_URL'),
  headers: (headers) => {
    headers.set('Authorization', g.env('STRAPI_API_KEY'))
  }
})

Now make sure to add your Strapi API URL (and optional API KEY) to grafbase/.env:

STRAPI_API_URL=

# Only if you set the Authorization header with a static value
# STRAPI_API_KEY=

Finally, run the Grafbase development server by using the command below:

npx grafbase dev

You now have a GraphQL API running locally that acts as a proxy to Strapi! 🎉

You can execute any GraphQL query or mutation you normally would with Strapi using the new endpoint: http://127.0.0.1:4000/graphql.

💡
Grafbase Pathfinder can be found at http://127.0.0.1:4000 where you can explore the Grafbase Edge Gateway API and schema.
💡
Make sure to commit the grafbase folder with the rest of your application.

Deploy to Production

You can continue to use the Grafbase CLI when building locally to proxy your Strapi API but you will need to deploy to production to take advantage of GraphQL Edge Caching.

Follow these steps to deploy to production:

  1. Signup for an account at http://grafbase.com

  2. Visit grafbase.com/new to create a new project

  3. Connect and deploy your application where the grafbase was added

  4. Make sure to add your STRAPI_API_URL (and optional STRAPI_API_KEY) when deploying

  5. Update your host (Netlify, Vercel, Fly, etc.) with the new GraphQL API endpoint that Grafbase supplied for your new project.

That's it!

Grafbase is programmed to autonomously deploy a fresh gateway each time it identifies a change to grafbase/grafbase.config.ts. Consequently, if you need to adjust any cache settings, including parameters like maxAge, staleWhileRevalidate, and mutationInvalidation, you’re free to do so.

Grafbase will handle the rest of the process seamlessly. We'll explore extending the Strapi API with custom fields in another post.

#GrafbaseHackathon