mirror of
https://github.com/graphql-python/graphene.git
synced 2024-11-11 12:16:58 +03:00
Merge pull request #410 from docmatrix/feature/source-method
Allow class methods to be used as a field source
This commit is contained in:
commit
a3c3be26f4
|
@ -13,7 +13,7 @@ base_type = type
|
||||||
|
|
||||||
def source_resolver(source, root, args, context, info):
|
def source_resolver(source, root, args, context, info):
|
||||||
resolved = getattr(root, source, None)
|
resolved = getattr(root, source, None)
|
||||||
if inspect.isfunction(resolved):
|
if inspect.isfunction(resolved) or inspect.ismethod(resolved):
|
||||||
return resolved()
|
return resolved()
|
||||||
return resolved
|
return resolved
|
||||||
|
|
||||||
|
|
|
@ -10,6 +10,9 @@ class MyInstance(object):
|
||||||
value = 'value'
|
value = 'value'
|
||||||
value_func = staticmethod(lambda: 'value_func')
|
value_func = staticmethod(lambda: 'value_func')
|
||||||
|
|
||||||
|
def value_method(self):
|
||||||
|
return 'value_method'
|
||||||
|
|
||||||
|
|
||||||
def test_field_basic():
|
def test_field_basic():
|
||||||
MyType = object()
|
MyType = object()
|
||||||
|
@ -76,6 +79,12 @@ def test_field_source_func():
|
||||||
assert field.resolver(MyInstance(), {}, None, None) == MyInstance.value_func()
|
assert field.resolver(MyInstance(), {}, None, None) == MyInstance.value_func()
|
||||||
|
|
||||||
|
|
||||||
|
def test_field_source_method():
|
||||||
|
MyType = object()
|
||||||
|
field = Field(MyType, source='value_method')
|
||||||
|
assert field.resolver(MyInstance(), {}, None, None) == MyInstance().value_method()
|
||||||
|
|
||||||
|
|
||||||
def test_field_source_as_argument():
|
def test_field_source_as_argument():
|
||||||
MyType = object()
|
MyType = object()
|
||||||
field = Field(MyType, source=String())
|
field = Field(MyType, source=String())
|
||||||
|
|
Loading…
Reference in New Issue
Block a user