parent : 이 필드의 부모(즉, resolver 체인의 이전 resolver)에 대한 resolver의 반환 값임
#parent 출력 예시
parent {
posts: [{
id: 'post1',
title: 'It is a first post',
description: 'It is a first post description',
comments: [Array]},
{
id: 'post2',
title: 'It is a second post',
description: 'It is a second post description',
comments: []}],
comments: [{
id: 'comment1',
text: 'It is a first comment',
likes: 1}]}
args : 이 필드에 제공된 모든 GraphQL 인수를 포함하는 객체로서 해당 값을 통해 필터링을 함
#graphql 쿼리{posts(postID:1){...}}
위와 같은 GraphQL 쿼리에서 전달한 인자가 포함됨
#args 출력 예시
args {}
context : 특정 작업에 대해 실행 중인 모든 resolver 간에 공유되는 object로서 인증 정보, 데이터 로더 인스턴스 및 resolver에서 추적할 기타 항목을 포함하여 작업별 상태를
공유하는데 사용함
분리된 graphql 타입 파일들을 한 번에 모아주는 graphql-tools/load-files 모듈의 loadFilesSync()를 사용한 것과 같이 동일하게 분리한 resolver 파일들을 한
번에 가져오기 위해 loadFilesSync() 함수 사용
// server.jsconst{ loadFilesSync }=require("@graphql-tools/load-files");const path =require("path");// 절대 경로를 이용해 가져오기const loadedResolvers =loadFilesSync(path.join(__dirname,"**/*.resolvers.js"));const schema =makeExecutableSchema({typeDefs: loadedTypes,resolvers: loadedResolvers,});
- 로직을 model 함수에서 만들어 처리하기
resolver 파일은 최대한 간단하게 만드는 것이 좋음
따라서 resolver 함수 안의 로직을 model 모듈 파일에서 작성해서 처리하기
// posts/posts.model.jsconst posts =[{id:"post1",title:"It is a first post",description:"It is a first post description",comments:[{id:"comment1",text:"It is a first comment",likes:1,},],},{id:"post2",title:"It is a second post",description:"It is a second post description",comments:[],},];// 모든 포스트들을 가져오는 함수(로직) 생성functiongetAllPosts(){return posts;}// 함수 내보내기
module.exports ={
getAllPosts,};
// comments/comments.model.jsconst comments =[{id:"comment1",text:"It is a first comment",likes:1,},];functiongetAllComments(){return comments;}
module.exports ={
getAllComments,};
- resolver 파일로 model 파일에 작성한 로직 가져오기
원래는 resolver 함수에서 root 데이터인 parent를 인자로 받아서 사용했지만 model 파일 자체에서 로직에서 데이터를 처리하기에 parent 인자가 필요없어짐