npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@pi-top/apollo-cache-updaters

v1.5.0

Published

<div align="center"> <h1>apollo-cache-updaters</h1>

Downloads

7

Readme

Build Status version MIT License semantic-release PRs Welcome Code of Conduct

The problem

Apollo cache updates are verbose and require an understanding of how the cache uses references and normalises data internally.

This solution

A set of declarative updaters that perform common actions automatically.

Restructuring updaters to be declarative makes cache updates more concise. This makes it possible to write complex updates inline.

Common patterns in update methods are done automatically, for instance writeFragment automatically generates it's fragment from data. All automatic behaviours are easy to disable.

Table of Contents

Installation

This module is distributed via npm which is bundled with node and should be installed as one of your project's dependencies:

npm install --save @pi-top/apollo-cache-updaters

The @apollo/client package is a peerDepencency.

Usage

This package exports updaters for the cache actions: evict, modify, writeFragment and writeQuery. It also exports a helper called combine (see below).

Updaters

Each updater accepts a createOptions callback that is called with the ExecutionResult and the ApolloCache as the first and second arguments respectively. The createOptions callback is used to create options for a cache method, for example writeQuery's createOptions callback should return the same options that would be passed cache.writeQuery:

import { writeQuery } from '@pi-top/apollo-cache-updaters';

const update = writeQuery((result) => ({
  data: result.data,
  query: gql`
    query GetStuff {
      stuff {
        id
        __typename
      }
    }
  `,
})),

createOptions can also return an array of options, for when multiple similar operations need to performed from a single ExecutionResult:

import { writeQuery } from '@pi-top/apollo-cache-updaters';

const update = writeQuery((result) => result.data.stuffs.map(stuff => ({
  data: { stuff },
  variables: { id: stuff.id }
  query: gql`
    query GetStuff($id: string) {
      stuff(id: $id) {
        id
        __typename
      }
    }
  `,
}))

writeFragment

writeFragment automatically generates it's fragment from the data property when fragment is undefined. Note that when using this behaviour the __typename property is required to exist in data:

import { writeFragment } from '@pi-top/apollo-cache-updaters';

const update = writeFragment((result) => ({
  data: {
    ...thing,
    __typename: 'Thing',
    stuff: result.data.stuff
  }
}))

evict

evict automatically calls cache.gc if there was a successful eviction. To stop the behaviour there is an option gc that can be set to false

import { evict } from '@pi-top/apollo-cache-updaters';

const update = evict((result, cache) => ({
  id: cache.identify(result.data),
  gc: false,
}))

combine

When multiple types of operation need to performed for one ExecutionResult the combine method can be used:

import { combine } from '@pi-top/apollo-cache-updaters';

const update = combine(
  writeFragment((result) => ({
    data: {
      ...thing,
      stuff: result.data.stuff
    }
  })),
  writeQuery((result) => ({
    data: result.data,
    query: GET_STUFF,
  })),
)

skipping updates

Sometimes we want to conditionally update the cache, to do this we can set the skip value in the updater options:

import { evict } from '@pi-top/apollo-cache-updaters'

const update = evict((result, cache) => ({
  id: cache.identify(objectToDelete),
  skip: !(result.data && result.data.deleted),
}));

Typescript

The project comes fully typed.

LICENSE

MIT