โœ๐Ÿป [Code Camp_TIL] 10์ผ์ฐจ: ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ ์œ ํ‹ธ๋ฆฌํ‹ฐ ํƒ€์ž…, API ์‘๋‹ต ๋ฐ์ดํ„ฐ ํƒ€์ž…

code_Jยท2023๋…„ 4์›” 2์ผ
0

TIL

๋ชฉ๋ก ๋ณด๊ธฐ
15/41
post-thumbnail

์œ ํ‹ธ๋ฆฌํ‹ฐ ํƒ€์ž…


๊ธฐ์กด ํƒ€์ž…์„ ์กฐ์ž‘ํ•ด์„œ ํ•„์š”ํ•œ ํƒ€์ž…๋งŒ ๋”ฐ๋กœ ๋นผ๋‘๊ฑฐ๋‚˜, ํ•„์š”์—†๋Š” ํƒ€์ž…๋งŒ ์ œ๊ฑฐํ•˜๋Š” ๊ฒƒ์„ ์˜๋ฏธํ•œ๋‹ค. ๋งŽ์ด ์‚ฌ์šฉ๋˜๋Š” ์œ ํ‹ธ๋ฆฌํ‹ฐ ํƒ€์ž…์˜ ๊ธฐ๋Šฅ๋“ค์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.


export interface IProfile {
  name: string;
  age: number;
  school: string;
  hobby?: string;
}

  
// 1. Partial ํƒ€์ž…: ์ „์ฒด ๋ฌผ์Œํ‘œ ๋ถ™์—ฌ์ฃผ๊ธฐ

type aaa = Partial<IProfile>;


// 2. Required ํƒ€์ž…: ์ „์ฒด ๋ฌผ์Œํ‘œ ์‚ญ์ œ

type bbb = Required<IProfile>;


// 3. Pick ํƒ€์ž…: ํŠน์ • ๊ฐ’ ๊ณ ๋ฅด๊ธฐ

type ccc = Pick<IProfile, "name" | "age">;


// 4. Omit ํƒ€์ž…: ํŠน์ • ๊ฐ’ ๋นผ๊ธฐ

type ddd = Omit<IProfile, "school">;


// 5. Record ํƒ€์ž…: ๊ฐœ๋ณ„ record์— ๋‚ด๊ฐ€ ์›ํ•˜๋Š” ๊ฒƒ ์ง€์ •.

type eee = "์ฒ ์ˆ˜" | "์˜ํฌ" | "ํ›ˆ์ด"; 
  // Union ํƒ€์ž…: string ํƒ€์ž…๋ณด๋‹ค ๋” ์—„๊ฒฉํ•จ.
let child1: eee = "์ฒ ์ˆ˜"; 
  // ์ฒ ์ˆ˜, ์˜ํฌ, ํ›ˆ์ด๋งŒ ๋จ.
let child2: string = "์‚ฌ๊ณผ"; 
  // ์ฒ ์ˆ˜, ์˜ํฌ, ํ›ˆ์ด, ์‚ฌ๊ณผ, ๋ฐ”๋‚˜๋‚˜ ๋“ฑ๋“ฑ ๋‹ค ๋จ.
type fff = Record<eee, IProfile>; 
// ์ฒ ์ˆ˜, ์˜ํฌ, ํ›ˆ์ด์— ๊ฐ๊ฐ IProfile ๋งตํ•‘


// 6. ๊ฐ์ฒด์˜ key๋“ค๋กœ Union ํƒ€์ž… ๋งŒ๋“ค๊ธฐ

type ggg = keyof IProfile; 
  // "name" | "age" | "school" | "hobby" ๋งŒ๋“ค์–ด์ง.
let myprofile: ggg = "hobby";

// 7. type vs interface ์ฐจ์ด  =>  interface๋Š” ์„ ์–ธ๋ณ‘ํ•ฉ ๊ฐ€๋Šฅ(๋‘ ๊ฐœ์˜ ์ธํ„ฐํŽ˜์ด์Šค ํ•ฉ์ฒด ๊ฐ€๋Šฅ)
export interface IProfile {
  candy: number; // ์„ ์–ธ๋ณ‘ํ•ฉ์œผ๋กœ ์ถ”๊ฐ€๋จ.
}

// 8. ๋ฐฐ์šด ๊ฒƒ ์‘์šฉ
let profile: Partial<IProfile> = {
  candy: 10,
};

API ์‘๋‹ต ๋ฐ์ดํ„ฐ ํƒ€์ž…


rest-api๋Š” ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ๋ฅผ ์ ์šฉํ•˜๋ฉด ์ผ์ผ์ด ์†์œผ๋กœ ๋งŒ๋“ค์–ด ์ค˜์•ผ ํ•˜๋Š” ๋ถ€๋ถ„์ด ๋งŽ๋‹ค. graphql-api ๊ฐ™์€ ๊ฒฝ์šฐ graphql-codegen์„ ์ด์šฉํ•ด ๋ช…๋ น์–ด ํ•˜๋‚˜๋กœ ๋งŒ๋“ค์–ด์ค€๋‹ค. ๋”ฐ๋ผ์„œ ๊ต‰์žฅํžˆ ๊ฐ„ํŽธํ•˜๊ฒŒ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค.


graphql-codegen


graphql-codegen ์„ค์น˜


graphql-codegen์„ ์„ค์น˜ํ•ด์ฃผ๊ณ (yarn์œผ๋กœ ์„ค์น˜ํ•ด์คŒ), yml ํŒŒ์ผ์„ ๋งŒ๋“ค์–ด์„œ ์„ค์ •์„ ํ•ด์คฌ๋‹ค. ์ดํ›„์— ํ„ฐ๋ฏธ๋„ ๋ช…๋ น์–ด๋กœ codegen์„ ์‹คํ–‰์‹œํ‚ค๋ฉด yml ํŒŒ์ผ์—์„œ ์„ค์ •ํ•ด๋’€๋˜ schema์˜ ์ฃผ์†Œ๋กœ ๋“ค์–ด๊ฐ€ api๋“ค์˜ ํƒ€์ž…์„ ์ญ‰ ๋‹ค์šด๋ฐ›์•„์˜จ๋‹ค.

์„ค์น˜์™€ ์„ค์ •์„ ์™„๋ฃŒํ•˜๊ณ , yarn generate ๋ช…๋ น์„ ์‹คํ–‰ํ•˜๋ฉด, ๋ฐฑ์—”๋“œ ์ปดํ“จํ„ฐ์— ์ ‘์†ํ•ด์„œ graphql-api๋กœ ๋ฐ›์•„์˜ค๋Š” ๋ชจ๋“  ๋ฐ์ดํ„ฐ์˜ ํƒ€์ž…์„ ๋น ๋ฅด๊ฒŒ ์กฐ์‚ฌํ•˜๊ณ , ์ด์™€ ์ผ์น˜ํ•˜๋Š” ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ ํŒŒ์ผ์„ ์ž๋™์œผ๋กœ ๋งŒ๋“ค์–ด์ค€๋‹ค.


mutation์— ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ ์ ์šฉ


์•„๋ž˜์˜ ํ˜•์‹์œผ๋กœ ์ ์šฉ์‹œํ‚ฌ ์ˆ˜ ์žˆ๋‹ค.
const [ํ•จ์ˆ˜] = useMutation<๊ฒฐ๊ณผํƒ€์ž…,๋ณ€์ˆ˜ํƒ€์ž…>(์ฟผ๋ฆฌ);

๋‹ค์šด๋ฐ›์•„์˜จ ํƒ€์ž… ์ค‘์— ๋งž๋Š” ๊ฒƒ์„ ๋ฝ‘์•„ ์ ์–ด์ฃผ๋ฉด ๋œ๋‹ค.
// types.ts ํŒŒ์ผ์—์„œ ๋‹ค์šด๋กœ๋“œ๋œ ํƒ€์ž…๋“ค ์ค‘ ํ•ด๋‹น ํƒ€์ž…์„ importํ•ด์™€์•ผ ํ•จ
import {
  IMutation,
  IMutationCreateBoardArgs,
} from "../../../src/commons/types/generated/types";

// <>๋ฅผ ์ด์šฉํ•ด์„œ ํƒ€์ž…์„ ์ง€์ •
// ํƒ€์ž…ํŒŒ์ผ์—์„œ IMutation ์ค‘ createBoard๋ฅผ pick, IMutationCreateBoardArgs๋ฅผ import
export default function GraphqlMutationPage() {
  const [๋‚˜์˜ํ•จ์ˆ˜] = useMutation<
    Pick<IMutation, "createBoard">,
    IMutationCreateBoardArgs
  >(๋‚˜์˜๊ทธ๋ž˜ํ”„ํ์—˜์…‹ํŒ…)

query์— ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ ์ ์šฉ

query์— ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ๋ฅผ ์ ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•๋„ mutation๊ณผ ํฌ๊ฒŒ ๋‹ค๋ฅด์ง€ ์•Š๋‹ค.

import {
  IQuery,
  IQueryFetchBoardArgs,
} from "../../../src/commons/types/generated/types";

const { data } = useQuery<Pick<IQuery,"fetchBoard">,IQueryFetchBoardArgs>(FETCH_BOARD,{
		variables: {number: Number(router.query.mynumber)}
	})



์˜ค๋Š˜์€ ์œ ํ‹ธ๋ฆฌํ‹ฐ ํƒ€์ž…๊ณผ graphql api์˜ ํƒ€์ž…์„ ์ง€์ •ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ๊ณต๋ถ€ํ–ˆ๋‹ค. ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ๋ฅผ ์“ฐ๊ธฐ ์‹œ์ž‘ํ•˜๋ฉด์„œ, ๋ณ€์ˆ˜์˜ ํƒ€์ž…์„ ์ผ์ผ์ด ์ง€์ •ํ•ด์ฃผ๋Š” ๊ฒŒ ๋ฒˆ๊ฑฐ๋กญ๊ธด ํ•˜์ง€๋งŒ, ๋‚˜์ค‘์— ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ๋Š” ์—๋Ÿฌ๋ฅผ ๊ณ ๋ คํ•˜๋ฉด ๊ผญ ํ•„์š”ํ•œ ๊ฒƒ ๊ฐ™๋‹ค. ์ง€๊ธˆ๊นŒ์ง€ ๋งŒ๋“ค์–ด๋†“์€ js ํŒŒ์ผ์„ ts/tsx๋กœ ๋ฐ”๊ฟ”๋ณด๋ฉด์„œ ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ๋ฅผ ์“ฐ๋Š” ์—ฐ์Šต์„ ํ•ด์•ผ๊ฒ ๋‹ค!



profile
Web FE ๊ฐœ๋ฐœ์ž ์ทจ์ค€์ƒ

0๊ฐœ์˜ ๋Œ“๊ธ€