98 lines
5.8 KiB
TypeScript
98 lines
5.8 KiB
TypeScript
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 = <T extends ClassType<any>, ARGS extends ClassType<any>>(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<T> {
|
|
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<T[]> {
|
|
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<number> {
|
|
const model = new init.modelName();
|
|
return getCountGeneric(model, args, ctx);
|
|
}
|
|
}
|
|
|
|
return BaseQueryResolver;
|
|
};
|
|
|
|
export const createGenericMutationResolver = <T extends ClassType<any>, NEW extends ClassType<any>, EDIT extends ClassType<any>>(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<T> {
|
|
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<T> {
|
|
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<T> {
|
|
const model = new init.modelName();
|
|
return deleteOneGeneric(model, id, ctx);
|
|
}
|
|
}
|
|
|
|
return BaseMutationResolver;
|
|
};
|