Skip to content

Commit 1760eaf

Browse files
caseywebdevdan98765
authored andcommitted
Add dict support for the default resolver (#174)
1 parent c6e400b commit 1760eaf

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

graphql/execution/tests/test_resolve.py

+7
Original file line numberDiff line numberDiff line change
@@ -224,3 +224,10 @@ def resolver(source, info, **args):
224224
assert result.data == {
225225
"test": '["Source!",{"a_input":{"input_recursive":{"input_one":"SourceRecursive!"}}}]'
226226
}
227+
228+
229+
def test_default_resolve_works_with_dicts():
230+
schema = _test_schema(GraphQLField(GraphQLString))
231+
result = graphql(schema, "{ test }", {"test": "testValue"})
232+
assert not result.errors
233+
assert result.data == {"test": "testValue"}

graphql/execution/utils.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -349,11 +349,14 @@ def get_field_entry_key(node):
349349

350350

351351
def default_resolve_fn(source, info, **args):
352-
# type: (Any, ResolveInfo, **Any) -> Union[int, str]
352+
# type: (Any, ResolveInfo, **Any) -> Optional[Any]
353353
"""If a resolve function is not given, then a default resolve behavior is used which takes the property of the source object
354354
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."""
355355
name = info.field_name
356-
property = getattr(source, name, None)
356+
if isinstance(source, dict):
357+
property = source.get(name)
358+
else:
359+
property = getattr(source, name, None)
357360
if callable(property):
358361
return property()
359362
return property

0 commit comments

Comments
 (0)