mirror of
https://github.com/graphql-python/graphene-django.git
synced 2024-11-22 09:37:07 +03:00
Fix error of multiple inputs with the same type. When using same serializer. (#530)
This commit is contained in:
parent
0988e0798a
commit
a2103c19f4
|
@ -57,18 +57,25 @@ def convert_serializer_field(field, is_input=True):
|
|||
|
||||
|
||||
def convert_serializer_to_input_type(serializer_class):
|
||||
cached_type = convert_serializer_to_input_type.cache.get(serializer_class.__name__, None)
|
||||
if cached_type:
|
||||
return cached_type
|
||||
serializer = serializer_class()
|
||||
|
||||
items = {
|
||||
name: convert_serializer_field(field)
|
||||
for name, field in serializer.fields.items()
|
||||
}
|
||||
|
||||
return type(
|
||||
ret_type = type(
|
||||
"{}Input".format(serializer.__class__.__name__),
|
||||
(graphene.InputObjectType,),
|
||||
items,
|
||||
)
|
||||
convert_serializer_to_input_type.cache[serializer_class.__name__] = ret_type
|
||||
return ret_type
|
||||
|
||||
|
||||
convert_serializer_to_input_type.cache = {}
|
||||
|
||||
|
||||
@get_graphene_type_from_serializer_field.register(serializers.Field)
|
||||
|
|
|
@ -0,0 +1,63 @@
|
|||
import graphene
|
||||
import pytest
|
||||
from django.db import models
|
||||
from graphene import Schema
|
||||
from rest_framework import serializers
|
||||
|
||||
from graphene_django import DjangoObjectType
|
||||
from graphene_django.rest_framework.mutation import SerializerMutation
|
||||
|
||||
pytestmark = pytest.mark.django_db
|
||||
|
||||
|
||||
class MyFakeChildModel(models.Model):
|
||||
name = models.CharField(max_length=50)
|
||||
created = models.DateTimeField(auto_now_add=True)
|
||||
|
||||
|
||||
class MyFakeParentModel(models.Model):
|
||||
name = models.CharField(max_length=50)
|
||||
created = models.DateTimeField(auto_now_add=True)
|
||||
child1 = models.OneToOneField(MyFakeChildModel, related_name='parent1', on_delete=models.CASCADE)
|
||||
child2 = models.OneToOneField(MyFakeChildModel, related_name='parent2', on_delete=models.CASCADE)
|
||||
|
||||
|
||||
class ParentType(DjangoObjectType):
|
||||
class Meta:
|
||||
model = MyFakeParentModel
|
||||
interfaces = (graphene.relay.Node,)
|
||||
|
||||
|
||||
class ChildType(DjangoObjectType):
|
||||
class Meta:
|
||||
model = MyFakeChildModel
|
||||
interfaces = (graphene.relay.Node,)
|
||||
|
||||
|
||||
class MyModelChildSerializer(serializers.ModelSerializer):
|
||||
class Meta:
|
||||
model = MyFakeChildModel
|
||||
fields = "__all__"
|
||||
|
||||
|
||||
class MyModelParentSerializer(serializers.ModelSerializer):
|
||||
child1 = MyModelChildSerializer()
|
||||
child2 = MyModelChildSerializer()
|
||||
|
||||
class Meta:
|
||||
model = MyFakeParentModel
|
||||
fields = "__all__"
|
||||
|
||||
|
||||
class MyParentModelMutation(SerializerMutation):
|
||||
class Meta:
|
||||
serializer_class = MyModelParentSerializer
|
||||
|
||||
|
||||
class Mutation(graphene.ObjectType):
|
||||
createParentWithChild = MyParentModelMutation.Field()
|
||||
|
||||
|
||||
def test_create_schema():
|
||||
schema = Schema(mutation=Mutation, types=[ParentType, ChildType])
|
||||
assert schema
|
Loading…
Reference in New Issue
Block a user