Nexus 공식 문서 요약 및 핵심 사용법

Nexus는 GraphQL 스키마를 TypeScript로 정의하는 도구로, 타입 안정성과 개발자 경험을 중시합니다.

아래는 주요 개념과 사용법을 구조화한 안내입니다.


📜 Nexus 공식 문서 핵심 내용

1. 기본 구조

// 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'
  }
});

2. 주요 기능

3. 설치 및 설정

npm install nexus graphql @prisma/client
npx nexus create


📌 Nexus vs Alternatives

도구 장점 단점
Nexus 타입 안정성, 코드 퍼스트 학습 곡선
SDL (graphql-tools) 간단함 타입 중복
Pothos 유연성 복잡성

🚀 실제 프로젝트 적용 단계

  1. 스키마 정의 → 2. 리졸버 연결 → 3. 서버 연동 (예: Apollo Server)