Skip to content

Commit

Permalink
Fix custom foreignkey resolvers (graphql-python#1361)
Browse files Browse the repository at this point in the history
* Fix custom foreignkey resolvers

* Fixed assert name conversion

* Fix lint
  • Loading branch information
syrusakbary authored and superlevure committed Jul 19, 2023
1 parent baf1dba commit 238c6d0
Showing 1 changed file with 15 additions and 7 deletions.
22 changes: 15 additions & 7 deletions graphene_django/converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,13 @@
from graphene.types.json import JSONString
from graphene.types.scalars import BigInt
from graphene.utils.str_converters import to_camel_case
from graphql import GraphQLError, assert_valid_name
from graphql import GraphQLError

try:
from graphql import assert_name
except ImportError:
# Support for older versions of graphql
from graphql import assert_valid_name as assert_name
from graphql.pyutils import register_description

from .compat import ArrayField, HStoreField, JSONField, PGJSONField, RangeField
Expand Down Expand Up @@ -56,7 +62,7 @@ def wrapped_resolver(*args, **kwargs):
def convert_choice_name(name):
name = to_const(force_str(name))
try:
assert_valid_name(name)
assert_name(name)
except GraphQLError:
name = "A_%s" % name
return name
Expand Down Expand Up @@ -311,17 +317,19 @@ def dynamic_type():
class CustomField(Field):
def wrap_resolve(self, parent_resolver):
"""
Implements a custom resolver which go through the `get_node` method to insure that
Implements a custom resolver which go through the `get_node` method to ensure that
it goes through the `get_queryset` method of the DjangoObjectType.
"""
resolver = super().wrap_resolve(parent_resolver)

def custom_resolver(root, info, **args):
fk_obj = resolver(root, info, **args)
if fk_obj is None:
return None
else:
return _type.get_node(info, fk_obj.pk)
if not isinstance(fk_obj, model):
# In case the resolver is a custom one that overwrites
# the default Django resolver
# This happens, for example, when using custom awaitable resolvers.
return fk_obj
return _type.get_node(info, fk_obj.pk)

return custom_resolver

Expand Down

0 comments on commit 238c6d0

Please sign in to comment.