graphene graphql dictionary as a type
Asked Answered
G

2

26

I'm a newbie for the graphene and I'm trying to map the following structure into a Object Type and having no success at all

{
    "details": {
        "12345": {"txt1": "9", "txt2": "0"},
        "76788": {"txt1": "6", "txt2": "7"},
    }
}

Any guidance is highly appreciated
Thanks

Germiston answered 25/9, 2017 at 9:46 Comment(2)
some more info on what issues you're having and an example of the code where those issues arise would be helpful. We don't really have much to go on with here mate.Reasonless
github.com/graphql-python/graphene/issues/… Using GenericScalar does the jobAmbry
G
25

It is unclear what you are trying to accomplish, but (as far as I know) you should not have any arbitrary key/value names when defining a GraphQL schema. If you want to define a dictionary, it has to be be explicit. This means '12345' and '76788' should have keys defined for them. For instance:

class CustomDictionary(graphene.ObjectType):
    key = graphene.String()
    value = graphene.String()

Now, to accomplish a schema similar to what you ask for, you would first need to define the appropriate classes with:

# Our inner dictionary defined as an object
class InnerItem(graphene.ObjectType):
    txt1 = graphene.Int()
    txt2 = graphene.Int()

# Our outer dictionary as an object
class Dictionary(graphene.ObjectType):
    key = graphene.Int()
    value = graphene.Field(InnerItem)

Now we need a way to resolve the dictionary into these objects. Using your dictionary, here's an example of how to do it:

class Query(graphene.ObjectType):

    details = graphene.List(Dictionary)  
    def resolve_details(self, info):
        example_dict = {
            "12345": {"txt1": "9", "txt2": "0"},
            "76788": {"txt1": "6", "txt2": "7"},
        }

        results = []        # Create a list of Dictionary objects to return

        # Now iterate through your dictionary to create objects for each item
        for key, value in example_dict.items():
            inner_item = InnerItem(value['txt1'], value['txt2'])
            dictionary = Dictionary(key, inner_item)
            results.append(dictionary)

        return results

If we query this with:

query {
  details {
    key
    value {
      txt1
      txt2
    }
  }
}

We get:

{
  "data": {
    "details": [
      {
        "key": 76788,
        "value": {
          "txt1": 6,
          "txt2": 7
        }
      },
      {
        "key": 12345,
        "value": {
          "txt1": 9,
          "txt2": 0
        }
      }
    ]
  }
}
Grampositive answered 24/7, 2018 at 1:8 Comment(1)
What if you want to return just dictionary, not list of dictionaries? Is GenericScalar the only way? Like details would be {"key1": "val1", "key2": "val2"}. In this case both key and value are dynamic.Rust
W
8

You can now use graphene.types.generic.GenericScalar

Ref : https://github.com/graphql-python/graphene/issues/384

Warfarin answered 23/6, 2020 at 14:18 Comment(1)
A contextual example would be really helpful.Neveda

© 2022 - 2024 — McMap. All rights reserved.