Skip to main content
 首页 » 编程设计

python之Graphite 烯 Python 列表为所有字段解析 null

2025年05月04日11tuyile006

我在 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) 

可以在 graphene playground 测试代码段

这是我当前的查询

和不受欢迎的 react

请您参考如下方法:

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

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 .