I want to create a mutation which takes a dict as an argument. There are implementation-specific reasons to want to do this, instead of creating a type/schema for the dict object.
# types.py
import typing
@strawberry.type
class Thing:
data: typing.Dict
# resolvers.py
import typing
from .types import Thing
def create_config(data: typing.Dict) -> Thing:
pass
# mutations.py
import strawberry
from .types import Thing
from .resolvers import create_thing
@strawberry.type
class Mutations:
create_thing: Thing = strawberry.mutation(resolver=create_thing)
mutation {
createThing(data: {}) {}
}
From reading the documentation, there's no GraphQL scalar equivalent for a dict. This is evidenced by this compilation error when I try to test:
TypeError: Thing fields cannot be resolved. Unexpected type 'typing.Dict'
My instinct is to flatten the dict into a JSON string and pass it that way. This seems un-elegant, which makes me think there's a more idiomatic approach. Where should I go from here?
Instead of a serialized JSON string, JSON itself can be a scalar.
from strawberry.scalars import JSON
@strawberry.type
class Thing:
data: JSON
def create_config(data: JSON) -> Thing:
pass