Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
context: (): Context => {
const requestId = Math.floor(Math.random() * Number.MAX_SAFE_INTEGER); // uuid-like
const container = Container.of(requestId); // get scoped container
const context = { requestId, container }; // create our context
container.set("context", context); // place context or other data in container
return context;
},
// create a plugin that will allow for disposing the scoped container created for every request
expressApp.use(env.graphql.route, (request: express.Request, response: express.Response) => {
// Build GraphQLContext
const requestId = Math.floor(Math.random() * Number.MAX_SAFE_INTEGER); // uuid-like
const container = Container.of(requestId); // get scoped container
const context = { requestId, container, request, response }; // create our context
container.set('context', context); // place context or other data in container
// Setup GraphQL Server
GraphQLHTTP({
schema,
context,
graphiql: env.graphql.editor,
formatError: error => ({
code: getErrorCode(error.message),
message: getErrorMessage(error.message),
path: error.path,
}),
})(request, response);
});
context(context) {
const requestId = Math.floor(Math.random() * Number.MAX_SAFE_INTEGER);
const container = Container.of(requestId);
const ctx = {
requestId,
container,
};
container.set('context', ctx);
return ctx;
},
logger: {
async context(session, currentContext) {
const requestId = Math.floor(Math.random() * Number.MAX_SAFE_INTEGER);
const container = Container.of(requestId);
const ctx = {
requestId,
container,
user: ((session || {}) as any).user,
ability: createAbility(currentContext),
};
container.set('context', ctx);
return ctx;
},
resolversComposition: {
async context(session, currentContext) {
const { req } = session;
const requestId = Math.floor(Math.random() * Number.MAX_SAFE_INTEGER);
log('requestId', requestId);
const container = Container.of(requestId);
const ctx: { [key: string]: any } = {
requestId,
container,
};
if (req.headers[PRIME_TOKEN]) {
const server = AccountsModule.injector.get(AccountsServer);
ctx.userSession = await server.findSessionByAccessToken(req.headers[PRIME_TOKEN]);
if (!ctx.userSession) {
throw new AuthenticationError('Token not valid');
}
if (req.headers[PRIME_PREVIEW]) {
ctx.preview = await getRepository(Document).findOneOrFail(req.headers[PRIME_PREVIEW]);
}
}