How to resolve models in CUD operations not on PK but on UUID?

108 Views Asked by At

The default CUD operation in srawberry_django identifies the model to be mutated by the model's id (the database's id column, which is the Primary Key). How can I change this behavior to point to a column's/model's UUID without overriding strawberry_django.mutations.update method?

In simple terms, how to do a search on a field that is not id/PK in strawberry-graphql-djang without mending update method?

2

There are 2 best solutions below

0
Lukasz Dynowski On

At the moment of strawberry-graphql-django v0.16.0 it's not possible to do so! The reason is the implementation of get_pk method.

def get_pk(data: dict[str, Any],) -> strawberry.ID | relay.GlobalID | Literal[UNSET] | None:
    pk = data.pop("id", UNSET)
    if pk is UNSET:
        pk = data.pop("pk", UNSET)
    return pk

As you can see, this method assumes that the model identifier is id or pk ONLY!

0
Ivan Kharlamov On

I have added a feature to resolve by custom key in update and delete mutations of strawberry graphql django, whis is available from verion starting from 0.21.0:

@strawberry_django.partial(SomeModel)
class SomeModelInputPartial:
    unique_field: strawberry.auto

@strawberry.type
class Mutation:
    update_model: SomeModelType = mutations.update(
        SomeModelInputPartial, key_attr="unique_field")
    delete_model: SomeModelType = mutations.delete(
        SomeModelInputPartial, key_attr="unique_field")