eamigo86 / graphene-django-extras

Extras functionalities for Graphene-Django
MIT License
417 stars 106 forks source link

Nested fields doens't work #163

Open miliakhaled opened 3 years ago

miliakhaled commented 3 years ago

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 commented 3 years ago

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}
briggySmalls commented 3 years ago

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