All Projects → lucasconstantino → Apollo Cache Invalidation

lucasconstantino / Apollo Cache Invalidation

Licence: mit
Experimental cache invalidation tools for Apollo.

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Apollo Cache Invalidation

Apollo Invalidation Policies
An extension of the Apollo 3 cache with support for type-based invalidation policies.
Stars: ✭ 55 (-16.67%)
Mutual labels:  graphql, apollo-client, cache
Githunt React
[DEPRECATED] 🔃 An example app frontend built with Apollo Client and React
Stars: ✭ 1,036 (+1469.7%)
Mutual labels:  graphql, apollo-client
Fullstack Tutorial
🚀 The Apollo platform tutorial app
Stars: ✭ 1,007 (+1425.76%)
Mutual labels:  graphql, apollo-client
Cynthesize Frontend
Frontend written in Angular 7 and deployed GraphQL for Cynthesize. Development build: https://cynthesize-develop.netlify.com
Stars: ✭ 65 (-1.52%)
Mutual labels:  graphql, apollo-client
React Boilerplate
⚛ The stable base upon which we build our React projects at Mirego.
Stars: ✭ 39 (-40.91%)
Mutual labels:  graphql, apollo-client
Crypto Grommet
Crypto and equities app
Stars: ✭ 39 (-40.91%)
Mutual labels:  graphql, apollo-client
Github Graphql React
A Github client built with React / GraphQL 💻👩‍💻💽 👨‍💻
Stars: ✭ 60 (-9.09%)
Mutual labels:  graphql, apollo-client
Apollo Link Maxage
An Apollo Link to invalidate cached queries
Stars: ✭ 23 (-65.15%)
Mutual labels:  graphql, apollo-client
A Pop
🎶 HD Music Streaming and Sharing Web App
Stars: ✭ 55 (-16.67%)
Mutual labels:  graphql, apollo-client
Blaze Apollo
Blaze integration for the Apollo Client
Stars: ✭ 56 (-15.15%)
Mutual labels:  graphql, apollo-client
Starter
Opinionated SaaS quick-start with pre-built user account and organization system for full-stack application development in React, Node.js, GraphQL and PostgreSQL. Powered by PostGraphile, TypeScript, Apollo Client, Graphile Worker, Graphile Migrate, GraphQL Code Generator, Ant Design and Next.js
Stars: ✭ 1,082 (+1539.39%)
Mutual labels:  graphql, apollo-client
Link state demo
🚀 Demonstrate how to support multiple stores in Apollo Link State
Stars: ✭ 30 (-54.55%)
Mutual labels:  graphql, apollo-client
Create Social Network
An educational project, demonstrating how to build a large scalable project with Javascript.
Stars: ✭ 853 (+1192.42%)
Mutual labels:  graphql, apollo-client
Persistgraphql Webpack Plugin
PersistGraphQL Webpack Plugin
Stars: ✭ 39 (-40.91%)
Mutual labels:  graphql, apollo-client
Kikstart Graphql Client
🚀 Small NodeJS Wrapper around apollo-client that provides easy access to running queries, mutations and subscriptions.
Stars: ✭ 27 (-59.09%)
Mutual labels:  graphql, apollo-client
Apollo Angular
A fully-featured, production ready caching GraphQL client for Angular and every GraphQL server 🎁
Stars: ✭ 1,058 (+1503.03%)
Mutual labels:  graphql, apollo-client
Offix
GraphQL Offline Client and Server
Stars: ✭ 694 (+951.52%)
Mutual labels:  graphql, cache
Dataloader
Implementation of Facebook's DataLoader in Golang
Stars: ✭ 703 (+965.15%)
Mutual labels:  graphql, cache
Guide To Graphql
A Frontend Developer's Guide to GraphQL (Fluent Conf 2018)
Stars: ✭ 59 (-10.61%)
Mutual labels:  graphql, apollo-client
Graphql Apollo Next.js
A server-rendering GraphQL client with Apollo Client and Next.js
Stars: ✭ 59 (-10.61%)
Mutual labels:  graphql, apollo-client

Apollo Cache Invalidation

A library to simplify cache invalidation for Apollo clients.

Build status sponsored by Taller

Installation

yarn add apollo-cache-invalidation

Or npm install --save apollo-cache-invalidation, if you are still in the old days.

Motivation

Cache control - and most of invalidation - is still a discussing issue for the Apollo Client team and the community involved. While participating in one of those issues, I've proposed a way to do field-based cache invalidation. This projects aims to fulfil this need, while something like this isn't implemented in core.

How does it work

This project exposes invalidateFields: a generator for a mutation update function implementation specialized in invalidating cache based on field paths.

In some cases after a mutation you want to invalidate cache on other queries that might have become outdated, but you can't really update their results from the data provided by the mutation. The refetchQueries is often the tool of choice, but it allows no deep field invalidation, meaning you'll have to invalidate the exact and very specific performed queries. invalidateFields is an alternative.

Usage

import { invalidateFields, ROOT } from 'apollo-cache-invalidation'
import gql from 'graphql-tag'

import { client } from './client' // Apollo Client instance.

const mutation = gql`
  mutation MakeUserHappy($user: ID!) {
    makeUserHappy(user: $user) {
      id
    }
  }
`

// Invalidate happyPeople field on the Root Query. Force it to run again.
const update = invalidateFields((proxy, result) => [
  [ROOT, 'happyPeople']
])

client.mutate({ mutation, update, variables: { user: 1 } })

The function provided to invalidateFields will receive a DataProxy instance and the result for the mutation as arguments. It must then return an array of field paths to invalidate. Each field path consist of an array of keys. Each key can be one of:

  • String: the key to invalidate;
  • RegExp: regex to match keys to invalidate;
  • Function: custom matching function to match keys to invalidate.

Each path will be compared individually to the whole cached data, invalidating any matched fields (possibly multiple) along the way.

The first key in a field path will test against either an object id (as resolved by the dataIdFromObject Apollo client config) or the ROOT_QUERY special key. In that case, you can provide the string 'ROOT_QUERY', or better, use the exported ROOT constant, as shown above.

Regex matching sample

Imagine you wan't to invalidate field happy for every user after a given mutation. Having a dataIdFromObject as such:

// Concatenate "__typename" and "id" field values to find identification.
// Do not uniquely identify resource if one of the fields is not provided
// (will use queried field name and variables, by default).
const dataIdFromObject = ({ __typename, id }) => {
  if (__typename && id) return __typename + id
  return null
}

you can invalidate a given field on all User type cached object with the following:

const update = invalidateFields(() => [[/^User[0-9]+$/, 'happy']])

client.mutate({ mutation, update })

Function matching

Similar to the Regex matching, you can do any customized field matching as so:

const randomKeyMatch = key => Math.random() >= 0.5

const update = invalidateFields(() => [
  [randomKeyMatch, 'happy']
])

client.mutate({ mutation, update })

This package should be temporary

I believe something similar to what is accomplished by this package should be soon added to the Apollo Client core. If someday that happens, this package will either be deprecated or hold other experimental functionality on the subject of caching and invalidation.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].