Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix error of multiple inputs with the same type. When using same seri… #530

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 9 additions & 2 deletions graphene_django/rest_framework/serializer_converter.py
Expand Up @@ -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)
Expand Down
@@ -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