how to return a list of dictionary in graphene?

LearningNoob

I am new on graphql and working with some datasets that is returned as a list of dictionaries.

snippet code:

class Player(ObjectType):
    username = String()
    role = String()

class Game(ObjectType):
    players = List(Player)

I'm wondering why below code doesn't work?

class Query(ObjectType):
        game_info = Field(Game, username=String(), role=String())
        
        def resolve_game_info(self, info):
            results =  [{
                        "username":"Malphite",
                        "role":"tank"
                        },
                        {
                        "username":"Teemo",
                        "role":"support"
                      }]
            output = []
            for res in results:
                 output.append(
                    Player(
                      username=res['username'],
                      role=res['role']
                    )
                  )

            return output

How I query in graphql:

query {
  game_info(username:"Teemo") {
    players {
      username
      role
    }
  }
}

Results to like this:

{
  "data": {
    "gameInfo": null
  }
}

Any help would this would be greatly appreciated!

Sagar Adhikari

The problem seems in format of returned data. Suppose, you have more fields in your Game, not only players. There is no way of including those fields in your return format.

Instead of return output .

Try: return {'players':output}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Return dictionary insted of string in Graphql using graphene

How to append the return of a function to a list and a dictionary in a for loop

How to return dictionary keys as a list in Python?

How to make the dictionary return a list value?

Graphene-django with generic resolver to return List or Field

How to fetch Django related model rows and return as a list of dictionary?

How to unpack a dictionary of list (of dictionaries!) and return as grouped tuples?

How do I return dictionary keys as a list in Python?

How to convert a list into a dictionary object and return from function?

Python- How to return a dictionary that counts occurrences in a list of strings?

Python: How do I remove elements from a dictionary and return it as a list?

How to walk on a dictionary which represent a graph and return a list of element

How do I return a dictionary of values from a list?

PYTHON: How to return a dictionary key if a list contains all values for that key

How do I pass multiple dictionary items into a function and return a list?

intersect a dictionary and a list to return another dictionary

Dictionary to return a character list with their indices

How to return random dictionary

How to return dictionary formatting?

How to get a list of all tags from Tagulous in Django Graphene

How do I return a new dictionary with values that are a list, from the current dictionary?

Graphene mutation with list as input

How can i pass each item in a list into a dictionary and recieve a list of outputs in return?

Graphene/Django mutations return null

How to change a list of list to a dictionary of dictionary?

List of Dictionary - How to combine a list of dictionary Python

How to convert list to dictionary

How to split the dictionary with in list

How to append a list to dictionary