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.
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
The issue was with the version of graphene I had installed, installing graphene 2.0 solved the issue.
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.
In your mutation:
Output = Address
Should be a graphene object:
Output = graphene.Field(Address)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With