Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
}
}`,
},
],
},
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;
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
})