当我尝试从解析器返回自定义dto时出现此错误。 (node:28156) UnhandledPromiseRejectionWarning: Error: Cannot determine GraphQL output type for getProducts
这是我的代码的样子。
product.entity.ts
import {
Entity,
Column,
PrimaryGeneratedColumn,
BaseEntity,
BeforeInsert,
} from 'typeorm';
import { ObjectType, Field, ID, Float, Int } from '@nestjs/graphql';
@ObjectType()
@Entity()
export class Product extends BaseEntity {
@Field(() => ID)
@PrimaryGeneratedColumn('uuid')
id: string;
@Column({ default: '', select: false })
ratingsString: string;
@Field(() => [Int])
ratings;
@BeforeInsert()
setRatingsString() {
this.ratingsString = this.ratings.join(',');
}
}
product.resolver.ts
import { Resolver, Query, Args, ResolveField, Int, Parent } from '@nestjs/graphql';
import { Product } from './product.entity';
import { ProductService } from './product.service';
@Resolver(() => Product)
export class ProductResolver {
constructor(private productService: ProductService) {}
@Query(() => GetProductDto, { name: 'products' })
async getProducts(@Args() page: number) {
return this.productService.getProducts(page);
}
}
product.service.ts
import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { Product } from './product.entity';
import { GetProductDto } from './product.dto';
@Injectable()
export class ProductService {
constructor(
@InjectRepository(Product) private productRepo: Repository<Product>,
) {}
async getProducts(page: number): Promise<GetProductDto> {
const MAX_PRODUCT_PER_PAGE = 20;
const start = page < 1 ? 1 : page;
const [products, productsCount] = await this.productRepo.findAndCount({
skip: (start - 1) * MAX_PRODUCT_PER_PAGE,
take: MAX_PRODUCT_PER_PAGE,
});
const productList = products.map(product => {
product.ratings = product.ratingsString.split(',').map(Number);
delete product.ratingsString;
return product;
});
return {
products: productList,
total: productsCount,
};
}
}
product.dto.ts
import { Field, Int, InputType } from '@nestjs/graphql';
import { Product } from './product.entity';
@InputType()
export class GetProductDto {
@Field(() => [Product])
products: Product[];
@Field(() => Int)
total: number;
}
这里的期望是,当我调用产品查询时,我将收到一个包含产品和总计的对象。 Whee产品将是我的产品系列。我在做什么错了?
答案 0 :(得分:0)
结果我只需要为Args提供一个参数即可。
@Query(() => GetProductDto, { name: 'products' })
async getProducts(@Args('arg_the_client_will_it_with') page: number) {
return this.productService.getProducts(page);
}