[英]Graphene Mutation error, fields must be a mapping (dict / OrderedDict)
I'm starting to wrap my head around with GraphQl/Graphene.我开始用 GraphQl/Graphene 来解决我的问题。 I'm building a schema connected to a MongoDB.
我正在构建一个连接到 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.
AssertionError: CreateAddress 字段必须是一个映射(dict / OrderedDict),其中字段名称作为键或返回此类映射的函数。
问题出在我安装的石墨烯版本上,安装石墨烯 2.0 解决了这个问题。
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
.所以
start
和end
不匹配start_time
和end_time
。 Making them the same fixed my issue.使它们相同解决了我的问题。
Had a similar error for a class that inherited from "InputObjectType".对于从“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)解决方案是从
graphene
而不是从graphql.type.tests.test_definition
导入“InputObjectType”(不知道为什么它首先从该库导入)
It can even happen if no output is specified in graphene.Mutation
inheriting class.如果在
graphene.Mutation
继承类中没有指定输出,它甚至可能发生。 But that is not case for DevilWarior.但DevilWarior并非如此。
In your mutation:在你的突变中:
Output = Address
Should be a graphene object:应该是石墨烯对象:
Output = graphene.Field(Address)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.