Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

What is the question mark for next to TypeScript arg when its GraphQL arg indicates nullable true?

// 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 });
  }
}

Like on the code, it linked with TypeORM and GraphQL. And some of args (also related columns) are nullable.

I got it that {nullable : true} made both args and columns of GraphQL and TypeORM doable without the question mark. It worked fine, but on the nestjs doc, the question mark was right below the

@Field(()=>Some Type, {nullable: true}.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

My question is that the question mark is just to indicate it is optional as typescript type?

>Solution :

Yes, it means that it’s optional, see https://www.geeksforgeeks.org/why-use-question-mark-in-typescript-variable/

It is understandable that it seems to be superfluous, the reason for this is that on the one hand, GraphQL has to know that it’s nullable and on the other, Typescript forces you to define your types strongly.

GraphQL can be used by "normal" Javascript as well, which is very much loose-typed, in that use-case the only place where the optional nature of the item would be specified is the nullable attribute.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading