Added auto resolver function

This commit is contained in:
Syrus Akbary 2017-07-23 17:55:26 -07:00
parent b185f4cae7
commit fb4b4df500
5 changed files with 59 additions and 5 deletions

View File

@ -32,4 +32,5 @@ def annotate(_func=None, _trigger_warning=True, **annotations):
else:
_func.__annotations__.update(annotations)
_func._is_annotated = True
return _func

View File

@ -0,0 +1,12 @@
from .resolver_from_annotations import resolver_from_annotations
def auto_resolver(func=None):
annotations = getattr(func, '__annotations__', {})
is_annotated = getattr(func, '_is_annotated', False)
if annotations or is_annotated:
# Is a Graphene 2.0 resolver function
return resolver_from_annotations(func)
else:
return func

View File

@ -4,7 +4,7 @@ from functools import wraps
from ..types import Context, ResolveInfo
def annotated_resolver(func):
def resolver_from_annotations(func):
func_signature = signature(func)
_context_var = None

View File

@ -0,0 +1,36 @@
import pytest
from ..annotate import annotate
from ..auto_resolver import auto_resolver
from ...types import Context, ResolveInfo
def resolver(root, args, context, info):
return root, args, context, info
@annotate
def resolver_annotated(root, **args):
return root, args, None, None
@annotate(context=Context, info=ResolveInfo)
def resolver_with_context_and_info(root, context, info, **args):
return root, args, context, info
def test_auto_resolver_non_annotated():
decorated_resolver = auto_resolver(resolver)
# We make sure the function is not wrapped
assert decorated_resolver == resolver
assert decorated_resolver(1, {}, 2, 3) == (1, {}, 2, 3)
def test_auto_resolver_annotated():
decorated_resolver = auto_resolver(resolver_annotated)
assert decorated_resolver(1, {}, 2, 3) == (1, {}, None, None)
def test_auto_resolver_annotated_with_context_and_info():
decorated_resolver = auto_resolver(resolver_with_context_and_info)
assert decorated_resolver(1, {}, 2, 3) == (1, {}, 2, 3)

View File

@ -1,21 +1,25 @@
import pytest
from ..annotate import annotate
from ..annotated_resolver import annotated_resolver
from ..resolver_from_annotations import resolver_from_annotations
from ...types import Context, ResolveInfo
@annotate
def func(root, **args):
return root, args, None, None
@annotate(context=Context)
def func_with_context(root, context, **args):
return root, args, context, None
@annotate(info=ResolveInfo)
def func_with_info(root, info, **args):
return root, args, None, info
@annotate(context=Context, info=ResolveInfo)
def func_with_context_and_info(root, context, info, **args):
return root, args, context, info
@ -27,13 +31,14 @@ args = {
context = 2
info = 3
@pytest.mark.parametrize("func,expected", [
(func, (1, {'arg': 0}, None, None)),
(func_with_context, (1, {'arg': 0}, 2, None)),
(func_with_info, (1, {'arg': 0}, None, 3)),
(func_with_context_and_info, (1, {'arg': 0}, 2, 3)),
])
def test_annotated_resolver(func, expected):
resolver_func = annotated_resolver(func)
def test_resolver_from_annotations(func, expected):
resolver_func = resolver_from_annotations(func)
resolved = resolver_func(root, args, context, info)
assert resolved == expected