How to invalidate 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"
    }
  }
}

Here, the Automatic Cache Invalidation cannot 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. Invalidate all responses that contain Todos, even if they haven't been changed by calling purgeTodo without any arguments.

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

    mutation {
      _purgeQuery(queries: [todos])
    }
    
  3. 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"])
    }
    
  4. Determining one of the Todos that was previously on the list and purging that specifically

    mutation {
      purgeTodo(id: "idOfLatestTodoAlreadyInTheList")
    }
    

Did this page help you?