How to make query existence optional? #1310
-
I'm creating abstract resolver that other resolvers will be extended from. It's created in function, which accepts various parameters. I want to be able to turn-off some queries in some resolvers via function parameters. But it's unclear to me how to do that export interface AbstractResolverOptions<T> {
dbModel: ReturnModelType<ClassType<T>>;
itemType: ClassType<T>;
paginatedResponseType: new () => IPaginatedResponse<T>;
getAll?: QueryOptions;
getItem?: QueryOptions;
}
export function makeAbstractResolver<TItemFieldValue>(
opts: AbstractResolverOptions<TItemFieldValue>
) {
const {
dbModel,
itemType,
getAll,
getItem,
paginatedResponseType,
} = opts;
const getAllQueryName = camelCase(dbModel.modelName).replace(/s$/, "") + "s";
const getItemQueryName = "get" + dbModel.modelName.replace(/s$/, "");
@Resolver((of) => itemType, { isAbstract: true })
abstract class AbstractResolverClass {
@ValidateAccessPermissions(getAll?.authOptions)
@Query((returns) => paginatedResponseType)
async [getAllQueryName](
@Args() { offset, take }: PaginationArgs,
@Args() { sortBy, order }: OrderArgs
): Promise<IPaginatedResponse<any>> {
const query: FilterQuery<typeof dbModel> = {};
const [items, total] = await Promise.all([
dbModel
.find(query)
// etc.. For example, sometimes i want to turn-off |
Beta Was this translation helpful? Give feedback.
Answered by
MichalLytek
Jul 15, 2022
Replies: 1 comment
-
You should use mixin classes and compose resolvers if you need all or only some part of the queries |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
Alex0007
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You should use mixin classes and compose resolvers if you need all or only some part of the queries