mirror of
https://github.com/graphql-python/graphene.git
synced 2024-11-22 09:36:44 +03:00
Dict or attr default resolver (#638)
* Add dict_or_attr resolver and set it as default * Add some tests * Dry up code * Updated formatting
This commit is contained in:
parent
bbe11c9b5e
commit
ca9188a615
|
@ -6,7 +6,14 @@ def dict_resolver(attname, default_value, root, info, **args):
|
|||
return root.get(attname, default_value)
|
||||
|
||||
|
||||
default_resolver = attr_resolver
|
||||
def dict_or_attr_resolver(attname, default_value, root, info, **args):
|
||||
resolver = attr_resolver
|
||||
if isinstance(root, dict):
|
||||
resolver = dict_resolver
|
||||
return resolver(attname, default_value, root, info, **args)
|
||||
|
||||
|
||||
default_resolver = dict_or_attr_resolver
|
||||
|
||||
|
||||
def set_default_resolver(resolver):
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
from ..resolver import (
|
||||
attr_resolver,
|
||||
dict_resolver,
|
||||
dict_or_attr_resolver,
|
||||
get_default_resolver,
|
||||
set_default_resolver,
|
||||
)
|
||||
|
@ -36,8 +37,16 @@ def test_dict_resolver_default_value():
|
|||
assert resolved == "default"
|
||||
|
||||
|
||||
def test_dict_or_attr_resolver():
|
||||
resolved = dict_or_attr_resolver("attr", None, demo_dict, info, **args)
|
||||
assert resolved == "value"
|
||||
|
||||
resolved = dict_or_attr_resolver("attr", None, demo_obj, info, **args)
|
||||
assert resolved == "value"
|
||||
|
||||
|
||||
def test_get_default_resolver_is_attr_resolver():
|
||||
assert get_default_resolver() == attr_resolver
|
||||
assert get_default_resolver() == dict_or_attr_resolver
|
||||
|
||||
|
||||
def test_set_default_resolver_workd():
|
||||
|
|
Loading…
Reference in New Issue
Block a user