11// @flow
2+ const debug = require ( 'debug' ) ( 'api:graphql' ) ;
23import { ApolloServer } from 'apollo-server-express' ;
4+ import responseCachePlugin from 'apollo-server-plugin-response-cache' ;
35import depthLimit from 'graphql-depth-limit' ;
46import costAnalysis from 'graphql-cost-analysis' ;
7+ import { RedisCache } from 'apollo-server-cache-redis' ;
8+ import { config } from 'shared/cache/redis' ;
59import createLoaders from './loaders' ;
610import createErrorFormatter from './utils/create-graphql-error-formatter' ;
711import schema from './schema' ;
812import { setUserOnline } from 'shared/db/queries/user' ;
13+ import { statsd } from 'shared/statsd' ;
914import { getUserIdFromReq } from './utils/session-store' ;
1015import UserError from './utils/UserError' ;
1116import type { DBUser } from 'shared/types' ;
@@ -55,6 +60,7 @@ const server = new ProtectedApolloServer({
5560 req . statsdTags = {
5661 graphqlOperationName : req . body . operationName || 'unknown_operation' ,
5762 } ;
63+ debug ( req . body . operationName || 'unknown_operation' ) ;
5864 const loaders = createLoaders ( ) ;
5965 let currentUser = req . user && ! req . user . bannedAt ? req . user : null ;
6066
@@ -70,20 +76,17 @@ const server = new ProtectedApolloServer({
7076 } ,
7177 subscriptions : {
7278 path : '/websocket' ,
73- onOperation : ( _ : any , params : Object ) => {
74- const errorFormatter = createErrorFormatter ( ) ;
75- params . formatError = errorFormatter ;
76- return params ;
77- } ,
7879 onDisconnect : rawSocket => {
80+ statsd . increment ( 'websocket.connections' , - 1 ) ;
7981 return getUserIdFromReq ( rawSocket . upgradeReq )
8082 . then ( id => id && setUserOnline ( id , false ) )
8183 . catch ( err => {
8284 console . error ( err ) ;
8385 } ) ;
8486 } ,
85- onConnect : ( connectionParams , rawSocket ) =>
86- getUserIdFromReq ( rawSocket . upgradeReq )
87+ onConnect : ( connectionParams , rawSocket ) => {
88+ statsd . increment ( 'websocket.connections' , 1 ) ;
89+ return getUserIdFromReq ( rawSocket . upgradeReq )
8790 . then ( id => ( id ? setUserOnline ( id , true ) : null ) )
8891 . then ( user => {
8992 return {
@@ -96,7 +99,8 @@ const server = new ProtectedApolloServer({
9699 return {
97100 loaders : createLoaders ( { cache : false } ) ,
98101 } ;
99- } ) ,
102+ } ) ;
103+ } ,
100104 } ,
101105 playground : process . env . NODE_ENV !== 'production' && {
102106 settings : {
@@ -118,8 +122,24 @@ const server = new ProtectedApolloServer({
118122 maxFileSize : 25 * 1024 * 1024 , // 25MB
119123 engine : false ,
120124 tracing : false ,
121- cacheControl : false ,
122125 validationRules : [ depthLimit ( 10 ) ] ,
126+ cacheControl : {
127+ calculateHttpHeaders : false ,
128+ // Cache everything for at least a minute since we only cache public responses
129+ defaultMaxAge : 60 ,
130+ } ,
131+ cache : new RedisCache ( {
132+ ...config ,
133+ prefix : 'apollo-cache:' ,
134+ } ) ,
135+ plugins : [
136+ responseCachePlugin ( {
137+ sessionId : ( { context } ) => ( context . user ? context . user . id : null ) ,
138+ // Only cache public responses
139+ shouldReadFromCache : ( { context } ) => ! context . user ,
140+ shouldWriteToCache : ( { context } ) => ! context . user ,
141+ } ) ,
142+ ] ,
123143} ) ;
124144
125145export default server ;
0 commit comments