Use default_resolver to resolve values when using the source at… (#1155)

This commit is contained in:
Jonathan Kim 2020-03-16 16:20:04 +00:00 committed by GitHub
parent 6f2863ef6e
commit cb3bfe011f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 9 additions and 1 deletions

View File

@ -4,6 +4,7 @@ from functools import partial
from .argument import Argument, to_arguments
from .mountedtype import MountedType
from .resolver import default_resolver
from .structures import NonNull
from .unmountedtype import UnmountedType
from .utils import get_type
@ -12,7 +13,7 @@ base_type = type
def source_resolver(source, root, info, **args):
resolved = getattr(root, source, None)
resolved = default_resolver(source, None, root, info, **args)
if inspect.isfunction(resolved) or inspect.ismethod(resolved):
return resolved()
return resolved

View File

@ -66,6 +66,13 @@ def test_field_source():
assert field.resolver(MyInstance(), None) == MyInstance.value
def test_field_source_dict_or_attr():
MyType = object()
field = Field(MyType, source="value")
assert field.resolver(MyInstance(), None) == MyInstance.value
assert field.resolver({"value": MyInstance.value}, None) == MyInstance.value
def test_field_with_lazy_type():
MyType = object()
field = Field(lambda: MyType)