Skip to content

Commit f24cbd5

Browse files
authored
Fix custom foreignkey resolvers (#1361)
* Fix custom foreignkey resolvers * Fixed assert name conversion * Fix lint
1 parent ed7c995 commit f24cbd5

File tree

1 file changed

+15
-7
lines changed

1 file changed

+15
-7
lines changed

graphene_django/converter.py

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,13 @@
2626
from graphene.types.json import JSONString
2727
from graphene.types.scalars import BigInt
2828
from graphene.utils.str_converters import to_camel_case
29-
from graphql import GraphQLError, assert_valid_name
29+
from graphql import GraphQLError
30+
31+
try:
32+
from graphql import assert_name
33+
except ImportError:
34+
# Support for older versions of graphql
35+
from graphql import assert_valid_name as assert_name
3036
from graphql.pyutils import register_description
3137

3238
from .compat import ArrayField, HStoreField, JSONField, PGJSONField, RangeField
@@ -56,7 +62,7 @@ def wrapped_resolver(*args, **kwargs):
5662
def convert_choice_name(name):
5763
name = to_const(force_str(name))
5864
try:
59-
assert_valid_name(name)
65+
assert_name(name)
6066
except GraphQLError:
6167
name = "A_%s" % name
6268
return name
@@ -311,17 +317,19 @@ def dynamic_type():
311317
class CustomField(Field):
312318
def wrap_resolve(self, parent_resolver):
313319
"""
314-
Implements a custom resolver which go through the `get_node` method to insure that
320+
Implements a custom resolver which go through the `get_node` method to ensure that
315321
it goes through the `get_queryset` method of the DjangoObjectType.
316322
"""
317323
resolver = super().wrap_resolve(parent_resolver)
318324

319325
def custom_resolver(root, info, **args):
320326
fk_obj = resolver(root, info, **args)
321-
if fk_obj is None:
322-
return None
323-
else:
324-
return _type.get_node(info, fk_obj.pk)
327+
if not isinstance(fk_obj, model):
328+
# In case the resolver is a custom one that overwrites
329+
# the default Django resolver
330+
# This happens, for example, when using custom awaitable resolvers.
331+
return fk_obj
332+
return _type.get_node(info, fk_obj.pk)
325333

326334
return custom_resolver
327335

0 commit comments

Comments
 (0)