File Upload

All Strawberry integrations support multipart uploads as described in the GraphQL multipart request specification. This includes support for uploading single files as well as lists of files.

Uploads can be used in mutations via the Upload scalar. The type passed at runtime depends on the integration:

ASGI

Since ASGI uses asyncio for communication the resolver must be async as well.

Example:

import typing
import strawberry
from strawberry.file_uploads import Upload
@strawberry.type
class Mutation:
@strawberry.mutation
async def read_file(self, file: Upload) -> str:
return await file.read()
@strawberry.mutation
async def read_files(self, files: typing.List[Upload]) -> typing.List[str]:
contents = []
for file in files:
content = (await file.read()).decode()
contents.append(content)
return contents

Sanic / Flask / Django / AIOHTTP

Example:

import typing
import strawberry
from strawberry.file_uploads import Upload
@strawberry.type
class Mutation:
@strawberry.mutation
def read_file(self, file: Upload) -> str:
return file.read().decode()
@strawberry.mutation
def read_files(self, files: typing.List[Upload]) -> typing.List[str]:
contents = []
for file in files:
content = file.read().decode()
contents.append(content)
return contents

Was this helpful?

Edit on Github

Newsletter 💌

Do you want to receive the latest updates on Strawberry? Subscribe to our newsletter!