graphene icon indicating copy to clipboard operation
graphene copied to clipboard

Custom serialization for UUIDs

Open bburtin opened this issue 4 years ago • 3 comments

Is there a way to override the default serialization for UUIDs (or any object type)? In order to not serialize UUIDs with dashes, I'm having to do this for every UUID field:

    @staticmethod
    def resolve_id(parent: MyObject, info: graphene.ResolveInfo) -> str:
        return parent.id.hex

bburtin avatar Dec 01 '20 19:12 bburtin

Hi! Sorry for the late reply. You can just define your own custom Scalar (e.g. UUIDHex) You can find information about this in the docs

Speedy1991 avatar Mar 02 '21 08:03 Speedy1991

Just wondering if this is for a custom node implementation to replace the default relay.Node style b64 hashed IDs? If so I was also wondering how to implement a dictionary/table to map the UUID to the primary key of the models?

daniel-skale avatar Sep 14 '21 01:09 daniel-skale

Not sure if I get the point (I don't use relay), but you can define a serializer and a deserializer in the scalar. That means you can use UUIDHex values in your schema but query by a real UUID from your database, so there is no need to create a mapping dictionary/table

Speedy1991 avatar Sep 15 '21 09:09 Speedy1991