parsing the object from a file upload in Strawberry-FastAPI

190 Views Asked by At

I am following this tutorial on Strawberry and FastAPI on managing uploaded files (I need to read a file of a specific format). This is the tutorial:

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:
        return (await file.read()).decode("utf-8")

The page for the tutorial is here: https://strawberry.rocks/docs/guides/file-upload

To test this, I use a CURL invocation:

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.docx

I actually have code to parse word docs in Python but they need a file path to load the file. Is there a better way than trying to write the file to disk and then loading it?

0

There are 0 best solutions below