add global event handler middleware

This commit is contained in:
Moriz Wahl 2022-06-22 16:02:28 +02:00
parent 7ecd96d28b
commit bab867a1bd
3 changed files with 18 additions and 1 deletions

View File

@ -10,7 +10,7 @@ Decimal.set({
})
const constants = {
DB_VERSION: '0040-add_contribution_link_id_to_user',
DB_VERSION: '0041-add_event_protocol_table',
DECAY_START_TIME: new Date('2021-05-13 17:46:31-0000'), // GMT+0
LOG4JS_CONFIG: 'log4js-config.json',
// default log level on production should be info

View File

@ -1,6 +1,7 @@
import { GraphQLSchema } from 'graphql'
import { buildSchema } from 'type-graphql'
import path from 'path'
import { EventHandler } from '@/middleware/EventHandler'
import isAuthorized from './directive/isAuthorized'
import DecimalScalar from './scalar/Decimal'
@ -11,6 +12,7 @@ const schema = async (): Promise<GraphQLSchema> => {
resolvers: [path.join(__dirname, 'resolver', `!(*.test).{js,ts}`)],
authChecker: isAuthorized,
scalarsMap: [{ type: Decimal, scalar: DecimalScalar }],
globalMiddlewares: [EventHandler],
})
}

View File

@ -0,0 +1,15 @@
import { MiddlewareFn } from 'type-graphql'
import { EventProtocol } from '@entity/EventProtocol'
export const EventHandler: MiddlewareFn = async (
/* eslint-disable-next-line @typescript-eslint/no-unused-vars */
{ root, args, context, info },
next,
) => {
const event = new EventProtocol()
// set values before calling the resolver here
const result = await next()
// set event values here when having the result ...
await event.save()
return result
}