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?
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…