common declaration for nestjs and graphql

◾️ declare one entity for nestjs and graphql

import { Field, InputType, ObjectType } from '@nestjs/graphql';
import { Column, Entity, PrimaryGeneratedColumn } from 'typeorm';
import { IsOptional, IsString, Length } from 'class-validator';

// do not include schema - isAbstract : true
@InputType({ isAbstract: true })
@ObjectType()
@Entity()
export class User {
  @Field((type) => Number)
  @PrimaryGeneratedColumn()
  id: number;

  @Field((type) => String)
  @Column()
  @IsString()
  @Length(5)
  name: string;

  @Field((type) => String)
  @Column()
  @IsOptional()
  @IsString()
  nickName: string;
}

◾️ declare create-dto without id (mapped type link)

import { InputType, OmitType } from '@nestjs/graphql';
import { Restaurant } from '../entities/user.entity';

@InputType()
export class CreateUserDto extends OmitType(User, ['id']) {}

◾️ declare update-dto using partialtype

import { Field, InputType, PartialType } from '@nestjs/graphql';
import { CreateUserDto } from './create-user.dto';

@InputType()
class UpdateUserInputType extends PartialType(CreateUserDto) {}

@InputType()
export class UpdateUserDto {
  @Field((type) => Number)
  id: number;

  @Field((type) => UpdateUserInputType)
  data: UpdateUserInputType;
}

コメントを残す

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です