siva
siva

Reputation: 629

How Can I pass dictionary data as a input to the fast api strawberry mutation?

My input data: {"Nepal": "Kathmandu", "Italy": "Rome", "England": "London"}

Goal: I want to pass above data as a input to the strawberry mutation.

It seems currently, strawberry is not supporting dict type. I found JSON type but if i want to use JSON as a type i need to pass the data like this "{\"Nepal\": \"Kathmandu\", \"Italy\": \"Rome\", \"England\": \"London\"}"

I dont want to escape the quotes by using \. Is there any alternative to handle this issue?

Upvotes: 0

Views: 955

Answers (2)

Fanna1119
Fanna1119

Reputation: 2288

I did something similar the other day, however not sure if it is exactly what you want, my database had a json field so my graphql also required the Json type

import ast

@strawberry.type(description="mutation for blogs")
class BlogMutation:

    @strawberry.mutation(description="Create a new blog")
    async def create_blog(self, id: int, blog: BlogCreate) -> int:

        parse_blog_body = ast.literal_eval(blog.body)
        parse_blog_body = dict(parse_blog_body)

        new_blog = BlogTable(title=blog.title, body=parse_blog_body, user_id=id)
        session.add(new_blog)
        session.commit()
        session.refresh(new_blog)

        return new_blog.id

my serializers.py


from strawberry.scalars import JSON
import strawberry


class BlogSerializer:
    id: int
    title: str
    body: JSON


class BlogCreateSerializer:
    title: str
    body: JSON


BlogCreate = strawberry.input(BlogCreateSerializer)
Blog = strawberry.type(BlogSerializer)

model.py


from sqlalchemy import Column, Integer, String, ForeignKey, JSON
from sqlalchemy.orm import relationship
from connection.connection import Base

class Blog(Base):
    __tablename__ = 'blog'

    id = Column(Integer, primary_key=True, autoincrement=True)
    title = Column(String)
    body = Column(JSON, default={})
    user_id = Column(Integer, ForeignKey('user.id'))
    user = relationship('User', back_populates='blog')

    class Config:
        arbitrary_types_allowed = True


do note that your issue might be to do with graphql quotes notice the query is using double quotes and then the json is using single, could not find a way around this


mutation {
  createBlog(id: 1, blog: {
    title: "hello",
    body: "{'hello': 'world'}"
  })
}

Upvotes: 1

blhsing
blhsing

Reputation: 106435

Python supports both single quotes and double quotes for enclosing a string literal, so you can write the said JSON string quoted in single quotes instead to avoid having to escape double quotes:

'{"Nepal": "Kathmandu", "Italy": "Rome", "England": "London"}'

If you have the input data given to you as a dict, you can also use the json.dumps method instead to output the object as a JSON string:

>>> import json
>>> json.dumps({"Nepal": "Kathmandu", "Italy": "Rome", "England": "London"})
'{"Nepal": "Kathmandu", "Italy": "Rome", "England": "London"}'
>>>

Upvotes: 0

Related Questions