allow unions to be used in connections

This commit is contained in:
Tony Angerilli 2016-11-10 16:48:54 -08:00
parent 6c7cd4e4fa
commit 03898007a5
2 changed files with 17 additions and 2 deletions

View File

@ -12,6 +12,7 @@ from ..types import (AbstractType, Boolean, Enum, Int, Interface, List, NonNull,
from ..types.field import Field
from ..types.objecttype import ObjectType, ObjectTypeMeta
from ..types.options import Options
from ..types import Union
from ..utils.is_base_type import is_base_type
from ..utils.props import props
from .node import is_node
@ -109,7 +110,7 @@ class IterableConnectionField(Field):
@property
def type(self):
type = super(IterableConnectionField, self).type
if is_node(type):
if issubclass(type, Union) or is_node(type):
connection_type = type.Connection
else:
connection_type = type

View File

@ -24,7 +24,21 @@ class UnionMeta(type):
len(options.types) > 0
), 'Must provide types for Union {}.'.format(options.name)
return type.__new__(cls, name, bases, dict(attrs, _meta=options))
cls = type.__new__(cls, name, bases, dict(attrs, _meta=options))
get_connection = getattr(cls, 'get_connection', None)
if not get_connection:
from graphene.relay.connection import Connection
class DefaultUnionConnection(Connection):
class Meta:
node = cls
cls.Connection = DefaultUnionConnection
else:
cls.Connection = get_connection()
return cls
def __str__(cls): # noqa: N805
return cls._meta.name