Graphene Mutation 错误,字段必须是映射 (dict / OrderedDict)

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

我开始思考 GraphQl/Graphene。我正在构建一个连接到 MongoDB 的模式。到目前为止,一切似乎都有效,除了突变。我一直在按照示例 here and here 进行操作,但不幸的是。有人可以指出我做错了什么吗?提前致谢。

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)

上面的代码产生了这个错误:

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

问题出在我安装的graphene版本上,安装graphene 2.0解决了问题。

问题出在导入中。 我在使用时遇到了同样的问题:

from graphene import ObjectType

我在 docs 的下一个示例中找到了如何正确导入它。这是:

from graphene_django.types import DjangoObjectType

我的问题是我错误地声明了所有字段。这是我的类型:

class EventDateRangeType(DjangoObjectType):

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

但我的模型是:

class EventDateRange(models.Model):

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

所以 start & end 不匹配 start_time & end_time。使它们相同解决了我的问题。

对于从“InputObjectType”继承的 class 有类似的错误。解决方案是从 graphene 而不是从 graphql.type.tests.test_definition 导入“InputObjectType”(不知道为什么首先从该库导入它)

如果在 graphene.Mutation 继承 class 中没有指定输出,甚至会发生这种情况。 但是DevilWarior不是这样。

在你的突变中:

Output = Address

应该是石墨烯物体:

Output = graphene.Field(Address)