【发布时间】:2020-07-28 21:07:41
【问题描述】:
我创建了一个使用 TypeORM 搜索 Company 对象的 GraphQL 解析器,我希望允许客户端(可选)使用分页或排序对象进行查询,所以我编写了这个解析器:
@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);
}
}
但是运行它会返回:
错误:您需要为 CompanyProductArgs#OrderBy 提供显式类型
解决这个问题的方法是使用自定义缩放器(我认为),但类型 GraphQL documentation 仅提供一个示例,其中仅接受一个变量,但我想接受一个带有 2 个键的对象(在这种情况下采取并跳过)。我将如何编写一个接受对象的调用程序,例如这样的分页对象:
{
take: 10
skip: 5
}
【问题讨论】:
标签: typescript graphql typegraphql