📚 GraphQL과 Apollo
Chapter 3. GraphQL 서버 깊이 파보기
Lesson 3. 유니언과 인터페이스

3. 유니언과 인터페이스

  • 3-3-union-interface 새로 열기

Union

  • 타입 여럿을 한 배열에 반환하고자 할 때 사용

EquipmentSupply를 함께 반환하기

givens.js

const { gql } = require('apollo-server') const dbWorks = require('../dbWorks.js') const typeDefs = gql` union Given = Equipment | Supply ` const resolvers = { Query: { givens: (parent, args) => { return [ ...dbWorks.getEquipments(args), ...dbWorks.getSupplies(args) ] } }, Given: { __resolveType(given, context, info) { if (given.used_by) { return 'Equipment' } if (given.team) { return 'Supply' } return null } } } module.exports = { typeDefs: typeDefs, resolvers: resolvers }

_queries.js

const typeDefs = gql` type Query { // ... givens: [Given] } `

index.js

// ... const givens = require('./typedefs-resolvers/givens') // ... const typeDefs = [ // ... givens.typeDefs ] // ... const resolvers = [ // ... givens.resolvers ] // ...
query { givens { __typename ... on Equipment { id used_by count new_or_used } ... on Supply { id team } } }


Interface

  • 유사한 객체 타입을 만들기 위한 공통 필드 타입
  • 추상 타입 - 다른 타입에 implement 되기 위한 타입

type Equipment { id: ID! used_by: Role! count: Int new_or_used: NewOrUsed! } ... type Software { id: ID! used_by: Role! developed_by: String! description: String }
  • 공통적으로 가진 필드: id, used_by

tools.js

const { gql } = require('apollo-server') const typeDefs = gql` interface Tool { id: ID! used_by: Role! } ` const resolvers = { Tool: { __resolveType(tool, context, info) { if (tool.developed_by) { return 'Software' } if (tool.new_or_used) { return 'Equipment' } return null } } } module.exports = { typeDefs: typeDefs, resolvers: resolvers }

equipments.js

type Equipment implements Tool { id: ID! used_by: Role! count: Int new_or_used: NewOrUsed! }

equipments.js

type Software implements Tool { id: ID! used_by: Role! developed_by: String! description: String }

index.js

// ... const tools = require('./typedefs-resolvers/tools') // ... const typeDefs = [ // ... tools.typeDefs ] // ... const resolvers = [ // ... tools.resolvers ] // ...
query { equipments { id used_by count new_or_used } softwares { id used_by description developed_by } }

People 쿼리에 적용

people.js

const { gql } = require('apollo-server') const dbWorks = require('../dbWorks.js') const typeDefs = gql` type People { id: ID! first_name: String! last_name: String! sex: Sex! blood_type: BloodType! serve_years: Int! role: Role! team: ID! from: String! tools: [Tool] givens: [Given] } ` const resolvers = { Query: { people: (parent, args) => dbWorks.getPeople(args), person: (parent, args) => dbWorks.getPeople(args)[0] } } module.exports = { typeDefs: typeDefs, resolvers: resolvers }

_queries.js

const typeDefs = gql` type Query { people: [People], // ... } `

index.js

// ... const people = require('./typedefs-resolvers/people') // ... const typeDefs = [ // ... people.typeDefs ] // ... const resolvers = [ // ... people.resolvers ] // ...
query { people { id first_name last_name givens { __typename ... on Equipment { id used_by count new_or_used } ... on Supply { id team } } tools { __typename ... on Equipment { id used_by count new_or_used } ... on Software { id used_by description developed_by } } } }
📚 GraphQL과 Apollo