Notice
Recent Posts
Recent Comments
Link
«   2026/08   »
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31
Tags
more
Archives
Today
Total
관리 메뉴

나의 개발 일상 기록

[API] GraphQL 유니온과 인터페이스 본문

API

[API] GraphQL 유니온과 인터페이스

느린 거북이 2021. 7. 10. 17:03

GraphQL Union과 Interface

 

1. GraphQL Union

 

GraphQL 유형 시스템은 공용 구조체입니다. 공용 구조체는 필드의 공통 필드 집합을 정의하지 않는다는 점을 제외하면 인터페이스와 동일합니다. 가능한 형식이 논리적 계층 구조를 공유하지 않는 경우 공용 구조체는 일반적으로 인터페이스보다 우선합니다.

 

* Union은 타입 여럿을 한 배열에 반환하고자 할때 사용합니다.

 

  • GraphQL Union 사용 예시
const { gql } = require('apollo-server')
const dbWorks = require('../dbWorks.js')
const typeDefs = gql`
    union Given = Equipment | Supply  // 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
}

Union의 query를 보낼 경우

query {
  givens {
    __typename
    ... on Equipment {
      id
      used_by
      count
      new_or_used
    }
    ... on Supply {
      id
      team
    }
  }
}

 

 

2. GraphQL Interface

 

인터페이스는 인터페이스 구현을 위해 형식에 포함해야 하는 특정 필드 집합을 노출시킵니다. 유사한 객체 타입을 만들기 위한 공통 필드 타입이면서 추상Type입니다. 다른 타입에  implement되기 위한 타입 입니다.

 

  • GraphQL Interface 사용 예시

공통적으로 가진 필드 : id, used_by

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
}

 

Interface 사용예시

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
}

Interface의 query를 보낼 경우

query {
  equipments {
    id
    used_by
    count
    new_or_used
  }
  softwares {
    id
    used_by
    description
    developed_by
  }
}

 

* Unoin과 Interface를 혼용해서 query를 보낼 경우

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
      }
    }
  }
}

 

'API' 카테고리의 다른 글

[API] GraphQL의 기본 타입들  (0) 2021.07.10
[API] GraphQL Query & Mutation  (0) 2021.07.10
[API] GraphQL 서버 만들기  (0) 2021.07.09
[API] GraphQL & Apllo  (0) 2021.07.09
카카오 지도 OPEN API  (0) 2021.03.05
Comments