Cache purging

GraphCDN automatically creates a custom GraphQL API, the "Purging API", for your service that allows you to purge your cache. To find your purging API, head to the "Purging API" tab on your service's dashboard!

Automatic mutation purging

Mutations invalidate any cached query results that contain the object(s) you return fully automatically. For example:

type Post {
  id: ID!
  author: User!
  comments: [Comment!]!
}

type Mutation {
  updatePost: Post!
}

mutation {
  updatePost {
    id
    comments {
      id
    }
    author {
      id
    }
  }
}

Once the mutation completes, any query result that contains the Post with the returned ID and any query result that contains the User with the returned ID (at Post.authorand any query result that contains any of the comments with the returned IDs (at Post.comments[n].id) will fully automatically be invalidated.

Purging API

The url structure for the purging API is this:

https://admin.graphcdn.io/<service-name>

Authentication

To use your service's purging API you need to authenticate with a Purging API Token. To create a new one, go to your service's "Settings" tab and navigate to the "Purging API Tokens" section.

💡
You might already see some tokens in the list called something like "dashboard-74asd8" that you did not create. This is nothing to worry about. When you navigate to the "Purging API" tab on your service's dashboard we automatically create a new token so you can try it out without having to create one manually.

To send a request to the purging API you need to send the purging API token in the graphcdn-token header.

For example, here is how to access the API from Node.js:

const fetch = require('node-fetch')

async function purgeAllPosts() {
  await fetch('https://admin.graphcdn.io/little-frog', {
    method: 'POST', // Always POST purge mutations
    headers: {
      'Content-Type': 'application/json', // and specify the Content-Type
      'graphcdn-token': 'das09dja09sjda0s9ja...'
    },
    body: JSON.stringify({ query: `mutation { purgePost }` })
  })
}

purgeAllPosts().catch(e => console.error(e))

If you want to try it in the GraphQL playground use the "HTTP Headers" tab at the bottom:

notion image

The purging API gives you access to fine-grained purging mutations that you can use to invalidate any data that has changed right from your backend.

Note that it's preferable to purge as fine-grained as possible. The less data you invalidate the higher your cache hit rate will be.

Purge all occurrences of a specific object

The most specific purge mutation possible. Use this unless you have a reason not to!

You can purge all queries that contain a specific object/node, for example the User with the id 5:

mutation {
  purgeUser(id: [5])
}

Of course, since this is GraphQL, you can purge as many specific objects/nodes as you want at once:

mutation {
  purgeUser(id: [1, 2, 5, 7])
  purgePost(id: [12, 13])
}

Purging by query name

With this mutation, you can purge all cached results of the query with the name(s) you provide. For example, to invalidate all listUsers and listPosts query results:

mutation {
  _purgeQuery(queries: [listUsers, listPosts])
}
💡
Note that this mutation has an underscore in front of it as we don't want this generic name to conflict with your specific types.

Purging all occurrences of a type

You can purge all query results that contain any instance of an object. For example, to purge all cached results of queries that have any user in them, simply purge all users by not passing the id argument:

mutation {
  purgeUser
}

Purging everything (advanced)

⚠️
BE CAREFUL: Purging all queries will temporarily increase traffic to your origin server while we don't have anything cached anymore. Use this sparingly as a last resort and make sure your origin servers can handle the resulting traffic spike.

You can purge all cached results GraphCDN has stored with the special _purgeAll mutation. Be extra careful when using this, as it will cause a traffic spike to your origin server.