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

@matters/apollo-response-cache

v2.0.0-alpha.0

Published

Caching and invalidation mechanisms (plugins, directives) of Apollo GraphQL

Downloads

211

Readme

apollo-response-cache

Publish Status Test Status Commitizen friendly

Caching and invalidation mechanisms (plugins, directives) of Apollo GraphQL, used by matters-server.

responseCachePlugin is forked from apollo-server-plugin-response-cache.

How it works?

Cache Mechanisms

On each query request,

  1. responseCachePlugin creates an empty key set, and injects it to the context.
  2. @logCache collects nodes on its field, then add to the key set.
  3. responseCachePlugin writes query response cache (fqc) and node-fqc key mapping to in-memory data store.

Once a mutation updates this node, @purgeCache will purge related fqc.

Usage

Note: there are breaking changes in API from 1.4.0 to 2.0.0, see below Breaking changes section for more info

Install package:

npm i @matters/apollo-response-cache

Add plugin and directives to the constructor:

import {
  logCacheDirective,
  purgeCacheDirective,
  responseCachePlugin,
} from '@matters/apollo-response-cache'

const {typeDef: logCacheDirectiveTypeDef, transformer: logCacheDirectiveTransformer} = logCacheDirective()
const {typeDef: purgeCacheDirectiveTypeDef, transformer: purgeCacheDirectiveTransformer} = purgeCacheDirective()

let schema = makeExecutableSchema({
  typeDefs: [yourTypeDef, logCacheDirectiveTypeDef, purgeCacheDirectiveTypeDef]
})

schema = logCacheDirectiveTransformer(
  purgeCacheDirectiveTransformer(schema)
)

const server = new ApolloServer({
  schema,
  plugins: [responseCachePlugin()],
})

Use in the schema:

type Query {
  article(id: ID!): Article! @logCache(type: "Article")
}

type Mutation {
  archiveArticle(id: ID!): Article! @purgeCache(type: "Article")
}

You can also purge cache in the resolver:

const schema = makeExecutableSchema({
  schemaDirectives: {
    purgeCache: PurgeCacheDirective({ extraNodesPath: '__invalid_nodes__' }),
  },
})

const resolvers = {
  Mutation: {
    archiveArticle: (parent, args, context) => {
      // ...
      article.__invalid_nodes__ = [
        {
          id: '2',
          type: 'Article',
        },
        {
          id: '3',
          type: 'Comment',
        },
      ]

      return article
    },
  },
}

Customize node type & id resolvers

You might want a custom function to resolve node's type and id since it may be a union or interface type.

const typeResolver = (type: string, result: any) => {
  if (['Node', 'Response'].indexOf(type) >= 0) {
    return result.__type
  }
  return type
}
const idResolver = (type: string, result: any) => {
  if (['Node', 'Response'].indexOf(type) >= 0) {
    return result.__unusual_id__
  }
  return result.id
}

const schema = makeExecutableSchema({
  schemaDirectives: {
    purgeCache: PurgeCacheDirective({ typeResolver, idResolver }),
  },
})

Breaking changes in 2.0.0

  1. Support apollo-server v4 now, but drop support for apollo-server v3 and graphql-tools v8 and below
  2. All APIs, including plugin option, directives, helpers interface, changed: a. pulgin constructor take redis client (type Redis from ioredis) instead of RedisCache from deprecated apollo-server-cache-redis b. invalidateFQC take redis instead of RedisCache c. directives api is totally changed to function way, as graphql-tools/utils v8 depreacated class base SchemaDirectiveVisitor

Known issues

If hit Types have separate declarations of a private property '__identity' when build apollo server codebase, make sure both direct dependency on @apollo/server and this package dependency on @apollo/server share totally same version.

TODOs

  • [x] responseCachePlugin
  • [x] @logCache
  • [x] @purgeCache
  • [ ] Unit Test