嵌套错误:无法确定 getProducts 的 GraphQL 输出类型
Posted
技术标签:
【中文标题】嵌套错误:无法确定 getProducts 的 GraphQL 输出类型【英文标题】:Nest Error: Cannot determine GraphQL output type for getProducts 【发布时间】:2020-07-07 08:34:54 【问题描述】:当我尝试从解析器返回自定义 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 产品将是我的一系列产品。我做错了什么?
【问题讨论】:
【参考方案1】:原来我只需要为 Args 提供一个参数。
@Query(() => GetProductDto, name: 'products' )
async getProducts(@Args('arg_the_client_will_it_with') page: number)
return this.productService.getProducts(page);
【讨论】:
以上是关于嵌套错误:无法确定 getProducts 的 GraphQL 输出类型的主要内容,如果未能解决你的问题,请参考以下文章