2

我在 python 中使用 GraphQL 正在尝试解析列表数据,但字段解析为空。我怎样才能让他们返回实际的列表数据?

这是我的代码片段

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)

该片段可以在石墨烯游乐场进行测试

这是我当前的查询 询问

和不受欢迎的反应 graphQL 响应

4

1 回答 1

9

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

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)

你可以在操场上签到。

于 2017-10-25T19:50:33.910 回答