I've created a GraphQL resolver which searches for Company objects using TypeORM, I would like the to allow the client to (optionality) query with pagination or order objects so I wrote this resolver:
@ArgsType()
class CompanyProductArgs {
@Field()
OrderBy?: {
fieldName: string;
direction: DirectionEnum;
};
@Field()
Pagination?: {
take: number;
skip: number;
};
}
@Resolver()
export class CompanyProductResolver {
@Query(() => [CompanyProduct])
companyProducts(@Args() { OrderBy, Pagination }: CompanyProductArgs) {
let args = {};
if (OrderBy) {
args = {
...args,
order: {
[OrderBy.fieldName]: OrderBy.direction,
},
};
}
if (Pagination) {
args = {
...args,
skip: Pagination.skip,
take: Pagination.take,
};
}
return CompanyProduct.find(args);
}
}
But running this returns:
Error: You need to provide explicit type for CompanyProductArgs#OrderBy
The way to solve this would be using a Custom Scalers (I think), but the type-GraphQL documentation only provide one example in which only one variable gets accepted, but I want to accept an object with 2 keys (take and skip in this case). How would I write a scaller that accepts object such as a pagination object like this:
{
take: 10
skip: 5
}