// image.entity.ts
import { Field, ObjectType } from '@nestjs/graphql';
import {
Column,
DeleteDateColumn,
Entity,
PrimaryGeneratedColumn,
} from 'typeorm';
@ObjectType()
@Entity()
export class ImageEntity {
@PrimaryGeneratedColumn('uuid')
@Field(() => String)
id: string;
@Column({ default: false })
@Field(() => Boolean, { defaultValue: false })
isThumbnail: boolean;
//isThumbnail?: boolean; // it worked without question mark
}
//image.resolver.ts
import { Args, Mutation, Query, Resolver } from '@nestjs/graphql';
import { ImageEntity } from './entities/image.entity';
import { ImageService } from './image.service';
@Resolver()
export class ImageResolver {
constructor(private readonly imageService: ImageService) {}
@Mutation(() => ImageEntity)
async createImage(
@Args('imageUrl') imageUrl: string,
//=============this part=================
@Args('isThumbnail', { nullable: true }) isThumbnail?: boolean, // <--- that question mark I'm takin bout
// @Args('isThumbnail', { nullable: true }) isThumbnail: boolean, // it work fine without the question mark
// what this thing for?
//========================================
) {
return await this.imageService.create({ isThumbnail, imageUrl });
}
}
就像在代碼上一樣,它與 TypeORM 和 GraphQL 相關聯。并且一些 args(也是相關列)可以為空。
我知道 {nullable : true} 使 GraphQL 和 TypeORM 的 args 和列都可以在沒有問號的情況下使用。它作業得很好,但是在nestjs doc上,問號就在下面
@Field(()=>某種型別,{nullable: true}。
我的問題是問號只是表示它作為打字稿型別是可選的?
uj5u.com熱心網友回復:
是的,這意味著它是可選的,請參閱https://www.geeksforgeeks.org/why-use-question-mark-in-typescript-variable/
可以理解,它似乎是多余的,其原因是一方面 GraphQL 必須知道它是nullable,另一方面,Typescript 強迫你強烈地定義你的型別。
GraphQL 也可以被“普通”Javascript 使用,它是非常松散型別的,在這種情況下,唯一可以指定專案的可選性質的地方是nullable屬性。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/428562.html
上一篇:<Typescript>函式引數具有泛型。如何通過?
下一篇:不能將型別與“匯入型別”一起使用
