简体   繁体   English

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

[英]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?知道可能是什么原因吗?

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

  2. 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.

相关问题 类型“文档”缺少类型中的以下属性 - Type 'Document' is missing the following properties from type “S3”类型缺少“S3Client”类型的以下属性:destroy、middlewareStack、sendts(2739) - Type 'S3' is missing the following properties from type 'S3Client': destroy, middlewareStack, sendts(2739) 类型“{}”缺少来自“请求”类型的以下属性:get、header、accepts、acceptsCharsets 和 67 个更多 - Type '{}' is missing the following properties from type 'Request': get, header, accepts, acceptsCharsets, and 67 more “typeof Sequelize”类型缺少“Sequelize”类型中的以下属性:Sequelize、config、modelManager、connectionManager 等 23 个 - Type 'typeof Sequelize' is missing the following properties from type 'Sequelize': Sequelize, config, modelManager, connectionManager, and 23 more 类型&#39;{孩子:元素[]; }&#39; 缺少“RouterProps”类型的以下属性:位置、导航器 - Type '{ children: Element[]; }' is missing the following properties from type 'RouterProps': location, navigator 从 promise 返回值时出现 TS 2739 错误。 输入“承诺”<any> ' 缺少类型中的以下属性</any> - Got TS 2739 error while returning value from promise. Type 'Promise<any>' is missing the following properties from type 响应中缺少GraphQL突变类型 - GraphQL mutation type missing in the response graphQL-类型必须为输出类型 - graphQL - type must be Output Type 将上传类型添加到 GraphQL - Add Upload type to GraphQL graphql的多个内置类型 - Multiple builtin type for graphql
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM