Service는 repository가 필요하다. 그래서 UsersModule에 imports에 입력한다.
import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { User } from './entities/user.entity';
import { UsesrsResolver } from './users.resolver';
import { UsersService } from './users.service';
@Module({
imports:[TypeOrmModule.forFeature([User])],
providers: [UsesrsResolver, UsersService]
})
export class UsersModule {}
User GraphQL object 만들기
import { Field, InputType, ObjectType } from "@nestjs/graphql";
import { CoreEntity } from "src/common/entities/core.entity";
import { Column, Entity } from "typeorm";
type UserRole = "client" | "owner" | "delivery";
@InputType({ isAbstract: true })
@ObjectType()
@Entity()
export class User extends CoreEntity {
@Column()
@Field(type => String)
email: string;
@Column()
@Field(type => String)
password: string;
@Column()
@Field(type => String)
role: UserRole;
}
InputType과 ObjectType 비교
Object Type
GraphQL Schema에서 대부분의 definition들은 Object Type이다. 각각의 Object Type은 애플리케이션 클라이언트가 상호 작용할 수 있는 Domain Object를 나타내야 한다. 만약 Author와 Post List를 가져오는 API가 있다고 가정해보자. 우리는 이 기능을 위해 Author Type과 Post Type을 정의해야 한다. code first 방식을 사용한다면, 데코레이터와 Typescript Class를 이용해서 다음과 같이 정의할 수 있다.
import { Field, Int, ObjectType } from '@nestjs/graphql';
import { Post } from './post';
@ObjectType()
export class Author {
@Field(type => Int)
id: number;
@Field({nullable: true})
firstName?: string;
@Field({nullable: true})
lastName?: string;
@Field(type => [Post])
posts: Post[];
}
위와 같이 만든 Author 클래스를 아래처럼 Resolver에서 사용할 수도 있다.
import { Resolver, Query } from '@nestjs/graphql';
import { Author } from './entities/author.entity';
@Resolver()
export class Resolver {
@Query(returns => [Author])
getAllAuthor() {
return [{ name: "example1" }, { name: "example2" }];
}
}
InputType과 ArgsType
InputType과 ArgsType 모두 Query혹은 Mutation에서 Argument들을 받고자할 때 사용할 수 있다. 두 개의 차이점은 코드를 작성할 때와 GraphQL 요청을 보낼 때 나타난다.
코드를 작성할 때 차이점
둘 다 @Args() 데코레이터를 사용한다. 그런데 @Args()의 인자로 이름을 넣어주냐 안넣어주냐에서 차이가 있다. InputType을 사용할 경우, @Args()의 인자로 args의 이름(string)을 넣어주어야한다. ArgsType을 사용할 경우, @Args()의 인자로 args의 이름을 넣지 않아도 된다.
InputType을 사용할 때
import { Field, InputType } from '@nestjs/graphql';
@InputType()
export class AuthorArgs {
@Field()
firstName: string;
@Field()
lastName: string;
}
------------------------------------------------------
import { Resolver, Query } from '@nestjs/graphql';
import { AuthorArgs } from "./dtos/author.dto";
@Resolver()
export class Resolver {
@Query(returns => Boolean)
createAuthor(@Args('example') args: AuthorArgs) {
return true;
}
}
ArgsType을 사용할 때
import { Field, ArgsType } from '@nestjs/graphql';
@ArgsType()
export class AuthorArgs {
@Field()
firstName: string;
@Field()
lastName: string;
}
----------------------------------------------
import { Resolver, Query } from '@nestjs/graphql';
import { AuthorArgs } from "./dtos/author.dto";
@Resolver()
export class Resolver {
@Query(returns => Boolean)
createAuthor(@Args() args: AuthorArgs) {
return true;
}
}
GraphQL 요청을 보낼 때 차이점
InputType은 @Args()에 넘겨준 args의 이름으로 하나의 객체를 보내고, ArgsType은 각각의 Field를 따로따로 보낸다.
InputType을 사용할 때
{
createAuthor(example: { firstName: "Brendan", lastName: "Eich" })
}
ArgsType을 사용할 때
{
createAuthor(firstName: "Brendan", lastName: "Eich")
}
출처: velog.io/@ordidxzero/nestjs-objecttype-and-inputtype
'Uber Eats' 카테고리의 다른 글
Uber Eats # 17 AuthGuard (0) | 2021.03.13 |
---|---|
Uber Eats # 16 User Authentication (0) | 2021.03.11 |
Uber Eats # 14 User Model (0) | 2021.03.09 |
Uber Eats # 13 User CRUD (0) | 2021.03.09 |
Uber Eats # 12 Create Restaurant (0) | 2021.03.08 |