简体   繁体   中英

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

entity/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

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 }) .

  2. Change your query method return type signature to Promise<Product | undefined> Promise<Product | undefined> or return null when product is undefined.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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