1

I'm trying to write a resolver in python graphql for the non-table data. For example, my dictionary is as follows:

api_list={1:"API 1", 2:"API 2"}

I want the graphql result as

[
   {
      "name":"API 01"
   },
   {
      "name":"API 02"
   }
]

I tried using the Query as

class Query(ObjectType):
  apis = List()
  def resolve_apis(self, info, api_list):
      results = []
      for key, value in apis_list.items():
          api = {}
          api[key] = value
          results.append(api)
      return results

But getting an error saying that: a model type is not defined. Can we write a resolver for returning dictionary elements without putting them in a Django table in real?

__init__() missing 1 required positional argument: 'of_type'
CryptoFool
  • 21,719
  • 5
  • 26
  • 44
Naga Satya
  • 23
  • 3
  • Do you want to get the query result of api_list={1:"API 1", 2:"API 2"} as [ { "name":"API 01" }, { "name":"API 02" } ] ? or are you expecting api_list as query param and returning the same in [ { "name":"API 01" }, { "name":"API 02" } ] format ? – danish_wani Apr 05 '21 at 09:49
  • 1
    You're on the right track, but you need to provide arguments to `List` -- see https://docs.graphene-python.org/en/latest/types/list-and-nonnull/ or better yet, see this related question https://stackoverflow.com/questions/51224477/django-graphene-passing-json-or-dict-data-as-input-for-mutation/51269035 which shows how to a dictionary for mutation. – Mark Chackerian Apr 07 '21 at 21:43
  • Thanks, @MarkChackerian, the second link clearly explained a solution. – Naga Satya Apr 14 '21 at 01:59

0 Answers0