Closed cpd67 closed 10 months ago
The field_cls
that is used for any attribute that doesn't have a = strawberry_or_django_field(...)
can be passed to strawberry_django.type
like:
@strawberry_django.type(SomeModel, field_cls=CustomStrawberryDjangoField)
class SomeModelType(BaseType):
field_5: bool
In this example, field_5
will be turned into a CustomStrawberryDjangoField
instance
I tried accomplishing the same thing using strawberry.interface and the same thing happened. Do I need to use strawberry_django.interface?
strawberry_django.interface
is to strawberry.interface
the same as strawberry_django.type
is to strawberry.type
. It basically adds the extra functionality (like auto
type conversion based on the ORM introspection) to it. Note that it calls the same _process_type, it just has some different defaults
Thanks for the reply! The issue isn't with field_5
, though. The issue is that the inherited field_1
, field_2
& field_4
fields from BaseType
become StrawberryDjangoField
s on SomeModelType
& AnotherModelType
and not CustomStrawberryDjangoField
s. I can make a quick reproduction later today that shows this.
@cpd67 oh I see. Well, the suggestion I have would workaround this, but I agree that there is a bug in this as well for field_1
, field_2
and field_4
.
Will be waiting for the repro :)
@bellini666 Thank you :) I have a repro made here. I created a small Django project with strawberry & strawberry-django. The strawberry types and custom fields are located at mysite/myapp/types.py
, and I created a custom GraphQLView
located at mysite/myapp/views.py
that prints out the fields for each of the types to the console whenever you go to localhost:8000/graphql
. Here's a screenshot of the output:
Notice that the field types for SomeModelType
are my custom ones, but then they are StrawberryDjangoField
s for the other subtypes.
Please let me know if there's anything else you need from me. I'd be happy to help contribute a fix for this, as well! :smile:
@bellini666 I just wanted to let you know that the fix worked :smile: Thanks a ton!
@cpd67 Awesome! :)
Describe the Bug
Hi there 😄
I've created a custom field class that has inherited from
StrawberryDjangoField
, and i'm using it in a few of my object types. Some of these types have common fields, so I factored those out into a base type and inherited from it. I've noticed that the fields for some of the subtypes becomesStrawberryDjangoField
and not my custom field class. Here's some code demonstrating what i'm trying to do:If I look at what
field_1
,field_2
andfield_4
are onSomeModelType
's strawberry object definition, they're myCustomStrawberryDjangoField
. But if I look at what those fields are onSomeOtherModelType
&AnotherModelType
's object definitions, they'reStrawberryDjangoField
s and not myCustomStrawberryDjangoField
. I think the issue is in this code block inside of_process_type()
, where we loop through the type's fields and process each one:I think i'm hitting the
else
block whenSomeOtherModelType
&AnotherModelType
are turned into strawberry django types, andStrawberryDjangoField
s are created forfield_1
,field_2
&field_4
(becausefield_cls
isStrawberryDjangoField
by default in_process_type()
). Is there any way I could make it so thatfield_1
,field_2
&field_4
areCustomStrawberryDjangoField
across all subtypes? Maybe_process_type()
could be updated to not create a copy of the field if it's one we've already seen and only updatedjango_name
,description
, andtype_annotation
on the field?System Information
Additional Context
I tried accomplishing the same thing using
strawberry.interface
and the same thing happened. Do I need to usestrawberry_django.interface
?Upvote & Fund