graphene-pythongraphql-python

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. The execution of a simple query leads to an AssertionError

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

AssertionError: The type Droid does not match with the associated graphene type 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

Please help.


Solution

  • Found the answer by diving deep into Interfaces on Graphene and Ariadne documentation. 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. 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!