mirror of
				https://github.com/graphql-python/graphene.git
				synced 2025-11-04 09:57:41 +03:00 
			
		
		
		
	Added support for old behavior of get_node
This commit is contained in:
		
							parent
							
								
									f69665dcd8
								
							
						
					
					
						commit
						d6a5269f0b
					
				| 
						 | 
					@ -36,7 +36,7 @@ def test_django_interface():
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@patch('graphene.contrib.django.tests.models.Article.objects.get', return_value=Article(id=1))
 | 
					@patch('graphene.contrib.django.tests.models.Article.objects.get', return_value=Article(id=1))
 | 
				
			||||||
def test_django_get_node(get):
 | 
					def test_django_get_node(get):
 | 
				
			||||||
    human = Human.get_node(1)
 | 
					    human = Human.get_node(1, None)
 | 
				
			||||||
    get.assert_called_with(id=1)
 | 
					    get.assert_called_with(id=1)
 | 
				
			||||||
    assert human.id == 1
 | 
					    assert human.id == 1
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -67,7 +67,7 @@ class DjangoNode(BaseNode, DjangoInterface):
 | 
				
			||||||
    id = GlobalIDField()
 | 
					    id = GlobalIDField()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    @classmethod
 | 
					    @classmethod
 | 
				
			||||||
    def get_node(cls, id):
 | 
					    def get_node(cls, id, info=None):
 | 
				
			||||||
        try:
 | 
					        try:
 | 
				
			||||||
            instance = cls._meta.model.objects.get(id=id)
 | 
					            instance = cls._meta.model.objects.get(id=id)
 | 
				
			||||||
            return cls(instance)
 | 
					            return cls(instance)
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -11,10 +11,32 @@ class OtherNode(relay.Node):
 | 
				
			||||||
    name = graphene.String()
 | 
					    name = graphene.String()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    @classmethod
 | 
					    @classmethod
 | 
				
			||||||
    def get_node(cls, id):
 | 
					    def get_node(cls, id, info):
 | 
				
			||||||
        pass
 | 
					        pass
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					def test_works_old_get_node():
 | 
				
			||||||
 | 
					    class Part(relay.Node):
 | 
				
			||||||
 | 
					        x = graphene.String()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        @classmethod
 | 
				
			||||||
 | 
					        def get_node(cls, id):
 | 
				
			||||||
 | 
					            return id
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    assert Part.get_node(1) == 1
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					def test_works_old_static_get_node():
 | 
				
			||||||
 | 
					    class Part(relay.Node):
 | 
				
			||||||
 | 
					        x = graphene.String()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        @staticmethod
 | 
				
			||||||
 | 
					        def get_node(id):
 | 
				
			||||||
 | 
					            return id
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    assert Part.get_node(1) == 1
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def test_field_no_contributed_raises_error():
 | 
					def test_field_no_contributed_raises_error():
 | 
				
			||||||
    with raises(Exception) as excinfo:
 | 
					    with raises(Exception) as excinfo:
 | 
				
			||||||
        class Part(relay.Node):
 | 
					        class Part(relay.Node):
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -1,3 +1,5 @@
 | 
				
			||||||
 | 
					import warnings
 | 
				
			||||||
 | 
					from functools import wraps
 | 
				
			||||||
from graphql_relay.node.node import to_global_id
 | 
					from graphql_relay.node.node import to_global_id
 | 
				
			||||||
 | 
					
 | 
				
			||||||
from ..core.types import (Boolean, Field, InputObjectType, Interface, List,
 | 
					from ..core.types import (Boolean, Field, InputObjectType, Interface, List,
 | 
				
			||||||
| 
						 | 
					@ -73,8 +75,26 @@ class BaseNode(object):
 | 
				
			||||||
    def _prepare_class(cls):
 | 
					    def _prepare_class(cls):
 | 
				
			||||||
        from graphene.relay.utils import is_node
 | 
					        from graphene.relay.utils import is_node
 | 
				
			||||||
        if is_node(cls):
 | 
					        if is_node(cls):
 | 
				
			||||||
            assert hasattr(
 | 
					            get_node = getattr(cls, 'get_node')
 | 
				
			||||||
                cls, 'get_node'), 'get_node classmethod not found in %s Node' % cls
 | 
					            assert get_node, 'get_node classmethod not found in %s Node' % cls
 | 
				
			||||||
 | 
					            assert callable(get_node), 'get_node have to be callable'
 | 
				
			||||||
 | 
					            args = 3
 | 
				
			||||||
 | 
					            if isinstance(get_node, staticmethod):
 | 
				
			||||||
 | 
					                args -= 1
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            if get_node.func_code.co_argcount < args:
 | 
				
			||||||
 | 
					                warnings.warn("get_node will receive also the info arg"
 | 
				
			||||||
 | 
					                              " in future versions of graphene".format(cls.__name__),
 | 
				
			||||||
 | 
					                              FutureWarning)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                @staticmethod
 | 
				
			||||||
 | 
					                @wraps(get_node)
 | 
				
			||||||
 | 
					                def wrapped_node(*node_args):
 | 
				
			||||||
 | 
					                    if len(node_args) < args:
 | 
				
			||||||
 | 
					                        node_args += (None, )
 | 
				
			||||||
 | 
					                    return get_node(*node_args[:-1])
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                setattr(cls, 'get_node', wrapped_node)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def to_global_id(self):
 | 
					    def to_global_id(self):
 | 
				
			||||||
        type_name = self._meta.type_name
 | 
					        type_name = self._meta.type_name
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue
	
	Block a user