import { Query, Arg, Resolver, Ctx, Args, Mutation, ClassType } from 'type-graphql'; import { ApolloContext } from '@seed/interfaces/context'; import { getOneGeneric, getManyGenericWithArgs, addOneGeneric, editOneGeneric, deleteOneGeneric, getCountGeneric } from '../BaseService'; import { EngineMiddleware, EngineMiddlewareInput } from '../MiddlewareV2'; export const createGenericQueryResolver = , ARGS extends ClassType>(init: { domain: string; modelName: T; argsType: ARGS; customMiddlewareInput: EngineMiddlewareInput; }): any => { @Resolver({ isAbstract: true }) abstract class BaseQueryResolver { /* ███████╗██╗███████╗██╗ ██████╗ ███████╗ ██╔════╝██║██╔════╝██║ ██╔══██╗██╔════╝ █████╗ ██║█████╗ ██║ ██║ ██║███████╗ ██╔══╝ ██║██╔══╝ ██║ ██║ ██║╚════██║ ██║ ██║███████╗███████╗██████╔╝███████║ ╚═╝ ╚═╝╚══════╝╚══════╝╚═════╝ ╚══════╝ */ /* ██████╗ ██╗ ██╗███████╗██████╗ ██╗ ██╗ ██╔═══██╗██║ ██║██╔════╝██╔══██╗╚██╗ ██╔╝ ██║ ██║██║ ██║█████╗ ██████╔╝ ╚████╔╝ ██║▄▄ ██║██║ ██║██╔══╝ ██╔══██╗ ╚██╔╝ ╚██████╔╝╚██████╔╝███████╗██║ ██║ ██║ ╚══▀▀═╝ ╚═════╝ ╚══════╝╚═╝ ╚═╝ ╚═╝ */ @Query(() => init.modelName, { name: `${init.domain}GetOne` }) @EngineMiddleware(init.customMiddlewareInput) async getOne(@Arg('id') id: string, @Ctx() ctx: ApolloContext): Promise { const model = new init.modelName(); return getOneGeneric(model, id, ctx); } @Query(() => [init.modelName], { name: `${init.domain}GetMany` }) @EngineMiddleware(init.customMiddlewareInput) async getMany(@Args(() => init.argsType) args: ARGS, @Ctx() ctx: ApolloContext): Promise { const model = new init.modelName(); return getManyGenericWithArgs(model, args, ctx); } @Query(() => Number, { name: `${init.domain}GetCount` }) @EngineMiddleware(init.customMiddlewareInput) async getCount(@Args(() => init.argsType) args: ARGS, @Ctx() ctx: ApolloContext): Promise { const model = new init.modelName(); return getCountGeneric(model, args, ctx); } } return BaseQueryResolver; }; export const createGenericMutationResolver = , NEW extends ClassType, EDIT extends ClassType>(init: { domain: string; modelName: T; newInput: NEW; editInput: EDIT; customMiddlewareInput: EngineMiddlewareInput; }): any => { @Resolver({ isAbstract: true }) abstract class BaseMutationResolver { /* ███╗ ███╗██╗ ██╗████████╗ █████╗ ████████╗ ██████╗ ██████╗ ███████╗ ████╗ ████║██║ ██║╚══██╔══╝██╔══██╗╚══██╔══╝██╔═══██╗██╔══██╗██╔════╝ ██╔████╔██║██║ ██║ ██║ ███████║ ██║ ██║ ██║██████╔╝███████╗ ██║╚██╔╝██║██║ ██║ ██║ ██╔══██║ ██║ ██║ ██║██╔══██╗╚════██║ ██║ ╚═╝ ██║╚██████╔╝ ██║ ██║ ██║ ██║ ╚██████╔╝██║ ██║███████║ ╚═╝ ╚═╝ ╚═════╝ ╚═╝ ╚═╝ ╚═╝ ╚═╝ ╚═════╝ ╚═╝ ╚═╝╚══════╝ */ @Mutation(() => init.modelName, { name: `${init.domain}AddOne` }) @EngineMiddleware(init.customMiddlewareInput) async addOne(@Arg('input', () => init.newInput) input: NEW, @Ctx() ctx: ApolloContext): Promise { const model = new init.modelName(); return addOneGeneric(model, input, ctx); } @Mutation(() => init.modelName, { name: `${init.domain}EditOne` }) @EngineMiddleware(init.customMiddlewareInput) async editOne(@Arg('id') id: string, @Arg('input', () => init.editInput) input: EDIT, @Ctx() ctx: ApolloContext): Promise { const model = new init.modelName(); return editOneGeneric(model, id, input, ctx); } @Mutation(() => init.modelName, { name: `${init.domain}DeleteOne` }) @EngineMiddleware(init.customMiddlewareInput) async deleteOne(@Arg('id') id: string, @Ctx() ctx: ApolloContext): Promise { const model = new init.modelName(); return deleteOneGeneric(model, id, ctx); } } return BaseMutationResolver; };