Skip to content

UploadFile file in FastAPI integration appears to be starlette.UploadFile #4007

@Nnonexistent

Description

@Nnonexistent

I'm trying to get a FileUpload instance of proper type inside a resolver in a FastAPI integration of strawberry. However I always get starlette.UploadFile instead of fastapi.UploadFile.

Describe the Bug

I'm following these guides: https://strawberry.rocks/docs/guides/file-upload and
https://strawberry.rocks/docs/integrations/fastapi.

I expect file to be a fastapi.UploadFile. Instead I'm getting starlette.datastructures.UploadFile.

scalar_overrides argument doesn't seems to change anything.

I'm not sure if my configuration is incorrect or it's a bug.

Reproducible example (copy/paste to ipython or similar with all used packages installed):

import io
import json

import httpx
import strawberry
from fastapi import FastAPI, UploadFile
from strawberry.fastapi import GraphQLRouter
from strawberry.file_uploads import Upload


# Arrange


@strawberry.type
class Mutation:
    @strawberry.mutation
    async def handle_file(self, file: Upload) -> str:
        return str(type(file))  # <- will check type here


@strawberry.type
class EmptyQuery:
    nothing: None


schema = strawberry.Schema(
    EmptyQuery,
    Mutation,
    scalar_overrides={UploadFile: Upload},  # <- this changes nothing :(
)

graphql_app = GraphQLRouter(
    schema=schema,
    multipart_uploads_enabled=True,
)
app = FastAPI()
app.include_router(graphql_app, prefix="/api")


# Testing


client = httpx.AsyncClient(transport=httpx.ASGITransport(app=app), base_url="http://test")

query = """
mutation TestMutation($file: Upload!) {
    handleFile (file: $file)
}"""

payload = {
    "operations": (
        None,
        json.dumps({"query": query, "variables": {"file": None}}),
        "application/json",
    ),
    "map": (None, json.dumps({"file": ["variables.file"]}), "application/json"),
    "file": ("test.txt", io.BytesIO(b"")),
}
response = await client.post("/api", files=payload)

print(response.json()["data"]["handleFile"])  # <class 'starlette.datastructures.UploadFile'>

System Information

  • Operating system: ubuntu
  • Python version: 3.11.3
  • Strawberry version (if applicable): 0.282.0

Additional Context

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions