首页 > 解决方案 > 将嵌套查询从 GraphQL 转换为 Graphene 不起作用

问题描述

所以我有一个 GraphQL API,我写了一个查询,它可以工作并将 GraphiQL 中的数据发回给我。

query dishes {
  dishes {
    name
    recipeNote
    description
  }
}

这是我对我所拥有的石墨烯的翻译

class Dish(ObjectType):
    name = String()
    recipeNote = String()
    description = String()
class Query(ObjectType):
    first = Field(Dish)

    def resolve_first(parent, info):
        return parent
query_string = "{ first { dishes { name recipeNote description } } }"

result = schema.execute(
        query_string)
print(result)
print(result.data)

然而,这给了我一个错误{'errors': [{'message': 'Cannot query field "dishes" on type "Dish".', 'locations': [{'line': 1, 'column': 11}]}]}

标签: python-3.xgraphqlgraphene-pythongraphene-django

解决方案


from graphene import List, ObjectType, String

class Dish(ObjectType):
    name = String()
    recipeNote = String()
    description = String()


class Query(ObjectType):
    dishes = List(Dish)

    def resolve_dishes(parent, info):
        return [Dish(name='', recipeNote='', description=''), ...]

query_string = "query dishes {
  dishes {
    name
    recipeNote
    description
  }
}"

result = schema.execute(
        query_string)
print(result)
print(result.data)

推荐阅读