Skip to content
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

Bugfix: call resolver function in DjangoConnectionField as documented #1529

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

PoByBolek
Copy link

This fixes a bug in that DjangoConnectionField doesn't call its resolver function despite it being documented in the Graphene docs: "Resolvers outside the class".

# currently, this will never be called :(
def resolve_ships(root, info):
    return [ShipModel()]

class Query(ObjectType):
    ships = DjangoConnectionField(Ship, resolver=resolve_ships)

I also fixed some warnings and running the tests now treats warnings as errors. But I really only care about c127aec here 😄

Comment on lines 193 to 199
@convert_django_field.register(models.AutoField)
@convert_django_field.register(models.BigAutoField)
@convert_django_field.register(models.SmallAutoField)
def convert_field_to_id(field, registry=None):
return ID(description=get_django_field_description(field), required=not field.null)


Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SmallAutoField has been arouns since Django 3.0 from 2019-12-02, which we don't support anymore. So the check for models.SmallAutoField shouldn't be necessary anymore.

Comment on lines 86 to +87
def test_should_uuid_convert_string():
if hasattr(forms, "UUIDField"):
assert_conversion(forms.UUIDField, UUID)
assert_conversion(forms.UUIDField, UUID)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

forms.UUIDField has been around since Django 1.8 from 2015-04-01.

Comment on lines 98 to 100
def test_should_uuid_convert_string():
if hasattr(serializers, "UUIDField"):
assert_conversion(serializers.UUIDField, graphene.String)
assert_conversion(serializers.UUIDField, graphene.String)

Copy link
Author

@PoByBolek PoByBolek Jul 4, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

DRF added support for UUIDField in 3.0.4 from 2015-01-28. We require DRF >= 3.6.3

@@ -166,14 +164,14 @@ def test_field_with_choices_convert_enum():
help_text="Language", choices=(("es", "Spanish"), ("en", "English"))
)

class TranslatedModel(models.Model):
class ChoicesModel(models.Model):
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This fixes a Django warning about the TranslatedModel being already registered:

RuntimeWarning: Model 'test.translatedmodel' was already registered. Reloading models is not advised as it can lead to inconsistencies, most notably with related models.

def resolve_ships(self):
def resolve_ships(self, info):
return get_ships()
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

resolve_only_args is deprecated

@PoByBolek
Copy link
Author

I just noticed that this is basically a duplicate of #1513 😅

@PoByBolek
Copy link
Author

@sjdemartini @kiendang any thoughts on this?

Copy link
Collaborator

@sjdemartini sjdemartini left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@PoByBolek Sorry for the delay, I haven't had much time to look at graphene-django recently. Overall the changes look good to me, and I appreciate the comments clarifying the updates you made (as well as the efforts to reduce noise with warnings, etc). I would generally prefer that the changes unrelated to the DjangoConnectionField resolver fix were put into their own PR to make this simpler to review and more targeted, but the separate commits helps. This seems a tad cleaner than the solution in #1513 (where the super call looks a bit odd, even if the effect is the same), so merging this seems good to me.

Comment on lines -199 to -205
if hasattr(models, "SmallAutoField"):

@convert_django_field.register(models.SmallAutoField)
def convert_field_small_to_id(field, registry=None):
return convert_field_to_id(field, registry)


Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that this could be considered a backwards-incompatible change as it removes the (undocumented) convert_field_small_to_id() function from the graphene_django.converter module.

So if you want, I can turn this into two PR: one only including c127aec (the bugfix) and the remaining commits that fix the warnings.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants