graphene-python学习笔记(7)Unions

1、Unions

Unions和接口有点像,只是他不定义公共的字段。

基础:

(1)每一个Union都是一个python class继承了graphene.Union

(2)Unions没有任何字段,仅仅是把Union连接在一起

示例

import graphene

class Human(graphene.ObjectType):
    name = graphene.String()
    born_in = graphene.String()

class Droid(graphene.ObjectType):
    name = graphene.String()
    primary_function = graphene.String()

class Starship(graphene.ObjectType):
    name = graphene.String()
    length = graphene.Int()

class SearchResult(graphene.Union):
    class Meta:
        types = (Human, Droid, Starship)

  Union无法接受Union和Interface作为联合的对象。这样任何需要返回SearchResult的请求都会从Human,Driod,StartShip中返回一个

上面的示例代表的schema:

type Droid {
  name: String
  primaryFunction: String
}

type Human {
  name: String
  bornIn: String
}

type Ship {
  name: String
  length: Int
}

union SearchResult = Human | Droid | Starship

  

posted @ 2018-08-30 11:22  tutu_python  阅读(247)  评论(0)    收藏  举报