Typescript 类型GraphQL将分页对象添加到解析器

Typescript 类型GraphQL将分页对象添加到解析器,typescript,graphql,typegraphql,Typescript,Graphql,Typegraphql,我创建了一个GraphQL解析器,它使用TypeORM搜索公司对象,我希望允许客户端(可选性)使用分页或排序对象进行查询,因此我编写了这个解析器: @ArgsType() class CompanyProductArgs { @Field() OrderBy?: { fieldName: string; direction: DirectionEnum; }; @Field() Pagination?: { take: number; ski

我创建了一个GraphQL解析器,它使用TypeORM搜索公司对象,我希望允许客户端(可选性)使用分页或排序对象进行查询,因此我编写了这个解析器:

@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只提供了一个示例,其中只有一个变量被接受,但是我想接受一个带有两个键的对象(在本例中为take和skip)。如何编写一个接受对象(如分页对象)的scaller,如下所示:

{
   take: 10
   skip: 5
}
@InputType()
class OrderByInputType {
  @Field()
  fieldName: string;

  @Field()
  direction: DirectionEnum;
}

@InputType()
class PaginationInputType {
  @Field(() => Int)
  take: number;

  @Field(() => Int)
  skip: number;
}

ArgsType
decorator一旦注入
Args
(),就会将所有内容展平。我建议像这样使用
InputType
装饰器:

{
   take: 10
   skip: 5
}
@InputType()
class OrderByInputType {
  @Field()
  fieldName: string;

  @Field()
  direction: DirectionEnum;
}

@InputType()
class PaginationInputType {
  @Field(() => Int)
  take: number;

  @Field(() => Int)
  skip: number;
}
然后将它们作为可选参数传递,如下所示:

companyProducts(
    @Arg("OrderBy", { nullable: true }) OrderBy?: OrderByInputType,
    @Arg("Pagination", { nullable: true }) Pagination?: PaginationInputType
  )

你可以用一种更干净或更紧凑的方式来做这件事,但这应该是可行的,你可以在这里玩

Args
()中注入后,装饰器会将所有内容展平。我建议像这样使用
InputType
装饰器:

{
   take: 10
   skip: 5
}
@InputType()
class OrderByInputType {
  @Field()
  fieldName: string;

  @Field()
  direction: DirectionEnum;
}

@InputType()
class PaginationInputType {
  @Field(() => Int)
  take: number;

  @Field(() => Int)
  skip: number;
}
然后将它们作为可选参数传递,如下所示:

companyProducts(
    @Arg("OrderBy", { nullable: true }) OrderBy?: OrderByInputType,
    @Arg("Pagination", { nullable: true }) Pagination?: PaginationInputType
  )
你可以用一种更干净或更紧凑的方式来做这件事,但这应该是可行的,你可以在这里玩