在本教程中,我实现了文件上传的一个变体:

https://strawberry.rocks/docs/guides/file-upload

代码基本上是这样的:

import typing
import strawberry
from strawberry.file_uploads import Upload


@strawberry.input
class FolderInput:
    files: typing.List[Upload]

@strawberry.type
class Mutation:
    @strawberry.mutation
    async def read_file(self, file: Upload) -> str:
        [do some processing]
        return "Processing of PDF done!"

我使用下面的调用对其进行测试:

curl localhost:8000/graphql \
  -F operations='{ "query": "mutation($file: Upload!){ readFile(file: $file) }", "variables": { "file": null } }' \
  -F map='{ "file": ["variables.file"] }' \
  -F file=@/path/to/some_file.pdf

CURL调用调用readFile,而Strawberry中的Mutations 使用read_file,映射在哪里完成,我们能控制吗?我想要多个读取方法(例如read_typeA_pdfread_typeB_pdf等).

推荐答案

据我所知,the GraphQL mutation namethe actual Python method name之间的映射是由Strawberry库本身处理的

根据您的示例,我认为GraphQLMutations 名称是readFile,而Python方法名称是read_file.Strawberry使用snake_case命名约定将GraphQL操作名称转换为Python方法名称

您可以直接try ,如下所示

@strawberry.type
class Mutation:
    @strawberry.mutation
    async def read_typeA_pdf(self, file: Upload) -> str:
        return "Processing of Type A PDF done!"

    @strawberry.mutation
    async def read_typeB_pdf(self, file: Upload) -> str:
        return "Processing of Type B PDF done!"

定义了这些方法后,您现在可能可以使用以下GraphQLMutations 名称来调用它们,如下所示:

用A型PDF文件调用read_typeA_pdfMutations

curl localhost:8000/graphql \
  -F operations='{ "query": "mutation($file: Upload!){ readTypeAPdf(file: $file) }", "variables": { "file": null } }' \
  -F map='{ "file": ["variables.file"] }' \
  -F file=@/path/to/typeA.pdf

用B型PDF文件调用read_typeB_pdfMutations :

curl localhost:8000/graphql \
  -F operations='{ "query": "mutation($file: Upload!){ readTypeBPdf(file: $file) }", "variables": { "file": null } }' \
  -F map='{ "file": ["variables.file"] }' \
  -F file=@/path/to/typeB.pdf

您可以将/path/to/typeA.pdf/path/to/typeB.pdf分别替换为指向类型A和类型B的PDF文件的实际路径.

https://strawberry.rocks/docs/general/mutations

希望这能帮上忙.

Python-3.x相关问答推荐

使用Python装载. iso文件

Heroku 中的未知错误代码缺少一个或多个参数

如何使用正则表达式通过反向搜索从链接中获取特定文本

selenium 无法执行网站上最简单的功能

为什么 get_form 方法中小部件的更改没有反映 Django 管理站点中的更改

不同的焦点顺序和堆叠顺序 tkinter

Pandas 转换为日期时间

Pandas 窗口聚合两个排序表

是否可以将多个 if 转换为数组?

如何将数据框中的每一行转换为具有属性的 node ?

具有函数值的 Python 3 枚举

将字符串表示与使用整数值的枚举相关联?

判断对 python 3 支持的要求

在数据类中创建类变量的正确方法

asyncio.Semaphore RuntimeError: Task got Future 附加到不同的循环

如何通过命令行将数组传递给python

Linux Mint 上的 Python3 错误没有名为蓝牙的模块

Python的max函数有多高效

如何在 Pandas 中的超 Big Data 框上创建数据透视表

Beautifulsoup 的单元测试失败