strawberry-graphql / strawberry-django

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

Way to allow the default strawberry django field to accept UUID as an input #145

Open 12tqian opened 2 years ago

12tqian commented 2 years ago

I am making a query with the following structure:

@strawberry.type
class ObjectQuery:
    object: ObjectNode = strawberry.django.field()

However, the input for this requires a type of pk: ID. Is it possible to change this to be of type pk: UUID? My current solution for this is just to write a generic custom resolver.

Upvote & Fund

Fund with Polar

patrick91 commented 2 years ago

@bellini666 do you have any ideas for an API for this use case?

Just thinking out loud, plain strawberry supports this:

def a_resolver(id: UUID) -> ObjectNode:
   ...

@strawberry.type
class ObjectQuery:
    object: ObjectNode = strawberry.field(resolver=a_resolver)

we could maybe have a way to fetch the params from a stub function:

def a_resolver(id: UUID) -> ObjectNode:
   ...

@strawberry.type
class ObjectQuery:
    object: ObjectNode = strawberry.django.field(params_from=a_resolver)

but sounds like it is a bit of work, but it might be useful for additional filtering.

or maybe we can pass a params_override:

@strawberry.type
class ObjectQuery:
    object: ObjectNode = strawberry.django.field(params_override={"pk": UUID})
bellini666 commented 2 years ago

Hey @patrick91 ,

Although I like your idea, I think we can probably do even better.

That pk gets created in here. We can probably just change the strawberry.ID to retrieve it from field_type_map using the pk field from django. That way it will use the correct type for the model's pk (e.g. strawberry.ID for AutoField, UUID for UUIDField, etc)

What do you think?

klowe0100 commented 2 years ago

Hey @patrick91 ,

Although I like your idea, I think we can probably do even better.

That pk gets created in here. We can probably just change the strawberry.ID to retrieve it from field_type_map using the pk field from django. That way it will use the correct type for the model's pk (e.g. strawberry.ID for AutoField, UUID for UUIDField, etc)

What do you think?

This approach makes a lot of sense to me and follows strawberry-django's theme of auto sensing field types from the django model.