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

问题描述 投票:0回答:1

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

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

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

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

请帮助。

graphene-python graphql-python
1个回答
0
投票

通过深入研究Graphene和Ariadne文档上的Interfaces找到了答案。它要求将接口的解析度指定为相关数据类型。在《星球大战》的例子中,角色必须解析为人类或机器人。这需要添加如下的类方法:

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]

该代码现在可用!

© www.soinside.com 2019 - 2024. All rights reserved.