Graphene Django返回空值

时间:2018-08-13 00:01:13

标签: python graphql graphene-python

使用以下架构时,在查询产品时,我一直获取空值。根据我对文档的理解,它应该返回一个包含2个包含id和name的对象的数组。谁能帮助我了解为什么以下代码不起作用?

import graphene

class Product(graphene.ObjectType):
    id = graphene.Int()
    name = graphene.String()


class Query(graphene.ObjectType):
    products = graphene.List(Product)

    def resolve_products(self, info, **kwargs):
        return [{
            "name": "TEST", "id": 1
        }, {
            "name": "TEST2", "id": 2
        }]

schema = graphene.Schema(query=Query)

1 个答案:

答案 0 :(得分:0)

我不知道您现在是否知道。我遇到了与您相同的问题,感谢互联网。以下答案可能会有所帮助。

PS,我使用flask来可视化网络。

from flask import Flask
from flask_graphql import GraphQLView
import graphene

app = Flask(__name__)

class Product(graphene.ObjectType):
    id = graphene.Int()
    name = graphene.String()

class Query(graphene.ObjectType):
    products = graphene.List(Product)

    def resolve_products(self, info, **kwargs):
        return [Product(name="TEST", id=1), Product(name="TEST2", id= 2)]

schema = graphene.Schema(query=Query)
app.add_url_rule('/graphql', view_func=GraphQLView.as_view('graphql',
    schema=schema, graphiql=True))

app.run(port=4901)

运行.py时,输入http://localhost:4901/graphql进行查询。

query{
  users{
    id
  }
}

返回列表类型。 return [Product(name="TEST", id=1), Product(name="TEST2", id= 2)]