1. 서버 구성요소 모듈화
- 3-1-server-modularized 열기
-
🚨 lodash 모듈 설치
npm i lodash
apollo-server 생성자 인자 모듈화
- apollo-server 문서 참조
- typeDefs: 단일 변수 또는 배열로 지정 가능
- resolvers: 단일 Object 또는 Merge 된 배열로 가능
typedefs-resolvers/_queries.js
// ...
const typeDefs = gql`
type Query {
equipments: [Equipment]
}
`
// ...
typedefs-resolvers/_mutations.js
// ...
const typeDefs = gql`
type Mutation {
deleteEquipment(id: String): Equipment
}
`
// ...
typedefs-resolvers/equipments.js
// ...
const typeDefs = gql`
type Equipment {
id: String
used_by: String
count: Int
new_or_used: String
}
`
const resolvers = {
Query: {
equipments: (parent, args) => dbWorks.getEquipments(args),
},
Mutation: {
deleteEquipment: (parent, args) => dbWorks.deleteItem('equipments', args),
}
}
// ...
typedefs-resolvers/index.js
// ...
const queries = require('./typedefs-resolvers/_queries')
const mutations = require('./typedefs-resolvers/_mutations')
const equipments = require('./typedefs-resolvers/equipments'
// ...
const typeDefs = [
queries,
mutations,
equipments.typeDefs,
]
const resolvers = [
equipments.resolvers
]
// ...
dbWorks.js 살펴보기
- Resolver에 사용할 기능들 모듈화
Supply 모듈 추가해보기
typedefs-resolvers/supplies.js
const typeDefs = gql`
type Supply {
id: String
team: Int
}
`
const resolvers = {
Query: {
supplies: (parent, args) => dbWorks.getSupplies(args)
},
Mutation: {
deleteSupply: (parent, args) => dbWorks.deleteItem('supplies', args),
}
}
typedefs-resolvers/_queries.js
// ...
const typeDefs = gql`
type Query {
...
supplies: [Supply]
}
`
// ...
typedefs-resolvers/_mutations.js
// ...
const typeDefs = gql`
type Mutation {
...
deleteSupply: [Supply]
}
`
// ...
typedefs-resolvers/index.js
// ...
const supplies = require('./typedefs-resolvers/supplies')
// ...
const typeDefs = [
// ...
supplies.typeDefs
]
const resolvers = [
// ...
supplies.resolvers
]
// ...