Mark
Mark

Reputation: 71

Graphene Django returning null values

When using the following schema I keep getting null values when querying for products. From my understanding of the documentation, it should return an array with 2 objects containing id and name. Can anyone help me to understand why the following code does not work?

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)

Upvotes: 2

Views: 2510

Answers (1)

jiap
jiap

Reputation: 11

I don't know whether you figure it out right now. I encountered the same problem as you and thanks for the internet. the following answer may be helpful.

PS, i use flask to visualize the web.

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)

when run the .py, enter http://localhost:4901/graphql to query.

query{
  users{
    id
  }
}

Return a list-type. return [Product(name="TEST", id=1), Product(name="TEST2", id= 2)]

Upvotes: 1

Related Questions