Graphene Python列表为所有字段解析null

时间:2017-10-25 19:17:11

标签: python graphql graphene-python

我正在使用python中的GraphQL尝试解析列表数据,但字段正在解析为null。如何让他们返回实际的列表数据?

以下是我的代码片段

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': 
                 'WRussell@dolor.gov', 'teamId': 0}, {'id': 39331, 
                 'username': 'AHohmann','email': 'AMarina@sapien.com', 
                 'teamId': 0}]
        return  resp

schema = graphene.Schema(query=Query)

该代码段可以在graphene playground

进行测试

这是我当前的查询 query

和不受欢迎的反应 graphQL response

1 个答案:

答案 0 :(得分:7)

您需要返回用户的对象,而不仅仅是字典:

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='WRussell@dolor.gov')]
        return  resp

schema = graphene.Schema(query=Query)

您可以登入playground