typedef, resolver 구조잡기

김한울·2020년 4월 3일
0

prisma-graphql-yoga

목록 보기
3/3
post-thumbnail

이번 포스팅에서는 typedefresolver 구조를 구성하는 방법을 알아보도록 하겠습니다.

패키지 설치

yarn add merge-graphql-schemas

디렉토리 구조

gql
|
+--User
    +--login
         +--login.ts
         +--login.gql
+--User.gql
schema.ts
server.ts

위와 같이 목적별로 디렉토리를 나누고, 해당 목적에 맞는 API들에 맞는 resolvertypedef 를 추가하였습니다. typescript 끼리, resolver 끼리 묶어서 사용하는 패턴도 있으니 편하신 대로 이용하면 좋을 것 같습니다.

Sample resolver 생성하기

Util/healthCheck/healthCheck.ts

export default {
  Query: {
    hello: (_, { name }) => `Hello ${name || "World"}`
  }
};

Util/healthCheck/healthCheck.gql

type Query {
  hello(name: String): String!
}

graphql-yoga 설정파일 변경

import { resolve } from "path";
import { config } from "dotenv";
import { GraphQLServer } from "graphql-yoga";
import schema from "./schema";
import * as logger from "morgan";

config({ path: resolve(__dirname, "../.env") });
const PORT = process.env.PORT;

const server = new GraphQLServer({ schema });
server.start({ port: PORT }, () =>
  console.log(`Server is running on http://localhost:${PORT}`)
);
server.express.use(logger("dev"));

전체코드는 아래 github에서 확인하실 수 있습니다.
https://github.com/toy-crane/graphql-yoga-auth-example

profile
적정 기술에 대해 고민합니다.

0개의 댓글