我想要创造一个突变,以一个二分法为论据。有一些特定于实现的原因需要这样做,而不是为dict对象创建类型/模式。
对象
# types.py
import typing
@strawberry.type
class Thing:
data: typing.DictResolver
# 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: {}) {}
}通过阅读文档,没有一个GraphQL标量等价于一个dict。当我尝试测试时,这个编译错误就证明了这一点:
TypeError: Thing fields cannot be resolved. Unexpected type 'typing.Dict'
我的本能反应是将数据块压平成JSON字符串,并以这种方式传递。这似乎不雅致,这让我觉得有一个更惯用的方法。从这里我该去哪里?
发布于 2022-05-05 16:08:08
JSON本身可以是标量,而不是序列化的JSON字符串。
from strawberry.scalars import JSON
@strawberry.type
class Thing:
data: JSON
def create_config(data: JSON) -> Thing:
passhttps://stackoverflow.com/questions/72113851
复制相似问题