2

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) 
3
  • Your resolve_products method is returning a list of dictionaries, not a list of Product Commented Aug 13, 2018 at 14:52
  • sorry, but not sure I quite understand. Is Product not an object type? Commented Aug 13, 2018 at 18:49
  • Apologies, misread what you said. Thanks Commented Aug 13, 2018 at 20:04

1 Answer 1

1

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)]

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.