Skip to content

Instantly share code, notes, and snippets.

@DillonMemo
Last active February 16, 2021 10:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DillonMemo/54f990f5994058001a944b0b7f1266c1 to your computer and use it in GitHub Desktop.
Save DillonMemo/54f990f5994058001a944b0b7f1266c1 to your computer and use it in GitHub Desktop.
finally NestJS configure practice
NestJS
  ├─src
  │  ├─users  
  │  │  ├─dtos
  │  │  │  └─create-user.dto.ts
  │  │  ├─entities
  │  │  │  └─users.entitiy.ts
  │  │  ├─users.module.ts  
  │  │  └─users.resolver.ts
  │  ├─app.module.ts
  │  └─main.ts
  ├─test
  │  └─...test files
  └─eslint,env,prettier etc config files
import { ArgsType, Field, Int } from '@nestjs/graphql';
import {
IsBoolean,
IsInt,
IsOptional,
IsString,
Length,
} from 'class-validator';
// data transfet object(DTO)
@ArgsType()
export class CreateUserDto {
@Field(() => Int)
@IsInt()
id: number;
@Field(() => String)
@IsString()
@Length(2, 10)
name: string;
@Field(() => String)
@IsString()
email: string;
@Field(() => String, { nullable: true })
@IsOptional()
@IsString()
gender?: 'male' | 'female';
@Field(() => String)
@IsString()
address: string;
@Field(() => Boolean)
@IsBoolean()
isVIP: boolean;
}
import { Field, Int, ObjectType } from '@nestjs/graphql';
@ObjectType()
export class User {
@Field(() => Int)
id: number;
@Field(() => String)
name: string;
@Field(() => String)
email: string;
@Field(() => String, { nullable: true })
gender?: 'male' | 'female';
@Field(() => String)
address: string;
@Field(() => Boolean)
isVIP: boolean;
}
import { Args, Mutation, Query, Resolver } from '@nestjs/graphql';
import { CreateUserDto } from './dtos/create-user.dto';
import { User } from './entities/users.entity';
@Resolver(() => User)
export class UserResolver {
@Query(() => Boolean)
users(@Args('bool') bool: boolean): boolean {
return bool;
}
@Mutation(() => Boolean)
createUser(@Args() createUserDto: CreateUserDto): boolean {
console.log(createUserDto);
return true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment