From d0e860920337e464710bd99e471e9cbc653d204b Mon Sep 17 00:00:00 2001 From: Chris Lyon Date: Tue, 24 Oct 2017 22:03:44 -0700 Subject: [PATCH] applied changes to latest master version and added tests --- graphene/relay/node.py | 2 +- graphene/relay/tests/test_node.py | 53 ++++++++++++++++++++++++++++++- 2 files changed, 53 insertions(+), 2 deletions(-) diff --git a/graphene/relay/node.py b/graphene/relay/node.py index 0cbafa9e..603041d9 100644 --- a/graphene/relay/node.py +++ b/graphene/relay/node.py @@ -100,7 +100,7 @@ class Node(AbstractNode): return None if only_type: - if graphene_type != only_type and isinstance(only_type, Union): + if graphene_type != only_type and issubclass(only_type, Union): union_types = only_type._meta.types assert graphene_type in union_types, ( 'Must receive one of {} id.' diff --git a/graphene/relay/tests/test_node.py b/graphene/relay/tests/test_node.py index 10dc5d94..26c8eedf 100644 --- a/graphene/relay/tests/test_node.py +++ b/graphene/relay/tests/test_node.py @@ -2,7 +2,7 @@ from collections import OrderedDict from graphql_relay import to_global_id -from ...types import ObjectType, Schema, String +from ...types import ObjectType, Schema, String, Union from ..node import Node, is_node @@ -40,11 +40,27 @@ class MyOtherNode(SharedNodeFields, ObjectType): return MyOtherNode(shared=str(id)) +class DummyNode(SharedNodeFields, ObjectType): + class Meta: + interfaces = (Node, ) + + @staticmethod + def get_node(info, id): + return DummyNode(shared=str(id)) + + +class UnionNode(Union): + class Meta: + types = [MyNode, MyOtherNode] + + class RootQuery(ObjectType): first = String() node = Node.Field() only_node = Node.Field(MyNode) only_node_lazy = Node.Field(lambda: MyNode) + union_node = Node.Field(UnionNode) + dummy_node = Node.Field(DummyNode) schema = Schema(query=RootQuery, types=[MyNode, MyOtherNode]) @@ -73,6 +89,20 @@ def test_subclassed_node_query(): [('shared', '1'), ('extraField', 'extra field info.'), ('somethingElse', '----')])}) +def test_union_node_query(): + executed1 = schema.execute( + '{ unionNode(id:"%s") { __typename, ... on MyNode { name }, ... on MyOtherNode { shared } } }' % Node.to_global_id("MyNode", 1) + ) + assert not executed1.errors + assert executed1.data == {'unionNode': {'__typename': 'MyNode', 'name': '1'}} + + executed2 = schema.execute( + '{ unionNode(id:"%s") { __typename, ... on MyNode { name }, ... on MyOtherNode { shared } } }' % Node.to_global_id("MyOtherNode", 1) + ) + assert not executed2.errors + assert executed2.data == {'unionNode': {'__typename': 'MyOtherNode', 'shared': '1'}} + + def test_node_requesting_non_node(): executed = schema.execute( '{ node(id:"%s") { __typename } } ' % Node.to_global_id("RootQuery", 1) @@ -119,6 +149,17 @@ def test_node_field_only_type_wrong(): assert executed.data == {'onlyNode': None} +def test_union_node_field_only_type_wrong(): + executed = schema.execute( + '{ unionNode(id:"%s") { __typename, ... on MyNode { name }, ... on MyOtherNode { shared } } }' % Node.to_global_id("DummyNode", 1) + ) + from pprint import pprint + pprint(executed.data) + assert len(executed.errors) == 1 + assert str(executed.errors[0]) == 'Must receive one of MyNode, MyOtherNode id.' + assert executed.data == {'unionNode': None} + + def test_node_field_only_lazy_type(): executed = schema.execute( '{ onlyNodeLazy(id:"%s") { __typename, name } } ' % Node.to_global_id("MyNode", 1) @@ -142,6 +183,12 @@ schema { query: RootQuery } +type DummyNode implements Node { + id: ID! + shared: String + somethingElse: String +} + type MyNode implements Node { id: ID! name: String @@ -163,5 +210,9 @@ type RootQuery { node(id: ID!): Node onlyNode(id: ID!): MyNode onlyNodeLazy(id: ID!): MyNode + unionNode(id: ID!): UnionNode + dummyNode(id: ID!): DummyNode } + +union UnionNode = MyNode | MyOtherNode """.lstrip()