115 lines
6.3 KiB
TypeScript
115 lines
6.3 KiB
TypeScript
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 = <U extends ClassType<any>, T extends ClassType<any>, ARGS extends ClassType<any>>(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<T> {
|
|
const model = new init.modelName() as EngineModel<any, any, any>;
|
|
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<T[]> {
|
|
const model = new init.modelName() as EngineModel<any, any, any>;
|
|
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<number> {
|
|
const model = new init.modelName() as EngineModel<any, any, any>;
|
|
return model.getCountGeneric(args, ctx);
|
|
}
|
|
}
|
|
|
|
return BaseQueryResolver;
|
|
};
|
|
|
|
export const createEngineMutationResolver = <
|
|
U extends ClassType<any>,
|
|
T extends ClassType<any>,
|
|
NEW extends ClassType<any>,
|
|
EDIT extends ClassType<any>
|
|
>(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<U> {
|
|
const model = new init.modelName() as EngineModel<any, any, any>;
|
|
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<U> {
|
|
const model = new init.modelName() as EngineModel<any, any, any>;
|
|
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<U> {
|
|
const model = new init.modelName() as EngineModel<any, any, any>;
|
|
return await await model.deleteOne({
|
|
query: { _id: id },
|
|
ctx: ctx,
|
|
});
|
|
}
|
|
}
|
|
|
|
return BaseMutationResolver;
|
|
};
|