← 글 목록

notes

Nexus

Nexus 도구의 역할·설정과 서버 개발 적용 포인트 메모.

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. 주요 기능

  • 타입 자동 생성: nexus generate로 GraphQL 스키마와 TypeScript 타입 동기화.
  • 플러그인 시스템: Prisma, Auth 등과의 통합 지원.
  • 개발 서버: 파일 변경 시 자동 리로드.

3. 설치 및 설정

npm install nexus graphql @prisma/client
npx nexus create
  • nexus.config.ts에서 빌드 옵션 설정 가능.

📌 Nexus vs Alternatives


🚀 실제 프로젝트 적용 단계

  • 스키마 정의 → 2. 리졸버 연결 → 3. 서버 연동 (예: Apollo Server)
// server.ts
import { ApolloServer } from 'apollo-server';
import { schema } from './schema';

const server = new ApolloServer({ schema });
server.listen().then(({ url }) => console.log(`Server ready at ${url}`));

⚠️ 주의사항

  • Next.js API Routes 사용 시:
    // pages/api/graphql.ts
    import { startServerAndCreateNextHandler } from '@as-integrations/next';
    import { schema } from '../../schema';
    
    export default startServerAndCreateNextHandler(server);
    
  • Prisma 연동 시: nexus-plugin-prisma 필수.

🔗 공식 자료

💡 Tip: nexus dev로 실시간 개발 모드 실행 가능.

특정 사용 사례가 있으면 알려주세요! 구체적인 코드로 안내드립니다.