[node.js] Apollo-server (cf. GraphQL)

Hyo Kyun Lee·2021년 8월 2일
0

node.js

목록 보기
33/34

1. apollo-server module

GraphQL과 마찬가지로 Query문을 통해 data 및 여러 정보를 얻을 수 있는 UI이다.

다만 GraphQL과 Query문 작성구조나 문법이 약간 다르다.

또한 GraphQL은 요구하는 module이 상대적으로 적은데, apollo server의 경우 Query 작성이 다소 복잡하고, 요구하는 module 및 사용객체(비구조화 방식)이 복잡한 편이다.

2. apollo-server module 활용방안

GraphQL schema 정하기

  • apollo-server module을 비구조화하여 Aplloserver, gql class를 선언한다.
  • gql class 내부의 객체를 정의하여 typeDefs 변수에 저장한다.
  • gql 객체는 data source로 활용될 모든 table의 column을 구조화하여 type에 저장, type에 저장된 table data들을 얻기위해 활용할 특정 Query문을 type Query에 저장한다.

data set

  • 위 gql 객체 중 특정 Query 문으로 활용한 type query에 어떤 data가 들어가는지 set한다.
  • 이 set 구성은 마찬가지로 gql에서 정의한 type의 table column 구성과 동일해야 한다.

Resolver(반환자) 정하기

  • gql에서 특정 Query문을 동작했을때 data를 반환받을 수 있도록 반환자 logic을 정의해야 한다.
  • 이 logic은 함수처럼 정의해야하고, 이를 resolvers 반환자에 저장해놓는다.

ApolloServer 객체를 생성하기

  • 위에서 정의한 typeDefs, resolvers를 인자로 전달해준다.
  • 여기서 생성한 server는 Graphql과 같이 UI화된 것이고, 앞서 정의한 Query문을 통해 data를 얻을 수 있다.
  • GraphQL과 마찬가지로 모든 데이터가 graph화(연결)되어있다는 전제이다.

3. 참조링크

apollo server documents
https://www.apollographql.com/docs/apollo-server/api/apollo-server/

apollo server modules의 활용
https://www.apollographql.com/docs/apollo-server/getting-started/#step-3-define-your-graphql-schema

0개의 댓글