strawberry-graphql / strawberry-django

Strawberry GraphQL Django extension
https://strawberry.rocks/docs/django
MIT License
404 stars 117 forks source link

Field name of reversed relations #24

Closed g-as closed 3 years ago

g-as commented 3 years ago

Hi,

django field name is used to determine the field name for the corresponding strawberry type.

This is obviously fine, but in the case of reversed relations, the related_name would be more appropriate IMO.

Something like that would do the trick:


from django.db.models.fields.reverse_related import ForeignObjectRel

def get_model_fields(cls, model, fields, types, is_input, partial):

    [...]

    if isinstance(field, ForeignObjectRel)
        field_name = field.get_accessor_name()
    else:
        field_name = field.name

    model_fields.append((field_name, field_type, field_value))

I'll draft a PR tomorrow.

Upvote & Fund

Fund with Polar

la4de commented 3 years ago

@g-as, thank you for feedback.

We are in the middle of publishing enhanced API where all fields would be defined in class body as discussed in #20.

This internal function is refactored there and I definitely want to take this into account there.

g-as commented 3 years ago

A'ight, I'll go take a look.

la4de commented 3 years ago

This should work now with code in features/class-api branch. Feel free to try.

g-as commented 3 years ago

You rock!

la4de commented 3 years ago

See pull request #30