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

6

8

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.

Lenlena answered 15/10, 2017 at 3:15 Comment(0)
L
1

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

Lenlena answered 15/10, 2017 at 4:42 Comment(0)
A
3

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
Antiseptic answered 5/8, 2019 at 14:33 Comment(0)
L
1

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

Lenlena answered 15/10, 2017 at 4:42 Comment(0)
A
1

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.

Adamson answered 13/10, 2020 at 14:49 Comment(0)
P
1

In your mutation:

Output = Address

Should be a graphene object:

Output = graphene.Field(Address)
Parallax answered 28/1, 2022 at 17:39 Comment(0)
E
0

Had a similar error for a class that inherited from "InputObjectType". The solution was to import "InputObjectType" from graphene instead of from graphql.type.tests.test_definition (don't know why it was imported from that library in the first place)

Espinoza answered 18/10, 2020 at 10:55 Comment(0)
L
0

It can even happen if no output is specified in graphene.Mutation inheriting class. But that is not case for DevilWarior.

Lehmann answered 24/1, 2022 at 11:35 Comment(1)
This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From ReviewPetrick

© 2022 - 2024 — McMap. All rights reserved.