Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion graphene/relay/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
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
Expand Down Expand Up @@ -109,7 +110,7 @@ def __init__(self, type, *args, **kwargs):
@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
Expand Down
24 changes: 0 additions & 24 deletions graphene/tests/issues/test_356.py

This file was deleted.

32 changes: 32 additions & 0 deletions graphene/types/tests/test_union.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import pytest

from ..objecttype import ObjectType
from ..schema import Schema
from ..union import Union
from graphene.relay.connection import ConnectionField


class MyObjectType1(ObjectType):
Expand Down Expand Up @@ -41,3 +43,33 @@ class MyUnion(Union):
pass

assert str(exc_info.value) == 'Must provide types for Union MyUnion.'


def test_union_as_connection():
class MyUnion(Union):
class Meta:
types = (MyObjectType1, MyObjectType2)

class Query(ObjectType):
objects = ConnectionField(MyUnion)

def resolve_objects(self, args, context, info):
return [MyObjectType1(), MyObjectType2()]

query = '''
query {
objects {
edges {
node {
__typename
}
}
}
}
'''
schema = Schema(query=Query)
result = schema.execute(query)
assert not result.errors
assert len(result.data['objects']['edges']) == 2
assert result.data['objects']['edges'][0]['node']['__typename'] == 'MyObjectType1'
assert result.data['objects']['edges'][1]['node']['__typename'] == 'MyObjectType2'
20 changes: 19 additions & 1 deletion graphene/types/union.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
import six
from functools import partial

from ..utils.is_base_type import is_base_type
from .options import Options


def get_default_connection(cls):
from graphene.relay.connection import Connection

class Meta:
node = cls

return type('{}Connection'.format(cls.__name__), (Connection,), {'Meta': Meta})


class UnionMeta(type):

def __new__(cls, name, bases, attrs):
Expand All @@ -24,7 +34,15 @@ def __new__(cls, name, bases, attrs):
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:
get_connection = partial(get_default_connection, cls)

cls.Connection = get_connection()

return cls

def __str__(cls): # noqa: N805
return cls._meta.name
Expand Down