How to use apollo-server-cache-redis - 2 common examples

To help you get started, we’ve selected a few apollo-server-cache-redis examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github withspectrum / spectrum / api / apollo-server.js View on Github external
}
}`,
      },
    ],
  },
  introspection: process.env.NODE_ENV !== 'production',
  maxFileSize: 25 * 1024 * 1024, // 25MB
  engine: false,
  tracing: false,
  validationRules: [depthLimit(10)],
  cacheControl: {
    calculateHttpHeaders: false,
    // Cache everything for at least a minute since we only cache public responses
    defaultMaxAge: 60,
  },
  cache: new RedisCache({
    ...config,
    prefix: 'apollo-cache:',
  }),
  plugins: [
    responseCachePlugin({
      sessionId: ({ context }) => (context.user ? context.user.id : null),
      // Only cache public responses
      shouldReadFromCache: ({ context }) => !context.user,
      shouldWriteToCache: ({ context }) => !context.user,
    }),
  ],
});

export default server;
github shfshanyue / apollo-server-starter / index.ts View on Github external
import Koa from 'koa'
import bodyParser from 'koa-bodyparser'
import _ from 'lodash'
import responseCachePlugin from 'apollo-server-plugin-response-cache'
import { RedisCache } from 'apollo-server-cache-redis'
import { formatError, Exception, responseLogger, queryLogger, session, redis } from './lib'
import { typeDefs, resolvers } from './src'
import directives from './src/directives'
import * as utils from './src/utils'
import sequelize, { models, contextOption } from './db'
import config from './config'
import { AppContext, KoaContext } from './type'
import { auth, context } from './middlewares'
import httpStatusPlugin from './src/plugin/httpStatus'

const cache = new RedisCache({
  host: config.redis.host,
  password: config.redis.password
})

const server = new ApolloServer({
  typeDefs,
  resolvers,
  context ({ ctx }: { ctx: KoaContext }): AppContext {
    const body = ctx.request.body || {}
    queryLogger.info(body.query, {
      operationName: body.operationName,
      query: body.query,
      variables: body.variables,
      ip: ctx.request.ip,
      user: ctx.user
    })

apollo-server-cache-redis

[![npm version](https://badge.fury.io/js/apollo-server-cache-redis.svg)](https://badge.fury.io/js/apollo-server-cache-redis) [![Build Status](https://circleci.com/gh/apollographql/apollo-server/tree/main.svg?style=svg)](https://circleci.com/gh/apollograph

MIT
Latest version published 2 years ago

Package Health Score

67 / 100
Full package analysis

Popular apollo-server-cache-redis functions

Similar packages