[英]Type '[]' is missing the following properties from type ''" graphql
I have a simple Resolver in typeGraphQL which findOne and returns a record against the query,i have a similar implementation for Company model that works just fine, but the Product doesnot work, let me show you my code我在typeGraphQL中有一个简单的解析器,它findOne并针对查询返回一条记录,我对公司model 有一个类似的实现,它工作得很好,但是产品不起作用,让我给你看我的代码
entity/Product.ts实体/Product.ts
import { Entity, PrimaryGeneratedColumn, Column, BaseEntity } from "typeorm";
import { ObjectType, Field, ID } from "type-graphql";
//the field decorator represents which fields user can query
@ObjectType()
@Entity()
export class Product extends BaseEntity {
@Field(() => ID)
@PrimaryGeneratedColumn()
id: number;
@Field({nullable: true})
@Column({length: 300, nullable: true})
description: string;
@Field({nullable: true})
@Column({length: 300, nullable: true})
image: string;
@Field({nullable: true})
@Column({ length: 300, nullable: true })
stock: string;
@Field({nullable: true})
@Column({ length: 300, nullable: true })
color: string;
@Field()
@Column({length: 300})
name: string;
@Field()
@Column({length: 300})
company_id: string;
}
Resolver解析器
import { Resolver, Query,Arg } from "type-graphql";
import { Product } from "../../entity/Product";
@Resolver()
export class ProductResolver {
@Query(() => Product)
async companyProducts(
@Arg("name") company_id: string,
): Promise<Product | null> {
const product=await Product.findOne({ where: { company_id } });
console.log(product)
return product;
// return product;
}
}
the console.log() does print out the expected object but when i return I am getting console.log() 确实打印出预期的 object 但是当我返回时我得到了
Type 'Product | undefined' is not assignable to type 'Product | null'.
any idea what might be the reason?知道可能是什么原因吗?
Make your query nullable by @Query(() => Product, { nullable: true })
.通过
@Query(() => Product, { nullable: true })
使您的查询可以为空。
Change your query method return type signature to Promise<Product | undefined>
将您的查询方法返回类型签名更改为
Promise<Product | undefined>
Promise<Product | undefined>
or return null when product is undefined. Promise<Product | undefined>
或在产品未定义时返回 null。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.