Skip to Content

Invalidating Lists

Let’s say you have a Todo type, a Query.todos query, and a Mutation.addTodo mutation in your GraphQL API

type Todo {
  id: ID!
  # ...
}
 
type Query {
  todos: [Todo!]!
  # ...
}
 
type Mutation {
  addTodo(...): Todo
}

When the addTodo mutation passes through Stellate, it returns a new todo.

{
  "data": {
    "addTodo": {
      "id": "5"
    }
  }
}

Depending on your setting for mutation policies the Automatic Cache Invalidation might not invalidate the Query.todos query automatically, as it doesn’t know which list that todo was added to.

You have multiple ways to ensure the stale cached query results are invalidated correctly by manually purging data via the Purging API from your backend:

  1. Setting your mutation policies to either List or even Type.

  2. Invalidate all responses that contain Todos, even if they haven’t been changed by calling purgeTodo without any arguments.

    mutation {
      _purgeTodo()
    }
  3. Purge all queries that request the todos field using the _purgeQuery mutation

    mutation {
      _purgeQuery(queries: [todos])
    }
  4. Add an operation name to your query (e.g. query getTodos { todos { ... } }) and purge any cached result of that query by its name

    mutation {
      _purgeOperationName(names: ["getTodos"])
    }
  5. Determining one of the Todos that was previously on the list and purging that specifically

    mutation {
      purgeTodo(id: "idOfLatestTodoAlreadyInTheList")
    }
Last updated on