graphene-django-extras icon indicating copy to clipboard operation
graphene-django-extras copied to clipboard

Nested fields doens't work

Open miliakhaled opened this issue 3 years ago • 2 comments

Hello I need to create a mutation with nested fields and i couldn't find any example to follow, so my code is :

class CharteredLivraisonMutation(CustomDjangoSerializerMutation):
    class Meta:
        serializer_class = serializers.CharteredLivraisonSerializer
        nested_fields = ("ressources", )

When i run this mutation, i get the following error:

{
          "field": "ressources",
          "messages": [
            "Incorrect type. Expected pk value, received GenericInputType."
          ]
        }

I debugged the code a little bit, I found that the type of the nested_fields must be a dict, so i tried a dict {"ressources":?????} but whatever i wrote as a value for ressources (serializer model, graphql types, django model ...) , it does not work.

miliakhaled avatar Dec 08 '20 09:12 miliakhaled

Hello, I'm sorry i found the solution. for those who encountered this problem, the solution is passing the serializer that matchs the nested fields.

class AffretementRessourcesSerializer(serializers.ModelSerializer):
	class Meta:
		model = models.AffretementRessources
		fields = '__all__'

class CharteredLivraisonMutation(CustomDjangoSerializerMutation):
    class Meta:
        serializer_class = serializers.CharteredLivraisonSerializer
        nested_fields = {"ressources": AffretementRessourcesSerializer}

miliakhaled avatar Dec 09 '20 07:12 miliakhaled

Just as a reference to anyone else encountering a similar issue. I too had this issue, whilst using the DjangoInputObjectType (rather than using DRF serializers).

I had specified a field inside nested_fields but never defined a subclass of DjangoInputObjectType that would be used for populating that model

briggySmalls avatar Jul 26 '21 08:07 briggySmalls