can query a union type

This commit is contained in:
Chris Lyon 2017-10-23 23:59:56 -07:00
parent 078230ad49
commit 467daaa1d6

View File

@ -7,6 +7,7 @@ from graphql_relay import from_global_id, to_global_id
from ..types import ID, Field, Interface, ObjectType from ..types import ID, Field, Interface, ObjectType
from ..types.utils import get_type from ..types.utils import get_type
from ..types.interface import InterfaceMeta from ..types.interface import InterfaceMeta
from ..types.union import UnionMeta
def is_node(objecttype): def is_node(objecttype):
@ -98,6 +99,12 @@ class Node(six.with_metaclass(NodeMeta, Interface)):
return None return None
if only_type: if only_type:
if graphene_type != only_type and isinstance(only_type, UnionMeta):
union_types = only_type._meta.types
assert graphene_type in union_types, (
'Must receive one of {} id.'
).format(', '.join(map(lambda t: t._meta.name, union_types)))
else:
assert graphene_type == only_type, ( assert graphene_type == only_type, (
'Must receive an {} id.' 'Must receive an {} id.'
).format(graphene_type._meta.name) ).format(graphene_type._meta.name)