Skip to content

Commit

Permalink
Allow class methods to be used as a field source
Browse files Browse the repository at this point in the history
  • Loading branch information
James Pollock committed Jan 27, 2017
1 parent 02eb685 commit f728542
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
2 changes: 1 addition & 1 deletion graphene/types/field.py
Expand Up @@ -13,7 +13,7 @@

def source_resolver(source, root, args, context, info):
resolved = getattr(root, source, None)
if inspect.isfunction(resolved):
if inspect.isfunction(resolved) or inspect.ismethod(resolved):
return resolved()
return resolved

Expand Down
9 changes: 9 additions & 0 deletions graphene/types/tests/test_field.py
Expand Up @@ -10,6 +10,9 @@ class MyInstance(object):
value = 'value'
value_func = staticmethod(lambda: 'value_func')

def value_method(self):
return 'value_method'


def test_field_basic():
MyType = object()
Expand Down Expand Up @@ -76,6 +79,12 @@ def test_field_source_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():
MyType = object()
field = Field(MyType, source=String())
Expand Down

0 comments on commit f728542

Please sign in to comment.