graphene
graphene copied to clipboard
Custom serialization for UUIDs
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
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
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?
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