简体   繁体   English

断言错误:类型 Droid 与关联的石墨烯类型 Droid 不匹配

[英]AssertionError: The type Droid does not match with the associated graphene type Droid

I am trying to understand the working of interfaces using the starwars example given in the github repository code.我正在尝试使用 github 存储库代码中给出的 starwars 示例来了解接口的工作。 The execution of a simple query leads to an AssertionError执行简单查询会导致 AssertionError

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

AssertionError: The type Droid does not match with the associated graphene type Droid.断言错误:类型 Droid 与关联的石墨烯类型 Droid 不匹配。

After spending a lot of time searching a resolution of this issue, I couldn't find a right answer.在花了很多时间搜索这个问题的解决方案后,我找不到正确的答案。 The relevant files are given on github repository path: examples/starwars/data.py examples/starwars/schema.py相关文件在github仓库路径中给出:examples/starwars/data.py examples/starwars/schema.py

Please help.请帮忙。

Found the answer by diving deep into Interfaces on Graphene and Ariadne documentation.通过深入研究 Graphene 和 Ariadne 文档中的 Interfaces 找到了答案。 It requires specifying resolution of Interface into related data type.它需要将接口的解析指定为相关数据类型。 In the Starwars example, the Character had to be resolved into either a Human or a Droid.在 Starwars 示例中,角色必须分解为人类或机器人。 This required a classmethod to be added as follows:这需要添加一个类方法,如下所示:

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]

The code works now!代码现在有效!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM