import { Query, Arg, Resolver, Ctx, Args, Mutation, ClassType } from 'type-graphql'; import { ApolloContext } from '@seed/interfaces/context'; import { EngineMiddleware, EngineMiddlewareInput } from '@seed/graphql/MiddlewareV2'; import { EngineModel } from '../EngineModel'; export const createEngineQueryResolver = , T extends ClassType, ARGS extends ClassType>(init: { domain: string; schemaName: U; modelName: T; argsType: ARGS; engineMiddleware: EngineMiddlewareInput; }): any => { @Resolver({ isAbstract: true }) abstract class BaseQueryResolver { /* ███████╗██╗███████╗██╗ ██████╗ ███████╗ ██╔════╝██║██╔════╝██║ ██╔══██╗██╔════╝ █████╗ ██║█████╗ ██║ ██║ ██║███████╗ ██╔══╝ ██║██╔══╝ ██║ ██║ ██║╚════██║ ██║ ██║███████╗███████╗██████╔╝███████║ ╚═╝ ╚═╝╚══════╝╚══════╝╚═════╝ ╚══════╝ */ /* ██████╗ ██╗ ██╗███████╗██████╗ ██╗ ██╗ ██╔═══██╗██║ ██║██╔════╝██╔══██╗╚██╗ ██╔╝ ██║ ██║██║ ██║█████╗ ██████╔╝ ╚████╔╝ ██║▄▄ ██║██║ ██║██╔══╝ ██╔══██╗ ╚██╔╝ ╚██████╔╝╚██████╔╝███████╗██║ ██║ ██║ ╚══▀▀═╝ ╚═════╝ ╚══════╝╚═╝ ╚═╝ ╚═╝ */ @Query(() => init.schemaName, { name: `${init.domain}GetOne` }) @EngineMiddleware(init.engineMiddleware) async getOne(@Arg('id') id: string, @Ctx() ctx: ApolloContext): Promise { const model = new init.modelName() as EngineModel; const result = await model.getOneGeneric(id, ctx); return result; } @Query(() => [init.schemaName], { name: `${init.domain}GetMany` }) @EngineMiddleware(init.engineMiddleware) async getMany(@Args(() => init.argsType) args: ARGS, @Ctx() ctx: ApolloContext): Promise { const model = new init.modelName() as EngineModel; return model.getManyGenericWithArgs(args, ctx); } @Query(() => Number, { name: `${init.domain}GetCount` }) @EngineMiddleware(init.engineMiddleware) async getCount(@Args(() => init.argsType) args: ARGS, @Ctx() ctx: ApolloContext): Promise { const model = new init.modelName() as EngineModel; return model.getCountGeneric(args, ctx); } } return BaseQueryResolver; }; export const createEngineMutationResolver = < U extends ClassType, T extends ClassType, NEW extends ClassType, EDIT extends ClassType >(init: { domain: string; schemaName: U; modelName: T; newInput: NEW; editInput: EDIT; engineMiddleware: EngineMiddlewareInput; validate?: boolean; }): any => { @Resolver({ isAbstract: true }) abstract class BaseMutationResolver { /* ███╗ ███╗██╗ ██╗████████╗ █████╗ ████████╗ ██████╗ ██████╗ ███████╗ ████╗ ████║██║ ██║╚══██╔══╝██╔══██╗╚══██╔══╝██╔═══██╗██╔══██╗██╔════╝ ██╔████╔██║██║ ██║ ██║ ███████║ ██║ ██║ ██║██████╔╝███████╗ ██║╚██╔╝██║██║ ██║ ██║ ██╔══██║ ██║ ██║ ██║██╔══██╗╚════██║ ██║ ╚═╝ ██║╚██████╔╝ ██║ ██║ ██║ ██║ ╚██████╔╝██║ ██║███████║ ╚═╝ ╚═╝ ╚═════╝ ╚═╝ ╚═╝ ╚═╝ ╚═╝ ╚═════╝ ╚═╝ ╚═╝╚══════╝ */ @Mutation(() => init.schemaName, { name: `${init.domain}AddOne` }) @EngineMiddleware({ ...init.engineMiddleware, validations: [{ schema: init.newInput }] }) async addOne(@Arg('input', () => init.newInput) input: NEW, @Ctx() ctx: ApolloContext): Promise { const model = new init.modelName() as EngineModel; return await await model.saveOne({ newData: input, ctx }); } @Mutation(() => init.schemaName, { name: `${init.domain}EditOne` }) @EngineMiddleware({ ...init.engineMiddleware, validations: [{ schema: init.editInput }] }) async editOne(@Arg('id') id: string, @Arg('input', () => init.editInput) input: EDIT, @Ctx() ctx: ApolloContext): Promise { const model = new init.modelName() as EngineModel; return await await model.updateOne({ query: { _id: id }, newData: input, ctx: ctx, }); } @Mutation(() => init.schemaName, { name: `${init.domain}DeleteOne` }) @EngineMiddleware({ ...init.engineMiddleware }) async deleteOne(@Arg('id') id: string, @Ctx() ctx: ApolloContext): Promise { const model = new init.modelName() as EngineModel; return await await model.deleteOne({ query: { _id: id }, ctx: ctx, }); } } return BaseMutationResolver; };