繁体   English   中英

类型“[]”缺少类型“”中的以下属性 graphql

[英]Type '[]' is missing the following properties from type ''" graphql

我在typeGraphQL中有一个简单的解析器,它findOne并针对查询返回一条记录,我对公司model 有一个类似的实现,它工作得很好,但是产品不起作用,让我给你看我的代码

实体/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;
}

解析器

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;
  }
}

console.log() 确实打印出预期的 object 但是当我返回时我得到了

Type 'Product | undefined' is not assignable to type 'Product | null'.

知道可能是什么原因吗?

  1. 通过@Query(() => Product, { nullable: true })使您的查询可以为空。

  2. 将您的查询方法返回类型签名更改为Promise<Product | undefined> Promise<Product | undefined>或在产品未定义时返回 null。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM