Files
2nd/10_Wiki/Topic_Programming/Frontend/GraphQL-Code-Generator.md
T
Antigravity Agent 9148c358d0 docs(10_Wiki): 위키 전체 재구성 — Topic_* 폴더를 4개 카테고리로 통합 + 대규모 중복 제거
Topic_Agent/Topic_Blog/Topics/Topics_Biz/Topics_Meeting/Topics_Rag의 마크다운 지식 문서를
Topic_General/Topic_Programming/Topic_Graphic/Topic_Business 4개 카테고리로 재분류.

- 중복 제거: frontmatter의 status:duplicate/merged + duplicate_of/redirect_to 필드로
  자기 자신을 중복으로 선언한 리다이렉트 stub 1032개 제거, 완전 동일 내용 파일 472개 제거,
  동일 파일명·다른 내용 충돌 시 더 큰(완전한) 버전만 유지(162개 제거) — 총 1639개 중복 제거.
- 분류: 폴더 단위로 명확한 항목(AI_and_ML/Coding/Architecture 등 → Programming,
  Comfyui/Visual_Effects → Graphic, Topics_Biz/Topics_Meeting/사업 등 → Business,
  Poetic_Blog_Writing/창의성/Game_Design 등 → General)은 폴더 우선순위로,
  나머지 혼재 폴더(Topic_Agent/Topic_Blog/Topics 루트/Thinking & Reasoning/Other/UI_UX_Assets)는
  title/tags 키워드 스코어링으로 파일 단위 분류(불명확한 경우 General로 폴백).
  원본 폴더명은 "From_*" 서브폴더로 보존해 추적 가능성 유지.
- 최종 배치: Programming 2784 / General 1608 / Graphic 285 / Business 249 = 4926개 문서.
- 에이전트 운영 상태(.astra/.agent/.obsidian/sessions/memory/_company/docs/lessons/_shared/src)는
  지식 콘텐츠가 아니므로 재분류 대상에서 제외하고 원위치 유지.
- Topics/Topic_email(상위 보호 폴더 Topic_email과 파일명 100% 중복) 삭제 — 보호 폴더 자체는 미변경.
- 완전히 비게 된 Topic_Agent/Topic_Blog/Topics_Biz/Topics_Rag 폴더 제거.
2026-07-05 00:33:48 +09:00

192 lines
5.3 KiB
Markdown

---
id: wiki-2026-0508-graphql-code-generator
title: GraphQL Code Generator
category: 10_Wiki/Topics
status: verified
canonical_id: self
aliases: [graphql-codegen, GQL Codegen, Type-safe GraphQL]
duplicate_of: none
source_trust_level: A
confidence_score: 0.9
verification_status: applied
tags: [graphql, typescript, codegen, type-safety]
raw_sources: []
last_reinforced: 2026-05-10
github_commit: pending
tech_stack:
language: typescript
framework: graphql
---
# GraphQL Code Generator
## 매 한 줄
> **"매 schema → typed client 의 자동화"**. `.graphql` schema + operation 으로부터 TypeScript types, hook, fragment 의 생성. 매 schema drift 의 compile-time 차단 — `any` 의 X, `User.email` typo 의 즉시 error.
## 매 핵심
### 매 핵심 plugin
- **typescript**: schema → TS types (scalar, enum, input, object).
- **typescript-operations**: query/mutation operation → typed result.
- **typed-document-node**: TypedDocumentNode (apollo-client / urql 의 input).
- **client-preset** (modern, 2026 default): 매 fragment-masking, persisted-query 의 통합 preset.
### 매 fragment masking
- Component A 가 fragment X 정의 → component B 가 fragment X 의 field 접근 시 compile error.
- 매 over-fetching 의 prevention 강제.
### 매 응용
1. React + Apollo / urql 매 typed hook 자동 생성.
2. Backend schema change 시 client compile error 즉시 감지.
3. Persisted queries (production safety, query whitelisting).
## 💻 패턴
### codegen.ts (client-preset)
```typescript
import { CodegenConfig } from '@graphql-codegen/cli';
const config: CodegenConfig = {
schema: 'https://api.example.com/graphql',
documents: ['src/**/*.{ts,tsx}', '!src/gql/**/*'],
generates: {
'./src/gql/': {
preset: 'client',
presetConfig: {
fragmentMasking: { unmaskFunctionName: 'getFragmentData' },
},
config: {
useTypeImports: true,
scalars: { DateTime: 'string', UUID: 'string' },
},
},
},
hooks: { afterAllFileWrite: ['prettier --write'] },
};
export default config;
```
```bash
pnpm graphql-codegen --watch
```
### Operation — typed result
```typescript
// src/components/UserCard.tsx
import { graphql } from '../gql';
import { useQuery } from '@apollo/client';
const USER_QUERY = graphql(`
query GetUser($id: ID!) {
user(id: $id) { id name email avatarUrl }
}
`);
export function UserCard({ id }: { id: string }) {
const { data } = useQuery(USER_QUERY, { variables: { id } });
// 매 data?.user 의 type 의 fully inferred
return <div>{data?.user?.name}</div>;
}
```
### Fragment masking
```typescript
const USER_AVATAR_FRAGMENT = graphql(`
fragment UserAvatar on User { avatarUrl name }
`);
function Avatar({ user }: { user: FragmentType<typeof USER_AVATAR_FRAGMENT> }) {
const u = getFragmentData(USER_AVATAR_FRAGMENT, user);
return <img src={u.avatarUrl} alt={u.name} />;
}
// parent — 매 user 의 email 의 access X (fragment 의 declare X)
function Parent({ user }) {
return <Avatar user={user} />;
// user.email 의 access 시 TS error
}
```
### Persisted queries
```typescript
{
generates: {
'./persisted-operations.json': {
preset: 'client',
presetConfig: { persistedDocuments: true },
},
},
}
```
```typescript
// runtime — query string 의 X, hash 만 전송
import { createPersistedQueryLink } from '@apollo/client/link/persisted-queries';
const link = createPersistedQueryLink({ generateHash: doc => doc['__meta__']['hash'] });
```
### Custom scalar mapping
```typescript
config: {
scalars: {
DateTime: 'string', // ISO 8601
JSON: 'Record<string, unknown>',
BigInt: 'string',
},
}
```
### Watch + CI
```json
{
"scripts": {
"codegen": "graphql-codegen",
"codegen:watch": "graphql-codegen --watch",
"ci:codegen-check": "graphql-codegen && git diff --exit-code src/gql/"
}
}
```
### Multi-schema (federation)
```typescript
{
schema: ['./schema/users.graphql', './schema/products.graphql'],
// 매 federated graph 의 single typed client
}
```
## 매 결정 기준
| 상황 | Approach |
|---|---|
| 새 React + GraphQL | client-preset + fragment masking |
| Apollo Client (legacy) | typescript + typescript-react-apollo |
| urql | client-preset (urql 의 native 지원) |
| Production 보안 | persisted queries 의 enable |
| Backend schema 의 evolve | CI 의 codegen drift check |
**기본값**: client-preset + fragmentMasking, CI 의 codegen drift check.
## 🔗 Graph
- 부모: [[TypeScript]]
- Adjacent: [[OpenAPI-Codegen]]
## 🤖 LLM 활용
**언제**: GraphQL schema 가 있는 TS project, multi-team 의 schema drift 방지, persisted query 도입.
**언제 X**: REST-only, 매 GraphQL schema 의 unstable 한 prototype phase.
## ❌ 안티패턴
- **codegen output 의 manual edit**: 매 next run 시 overwrite, 매 변경 의 lost.
- **fragment 의 component 외 정의**: fragment masking 의 weak — co-location 강제.
- **DateTime 의 `Date` mapping**: GraphQL response 는 string, 매 runtime mismatch 유발.
- **CI 에 codegen drift check 의 X**: schema 의 silent breakage.
## 🧪 검증 / 중복
- Verified (graphql-code-generator.com docs, client-preset migration guide).
- 신뢰도 A.
## 🕓 Changelog
| 날짜 | 변경 |
|---|---|
| 2026-05-08 | Phase 1 |
| 2026-05-10 | Manual cleanup — GraphQL Codegen client-preset 패턴 정리 |