【问题标题】:AssertionError: The type Droid does not match with the associated graphene type DroidAssertionError:类型 Droid 与关联的石墨烯类型 Droid 不匹配
【发布时间】:2020-06-21 15:21:01
【问题描述】:

我正在尝试使用 github 存储库代码中给出的 starwars 示例来了解接口的工作原理。 执行简单查询会导致 AssertionError

query = """query HeroNameQuery { hero { name } }"""

AssertionError:类型 Droid 与关联的石墨烯类型 Droid 不匹配。

在花了很多时间寻找这个问题的解决方案后,我找不到正确的答案。 相关文件在 github 存储库路径中给出: 示例/starwars/data.py 例子/starwars/schema.py

请帮忙。

【问题讨论】:

    标签: graphene-python graphql-python


    【解决方案1】:

    通过深入研究 Interfaces on Graphene 和 Ariadne 文档找到了答案。 它需要将接口的分辨率指定为相关的数据类型。 在 Starwars 示例中,角色必须分解为人类或机器人。这需要添加一个类方法,如下所示:

    class Character(graphene.Interface):
    id = graphene.ID()
    name = graphene.String()
    friends = graphene.List(lambda: Character)
    appears_in = graphene.List(Episode)
    
    @classmethod
    def resolve_type(cls, instance, info):
        if isinstance(cls, Droid):
            return Droid
        else:
            return Human
    
    def resolve_friends(self, info):
        # The character friends is a list of strings
        return [get_character(f) for f in self.friends]
    

    代码现在可以工作了!

    【讨论】:

      猜你喜欢
      • 2018-03-06
      • 2019-10-05
      • 2020-11-18
      • 2019-08-08
      • 1970-01-01
      • 2021-07-11
      • 2018-01-29
      • 2019-10-04
      • 1970-01-01
      相关资源
      最近更新 更多