graphql-python / graphql-python/graphene-django
Using SerializerMutation with Relay
- Dominant language
- Python
- Stars
- 4.4k
- Forks
- 760
- PR merge metrics
- No merged PRs in 30d
Description
I'm overriding `mutate_and_get_payload` to convert the global ID to PK:
```py
class BaseSerializerMutation(SerializerMutation):
class Meta:
abstract = True
@classmethod
def mutate_and_get_payload(cls, root, info, **input):
model = cls._meta.serializer_class.Meta.model
for field, value in input.items():
if not hasattr(model, field):
continue
model_field = model._meta.get_field(field)
# convert relay ID to PK
if isinstance(model_field, (AutoField, ForeignKey)):
_, input[field] = from_global_id(value)
return super().mutate_and_get_payload(root, info, **input)
```
Then I also need to tell DRF that the global ID is not an integer:
```py
from rest_framework import serializers
class BaseModelSerializer(serializers.ModelSerializer):
id = serializers.CharField(required=False)
```
Ideally this should already be supported by graphene-django. Perhaps have a `RelaySerializerMutation` and a `RelayModelSerializer`? I can create a PR if you're OK with adding this.
Contributor guide
Assessment
This issue has not been assessed yet.