|
|
|
|
|
const debug = require('debug')('api:graphql'); |
|
|
import { ApolloServer } from 'apollo-server-express'; |
|
|
import responseCachePlugin from 'apollo-server-plugin-response-cache'; |
|
|
import depthLimit from 'graphql-depth-limit'; |
|
|
import costAnalysis from 'graphql-cost-analysis'; |
|
|
import { RedisCache } from 'apollo-server-cache-redis'; |
|
|
import createLoaders from './loaders'; |
|
|
import createErrorFormatter from './utils/create-graphql-error-formatter'; |
|
|
import schema from './schema'; |
|
|
import { statsd } from 'shared/statsd'; |
|
|
import { getUserIdFromReq } from './utils/session-store'; |
|
|
import UserError from './utils/UserError'; |
|
|
import type { DBUser } from 'shared/types'; |
|
|
import { getUserById } from '../shared/db/queries/user'; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class ProtectedApolloServer extends ApolloServer { |
|
|
async createGraphQLServerOptions( |
|
|
req: express$Request, |
|
|
res: express$Response |
|
|
): Promise<*> { |
|
|
const options = await super.createGraphQLServerOptions(req, res); |
|
|
|
|
|
return { |
|
|
...options, |
|
|
validationRules: [ |
|
|
...options.validationRules, |
|
|
costAnalysis({ |
|
|
maximumCost: 750, |
|
|
defaultCost: 1, |
|
|
variables: req.body.variables, |
|
|
createError: (max, actual) => { |
|
|
const err = new UserError( |
|
|
`GraphQL query exceeds maximum complexity, please remove some nesting or fields and try again. (max: ${max}, actual: ${actual})` |
|
|
); |
|
|
return err; |
|
|
}, |
|
|
}), |
|
|
], |
|
|
}; |
|
|
} |
|
|
} |
|
|
|
|
|
let connections = 0; |
|
|
|
|
|
setInterval(() => { |
|
|
statsd.gauge('websocket.connections.count', connections); |
|
|
}, 5000); |
|
|
|
|
|
const server = new ProtectedApolloServer({ |
|
|
schema, |
|
|
formatError: createErrorFormatter(), |
|
|
|
|
|
context: ({ req, res, connection, ...rest }, ...other) => { |
|
|
if (connection) { |
|
|
return { |
|
|
...(connection.context || {}), |
|
|
}; |
|
|
} |
|
|
|
|
|
|
|
|
req.statsdTags = { |
|
|
graphqlOperationName: req.body.operationName || 'unknown_operation', |
|
|
}; |
|
|
debug(req.body.operationName || 'unknown_operation'); |
|
|
const loaders = createLoaders(); |
|
|
let currentUser = req.user && !req.user.bannedAt ? req.user : null; |
|
|
|
|
|
return { |
|
|
loaders, |
|
|
updateCookieUserData: (data: DBUser) => |
|
|
new Promise((res, rej) => |
|
|
req.login(data, err => (err ? rej(err) : res())) |
|
|
), |
|
|
req, |
|
|
user: currentUser, |
|
|
}; |
|
|
}, |
|
|
subscriptions: { |
|
|
path: '/websocket', |
|
|
onConnect: (connectionParams, rawSocket) => { |
|
|
connections++; |
|
|
return getUserIdFromReq(rawSocket.upgradeReq) |
|
|
.then(id => (id ? getUserById(id) : null)) |
|
|
.then(user => { |
|
|
return { |
|
|
user: user || null, |
|
|
loaders: createLoaders({ cache: false }), |
|
|
}; |
|
|
}) |
|
|
.catch(err => { |
|
|
console.error(err); |
|
|
return { |
|
|
loaders: createLoaders({ cache: false }), |
|
|
}; |
|
|
}); |
|
|
}, |
|
|
}, |
|
|
playground: process.env.NODE_ENV !== 'production' && { |
|
|
settings: { |
|
|
'editor.theme': 'light', |
|
|
}, |
|
|
tabs: [ |
|
|
{ |
|
|
endpoint: 'http://localhost:3001/api', |
|
|
query: `{ |
|
|
user(username: "mxstbr") { |
|
|
id |
|
|
username |
|
|
} |
|
|
}`, |
|
|
}, |
|
|
], |
|
|
}, |
|
|
introspection: process.env.NODE_ENV !== 'production', |
|
|
maxFileSize: 25 * 1024 * 1024, |
|
|
engine: false, |
|
|
tracing: false, |
|
|
validationRules: [depthLimit(10)], |
|
|
cacheControl: { |
|
|
calculateHttpHeaders: false, |
|
|
|
|
|
defaultMaxAge: 60, |
|
|
}, |
|
|
plugins: [ |
|
|
responseCachePlugin({ |
|
|
sessionId: ({ context }) => (context.user ? context.user.id : null), |
|
|
|
|
|
shouldReadFromCache: ({ context }) => !context.user, |
|
|
shouldWriteToCache: ({ context }) => !context.user, |
|
|
}), |
|
|
], |
|
|
}); |
|
|
|
|
|
export default server; |
|
|
|