From c7cb244edb4eed472f54811e92f8bcfd529008d0 Mon Sep 17 00:00:00 2001 From: Tim Griesser Date: Thu, 19 Jan 2017 22:25:20 -0500 Subject: [PATCH] Default resolve uses dict key if source value is dict --- graphql/execution/base.py | 8 ++++++-- graphql/execution/tests/test_resolve.py | 7 +++++++ 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/graphql/execution/base.py b/graphql/execution/base.py index 7929cd5a..533faafa 100644 --- a/graphql/execution/base.py +++ b/graphql/execution/base.py @@ -285,10 +285,14 @@ def __init__(self, field_name, field_asts, return_type, parent_type, def default_resolve_fn(source, args, context, info): - """If a resolve function is not given, then a default resolve behavior is used which takes the property of the source object + """If a resolve function is not given, then a default resolve behavior is used which takes the property of the source object or dict of the same name as the field and returns it as the result, or if it's a function, returns the result of calling that function.""" name = info.field_name - property = getattr(source, name, None) + property = None + if hasattr(source, name): + property = getattr(source, name) + elif type(source) is dict: + property = source.get(name) if callable(property): return property() return property diff --git a/graphql/execution/tests/test_resolve.py b/graphql/execution/tests/test_resolve.py index 42b96899..6695bf49 100644 --- a/graphql/execution/tests/test_resolve.py +++ b/graphql/execution/tests/test_resolve.py @@ -190,3 +190,10 @@ def resolver(source, args, *_): assert result.data == { 'test': '["Source!",{"a_input":{"input_recursive":{"input_one":"SourceRecursive!"}}}]' } + + +def test_default_resolve_works_with_dicts(): + schema = _test_schema(GraphQLField(GraphQLString)) + result = graphql(schema, '{ test }', {'test': 'testValue'}) + assert not result.errors + assert result.data == {'test': 'testValue'}