Search code examples
pythongraphqlgraphene-python

Graphene Python Lists resolve null for all fields


I am working with GraphQL in python am trying to resolve list data but the fields are resolving null. How can i make them return the actual list data?

Here is a snippet of my code

import graphene


class User(graphene.ObjectType):
    """ Type definition for User """
    id = graphene.Int()
    username = graphene.String()
    email = graphene.String()

class Query(graphene.ObjectType):
    users = graphene.List(User)

    def resolve_users(self, args):
        resp = [{'id': 39330, 'username': 'RCraig', 'email': 
                 '[email protected]', 'teamId': 0}, {'id': 39331, 
                 'username': 'AHohmann','email': '[email protected]', 
                 'teamId': 0}]
        return  resp

schema = graphene.Schema(query=Query)

The snippet is can tested at graphene playground

Here is my current query query

and the undesired response graphQL response


Solution

  • You need to return User's objects, not just dictionary:

    import graphene
    
    
    class User(graphene.ObjectType):
        """ Type definition for User """
        id = graphene.Int()
        username = graphene.String()
        email = graphene.String()
    
    class Query(graphene.ObjectType):
        users = graphene.List(User)
    
        def resolve_users(self, args):
            resp = [User(id=39330, username='RCraig', email='[email protected]')]
            return  resp
    
    schema = graphene.Schema(query=Query)
    

    You can check in playground.