Nexus 공식 문서 요약 및 핵심 사용법
Nexus는 GraphQL 스키마를 TypeScript로 정의하는 도구로, 타입 안정성과 개발자 경험을 중시합니다.
아래는 주요 개념과 사용법을 구조화한 안내입니다.
// schema.ts
import { objectType, queryType, makeSchema } from 'nexus';
// 1. 타입 정의
const User = objectType({
name: 'User',
definition(t) {
t.string('id');
t.string('name');
}
});
// 2. 쿼리 정의
const Query = queryType({
definition(t) {
t.list.field('users', {
type: 'User',
resolve: () => [{ id: '1', name: 'Alice' }]
});
}
});
// 3. 스키마 생성
export const schema = makeSchema({
types: [User, Query],
outputs: {
schema: __dirname + '/generated/schema.graphql',
typegen: __dirname + '/generated/typings.ts'
}
});
nexus generate로 GraphQL 스키마와 TypeScript 타입 동기화.npm install nexus graphql @prisma/client
npx nexus create
nexus.config.ts에서 빌드 옵션 설정 가능.| 도구 | 장점 | 단점 |
|---|---|---|
| Nexus | 타입 안정성, 코드 퍼스트 | 학습 곡선 |
SDL (graphql-tools) |
간단함 | 타입 중복 |
| Pothos | 유연성 | 복잡성 |