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

时间:2020-03-09 17:27:25

标签: graphene-python graphql-python

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

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

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

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

请帮助。

1 个答案:

答案 0 :(得分: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]

代码现在可以使用!