Skip to content

Add dict support for the default resolver #174

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 12, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions graphql/execution/tests/test_resolve.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,3 +224,10 @@ def resolver(source, info, **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"}
7 changes: 5 additions & 2 deletions graphql/execution/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -345,11 +345,14 @@ def get_field_entry_key(node):


def default_resolve_fn(source, info, **args):
# type: (Any, ResolveInfo, **Any) -> Union[int, str]
# type: (Any, ResolveInfo, **Any) -> Optional[Any]
"""If a resolve function is not given, then a default resolve behavior is used which takes the property of the source object
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)
if isinstance(source, dict):
property = source.get(name)
else:
property = getattr(source, name, None)
if callable(property):
return property()
return property
Expand Down