eamigo86 / graphene-django-extras

Extras functionalities for Graphene-Django
MIT License
417 stars 106 forks source link

No docs on overriding a field #99

Open kneufeld opened 5 years ago

kneufeld commented 5 years ago

I had a lot of difficulty getting a django BinaryField overridden and I'm not sure if I did it correctly but it seems to work.

graphene_django.extras.converter.convert_binary_to_string doesn't seem to get called/registered no matter how/when I import it. I couldn't find any docs/examples/tests anywhere showing how to make use of the converter functions but I may have just missed them somehow.

Maybe the docs just need an example?

Here's how I got something working...

# models.py
from django.db import models

class Venue(models.Model):
    name = models.CharField(max_length=128)
    icon = models.BinaryField(null=True)
# schema.py

import graphene
from graphene import relay, ObjectType, Schema
from graphene_django.types import DjangoObjectType
from graphene_django.filter import DjangoFilterConnectionField
from graphene_django_extras.base_types import Binary

from backend.models import Venue

class VenueNode(DjangoObjectType):
    class Meta:
        model = Venue
        exclude_fields = ('icon',) # must exclude first!!!

        icon    = graphene.Field(Binary)

class Query(ObjectType):
    venue = relay.Node.Field(VenueNode)
    all_venues = DjangoFilterConnectionField(VenueNode)

schema = Schema(query=Query)
Nelson-Morais commented 1 year ago

I tried this approach but my app throws an Error while importing "Binary", still no docs on how to use the conversion. Any ideas ?

Traceback (most recent call last): 2023-03-27T16:00:23.978813186Z File "/usr/src/venv/lib/python3.10/site-packages/graphene_django/settings.py", line 80, in import_from_string 2023-03-27T16:00:23.978817194Z module = importlib.import_module(module_path) 2023-03-27T16:00:23.978820770Z File "/usr/local/lib/python3.10/importlib/init.py", line 126, in import_module 2023-03-27T16:00:23.978824538Z return _bootstrap._gcd_import(name[level:], package, level) 2023-03-27T16:00:23.978827563Z File "", line 1050, in _gcd_import 2023-03-27T16:00:23.978831591Z File "", line 1027, in _find_and_load 2023-03-27T16:00:23.978834907Z File "", line 1006, in _find_and_load_unlocked 2023-03-27T16:00:23.978838263Z File "", line 688, in _load_unlocked 2023-03-27T16:00:23.978841399Z File "", line 883, in exec_module 2023-03-27T16:00:23.978845177Z File "", line 241, in _call_with_frames_removed 2023-03-27T16:00:23.978848343Z File "/usr/src/app/api/core/graphql/root.py", line 12, in 2023-03-27T16:00:23.978852050Z from passbolt.graphql.passboltEntries import schema as passbolt_schema 2023-03-27T16:00:23.978854785Z File "/usr/src/app/api/passbolt/graphql/passboltEntries.py", line 10, in 2023-03-27T16:00:23.978857630Z from graphene_django_extras.base_types import Binary 2023-03-27T16:00:23.978860335Z File "/usr/src/venv/lib/python3.10/site-packages/graphene_django_extras/init.py", line 4, in 2023-03-27T16:00:23.978863842Z from .directives import get_all_directives 2023-03-27T16:00:23.978884651Z File "/usr/src/venv/lib/python3.10/site-packages/graphene_django_extras/directives/init.py", line 4, in 2023-03-27T16:00:23.978887757Z from .string import * 2023-03-27T16:00:23.978889971Z File "/usr/src/venv/lib/python3.10/site-packages/graphene_django_extras/directives/string.py", line 8, in 2023-03-27T16:00:23.978892516Z from ..utils import to_kebab_case 2023-03-27T16:00:23.978901703Z File "/usr/src/venv/lib/python3.10/site-packages/graphene_django_extras/utils.py", line 15, in 2023-03-27T16:00:23.978904749Z from rest_framework.compat import _resolve_model 2023-03-27T16:00:23.978907033Z ImportError: cannot import name '_resolve_model' from 'rest_framework.compat' (/usr/src/venv/lib/python3.10/site-packages/rest_framework/compat.py)