안녕하세요. 주니어 백엔드 개발자 입니다.
심바 블로그
안녕하세요. 주니어 백엔드 개발자 입니다.
>> Github 바로가기
  • 분류 전체보기 (31)
    • AWS (3)
    • DATABASE (2)
    • Docker (1)
    • Architecture (3)
    • GraphQL (5)
    • JS (1)
    • NestJS (8)
    • 테스트 코드 (3)
    • 세미나 (4)
    • 기타 (1)
    • 이전 블로그 자료들 (0)

블로그 메뉴

  • 홈
  • 방명록

공지사항

인기 글

최근 댓글

최근 글

티스토리

hELLO · Designed By 정상우.
안녕하세요. 주니어 백엔드 개발자 입니다.

심바 블로그

GraphQL

nestjs-query_02_Hook, Authorize

2024. 1. 14. 20:56
반응형

nestjs-query_02

1. BeforeCreateOneHook, BeforeUpdateOneHook

nestjs-query에서는 몇 가지 훅들을 제공한다. 그중 가장 대표적으로 BeforeCreateOneHook과 BeforeUpdateOneHook이다. 해당 훅들은 module에서 자동으로 만들어 주는 CreateOne, UpdateOne 전에 실행되게 사용할 수 있는 기능이다. resolver에 도달하기 전에 실행되기 때문에 nestjs의 interceptor라고 봐도 된다.

2. 사용법

2-1. BeforeCreateOneHook 사용법

아래 보이는 것처럼 BeforeCreateOneHook를 사용해 구현할 수 있다.

instance는 자동 생성되는 createOne의 input query를 받아오고 context는 GqlContext를 받아온다.

import { Injectable } from '@nestjs/common';
import { BoardEntity } from '../board.entity';
import {
  BeforeCreateOneHook,
  CreateOneInputType,
} from '@ptc-org/nestjs-query-graphql';

export type GqlContext = {
  req: {
    headers: Record<string, string>;
    connection?: any;
    ip?: string;
  };
};

@Injectable()
export class CreatedByHook implements BeforeCreateOneHook<BoardEntity> {
  async run(
    instance: CreateOneInputType<BoardEntity>,
    context: GqlContext,
  ): Promise<any> {
    const input = instance.input;
    const ip = context.req.headers['x-user-id'] || context.req.ip;
    return { input, ip };
  }
}

2-2. BeforeUpdateOneHook 사용법

BeforeUpdateOneHook도 BeforeCreateOneHook과 동일한 방식으로 사용 가능하다.

import { Injectable } from '@nestjs/common';
import { BoardEntity } from '../board.entity';
import {
  BeforeUpdateOneHook,
  UpdateOneInputType,
} from '@ptc-org/nestjs-query-graphql';

export type GqlContext = {
  req: {
    headers: Record<string, string>;
    connection?: any;
    ip?: string;
  };
};

@Injectable()
export class UpdatedByHook implements BeforeUpdateOneHook<BoardEntity> {
  async run(
    instance: UpdateOneInputType<BoardEntity>,
    context: GqlContext,
  ): Promise<any> {
    const update = instance.update;
    const ip = context.req.headers['x-user-id'] || context.req.ip;
    return { update, ip };
  }
}

3. Hook 종류들

이 두개 외에도 아래와 같은 Hook을 제공한다.

@BeforeFindOne - invoked before any findOne query. @BeforeQueryMany - invoked before any queryMany query. @BeforeCreateOne - invoked before any createOne mutation. @BeforeCreateMany - invoked before any createMany mutation. @BeforeUpdateOne - invoked before any updateOne mutation. @BeforeUpdateMany - invoked before any updateMany mutation. @BeforeDeleteOne - invoked before any deleteOne mutation. @BeforeDeleteMany - invoked before any deleteMany mutation.

4. Authorize

nestjs에서는 인증에 대한 기능도 제공한다. nestjs-query01(링크)에서 설정한 module에서 guards 옵션을 통해서 설정할 수도 있고, entity에 @Authorize()를 통해 자동생성되는 CRUD에 대한 인증을 설정해 줄 수도 있다

4-1. entity에 Authorize 적용하는 방법

import { Field, GraphQLISODateTime, ID, ObjectType } from '@nestjs/graphql';
import { FilterableField, IDField } from '@ptc-org/nestjs-query-graphql';
import {
  Column,
  CreateDateColumn,
  Entity,
  PrimaryGeneratedColumn,
  UpdateDateColumn,
} from 'typeorm';

@Entity()
// 아래처럼 @Authorize()를 추가해 자동생성 되는 모든 CRUD에 인증 관련 기을 추가해줄 수 있다.
@Authorize({ authorize: (context: UserContext) => ({ userId: { eq: context.req.user.id } }) })
// BoardEntity와 Relation에 UserDTO가 있을 때
@FilterableRelation('owner', () => UserDTO)
@ObjectType('Board')
export class BoardEntity {
  @PrimaryGeneratedColumn()
  @IDField(() => ID) // 기존 graphql 처럼 하나의 엔티티에서 DTO도 선능언 가능함
  id!: number;

  @Column()
  @FilterableField()
  title!: string;

  @Column()
  @FilterableField()
  description!: string;

  @CreateDateColumn()
  @Field(() => GraphQLISODateTime)
  created!: Date;

  @UpdateDateColumn()
  @Field(() => GraphQLISODateTime)
  updated!: Date;
}
반응형

'GraphQL' 카테고리의 다른 글

nestjs-query_01_세팅  (1) 2024.01.10
Nest + Graphql 구현하기  (0) 2022.09.29
Express + Graphql 좀 더 구조적으로 짜기 (+ Type-graphql, Typegoose, Typedi)  (0) 2022.09.29
TypeScript로 GraphQL 사용할 때의 문제  (0) 2022.09.28
    'GraphQL' 카테고리의 다른 글
    • nestjs-query_01_세팅
    • Nest + Graphql 구현하기
    • Express + Graphql 좀 더 구조적으로 짜기 (+ Type-graphql, Typegoose, Typedi)
    • TypeScript로 GraphQL 사용할 때의 문제
    안녕하세요. 주니어 백엔드 개발자 입니다.
    안녕하세요. 주니어 백엔드 개발자 입니다.

    티스토리툴바