Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Graphene Mutation error, fields must be a mapping (dict / OrderedDict)

I'm starting to wrap my head around with GraphQl/Graphene. I'm building a schema connected to a MongoDB. All seems to work so far except mutations. I've been following the example here and here without luck. Can someone point me towards what I'm doing wrong? Thanks in advance.

import graphene

class GeoInput(graphene.InputObjectType):
    lat = graphene.Float(required=True)
    lng = graphene.Float(required=True)

    @property
    def latlng(self):
        return "({},{})".format(self.lat, self.lng)


class Address(graphene.ObjectType):
    latlng = graphene.String()


class CreateAddress(graphene.Mutation):

    class Arguments:
        geo = GeoInput(required=True)

    Output = Address

    def mutate(self, info, geo):
        return Address(latlng=geo.latlng)


class Mutation(graphene.ObjectType):
    create_address = CreateAddress.Field()


class Query(graphene.ObjectType):
    address = graphene.Field(Address, geo=GeoInput(required=True))
    def resolve_address(self, info, geo):
        return Address(latlng=geo.latlng)

schema = graphene.Schema(query=Query, mutation=Mutation)

The code above generates this error:

AssertionError: CreateAddress fields must be a mapping (dict / OrderedDict) with field names as keys or a function which returns such a mapping.

like image 630
DevilWarrior Avatar asked Oct 15 '17 03:10

DevilWarrior


4 Answers

The problem is in the import. I've had same issue when I used:

from graphene import ObjectType

I've found how to import it properly in next example from docs. Here it is:

from graphene_django.types import DjangoObjectType
like image 169
scylo Avatar answered Nov 15 '22 20:11

scylo


The issue was with the version of graphene I had installed, installing graphene 2.0 solved the issue.

like image 28
DevilWarrior Avatar answered Nov 15 '22 20:11

DevilWarrior


My problem was that I had declared all of my fields incorrectly. This is my Type:

class EventDateRangeType(DjangoObjectType):

    class Meta:
        model = EventDateRange
        fields = ('start', 'end')

But my Model was:

class EventDateRange(models.Model):

    event = models.ForeignKey(Event, on_delete=models.CASCADE)
    start_time = models.DateTimeField()
    end_time = models.DateTimeField()

So start & end don't match start_time & end_time. Making them the same fixed my issue.

like image 25
smoquet Avatar answered Nov 15 '22 20:11

smoquet


In your mutation:

Output = Address

Should be a graphene object:

Output = graphene.Field(Address)
like image 31
Francisco de Larrañaga Avatar answered Nov 15 '22 21:11

Francisco de Larrañaga