Skip to content

Commit 9d9b465

Browse files
committed
Let default resolver check for Mapping instead of dict (#102)
1 parent 3267c4c commit 9d9b465

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

src/graphql/execution/execute.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from asyncio import gather
2+
from collections.abc import Mapping
23
from typing import (
34
Any,
45
Awaitable,
@@ -1116,7 +1117,7 @@ def invalid_return_type_error(
11161117

11171118
def get_typename(value: Any) -> Optional[str]:
11181119
"""Get the ``__typename`` property of the given value."""
1119-
if isinstance(value, dict):
1120+
if isinstance(value, Mapping):
11201121
return value.get("__typename")
11211122
# need to de-mangle the attribute assumed to be "private" in Python
11221123
for cls in value.__class__.__mro__:
@@ -1193,7 +1194,7 @@ def default_field_resolver(source: Any, info: GraphQLResolveInfo, **args: Any) -
11931194
field_name = info.field_name
11941195
value = (
11951196
source.get(field_name)
1196-
if isinstance(source, dict)
1197+
if isinstance(source, Mapping)
11971198
else getattr(source, field_name, None)
11981199
)
11991200
if callable(value):

tests/execution/test_resolve.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from collections import ChainMap
2+
13
from graphql import graphql_sync
24
from graphql.type import (
35
GraphQLArgument,
@@ -25,7 +27,7 @@ class RootValue:
2527
root_value=RootValue(),
2628
) == ({"test": "testValue"}, None,)
2729

28-
def default_function_accesses_keys():
30+
def default_function_accesses_keys_of_dict():
2931
root_value = {"test": "testValue"}
3032

3133
assert graphql_sync(
@@ -34,6 +36,16 @@ def default_function_accesses_keys():
3436
root_value=root_value,
3537
) == ({"test": "testValue"}, None)
3638

39+
def default_function_accesses_keys_of_chain_map():
40+
# use a mapping that is not a subclass of dict
41+
root_value = ChainMap({"test": "testValue"})
42+
43+
assert graphql_sync(
44+
schema=_test_schema(GraphQLField(GraphQLString)),
45+
source="{ test }",
46+
root_value=root_value,
47+
) == ({"test": "testValue"}, None)
48+
3749
def default_function_calls_methods():
3850
class RootValue:
3951
_secret = "secretValue"

0 commit comments

Comments
 (0)