Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
297 views
in Technique[技术] by (71.8m points)

node.js - 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?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
  1. Make your query nullable by @Query(() => Product, { nullable: true }).

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...