我正在使用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
进行测试答案 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。