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

nestjs-gql-cache-control

v0.1.2

Published

This package offers you a simple decorator to set cache control on your resolvers.

Downloads

6,140

Readme

GraphQL Cache Control

This package offers you a simple decorator to set cache control on your resolvers.

Installation

On Yarn:

yarn add nestjs-gql-cache-control

On NPM:

npm install nestjs-gql-cache-control

Usage

To use caching, you are gonna need these packages too:

@nestjs/graphql
apollo-server-core
apollo-server-plugin-response-cache

First, register graphql module and cache plugins in your app module:

import responseCachePlugin from 'apollo-server-plugin-response-cache';
import { ApolloServerPluginCacheControl } from 'apollo-server-core/dist/plugin/cacheControl';

GraphQLModule.forRoot({
  // ...
  plugins: [
    ApolloServerPluginCacheControl({ defaultMaxAge: 5 }), // optional
    responseCachePlugin(),
  ],
}),

To add Redis or other caching stores, check Apollo's docs

Then, you can use the decorator on your queries and field resolvers:

import { CacheControl } from 'nestjs-gql-cache-control';

@Resolver((type) => Post)
export class PostResolver {
  @Query(() => [Post])
  @CacheControl({ maxAge: 10 })
  posts() {
    // database calls
    return posts;
  }

  @ResolveField(() => User)
  @CacheControl({ inheritMaxAge: true })
  owner() {
    // database calls
    return owner;
  }

  @ResolveField(() => boolean)
  @CacheControl({ maxAge: 5, scope: 'PRIVATE' })
  hasLiked() {
    // database calls
    return hasLiked;
  }
}

How The Apollo Cache Works

Please carefully read Apollo's docs about caching to understand how caching works, since it has a set of rules for cache calculation. In a brief:

a response should only be considered cacheable if every part of that response opts in to being cacheable. At the same time, we don't think developers should have to specify cache hints for every single field in their schema. So, we follow these heuristics: Root field resolvers are extremely likely to fetch data (because these fields have no parent), so we set their default maxAge to 0 to avoid automatically caching data that shouldn't be cached. Resolvers for other non-scalar fields (objects, interfaces, and unions) also commonly fetch data because they contain arbitrarily many fields. Consequently, we also set their default maxAge to 0. Resolvers for scalar, non-root fields rarely fetch data and instead usually populate data via the parent argument. Consequently, these fields inherit their default maxAge from their parent to reduce schema clutter.

Connections (Pagination)

If you happen to use nestjs-gql-connections, edges and node will automatically inherit cache control from their parents. but otherwise you should set inheritMaxAge on your connection fields to prevent connections from cancelling your cache.

Why you should do that? because you probably don't want your connections to cancel your cache control. (learn more)