strawberry-graphql / strawberry

A GraphQL library for Python that leverages type annotations 🍓
https://strawberry.rocks
MIT License
4.02k stars 533 forks source link

multipart upload struggle #3655

Closed itsklimov closed 1 month ago

itsklimov commented 1 month ago

I am trying to make the file upload work and no luck yet I got back to the example on https://strawberry.rocks/docs/guides/file-upload#sending-file-upload-requests but just copy past multi file requests from postman returns "Unsupported content type"

Describe the Bug

System Information

Additional Context

python code

@strawberry.mutation
def read_files(self, files: List[Upload]) -> List[str]:
    print(f"Received read_files mutation. Number of files: {len(files)}")
    contents = []
    for file in files:
        content = file.read().decode("utf-8")
        contents.append(content)
    return contents

curl --location 'localhost:7675/graphql' \ --form 'operations="{ \"query\": \"mutation(\$files: [Upload!]!) { readFiles(files: \$files) }\", \"variables\": { \"files\": [null, null] } }"' \ --form 'map="{\"file1\": [\"variables.files.0\"], \"file2\": [\"variables.files.1\"]}"' \ --form 'file1=@"/Users/its/Documents/roll.csv"' \ --form 'file2=@"/Users/its/Documents/dump.csv"'

Request Body operations: "{ "query": "mutation($files: [Upload!]!) { readFiles(files: $files) }", "variables": { "files": [null, null] } }" map: "{"file1": ["variables.files.0"], "file2": ["variables.files.1"]}" file1: undefined file2: undefined Response Headers date: Tue, 01 Oct 2024 15:10:58 GMT server: uvicorn content-length: 24 content-type: text/plain; charset=utf-8 Response Body Unsupported content type

response Unsupported content type

Upvote & Fund

Fund with Polar

patrick91 commented 1 month ago

@itsklimov sorry about that! we recently release a change that disabled multipart uploads by default you can see it here: https://strawberry.rocks/docs/breaking-changes/0.243.0

you need to update your view config, here's how can do it with django: https://strawberry.rocks/docs/integrations/django#options (the param should be the same for all the integrations)

I'll keep this open so we can update that doc page :)

itsklimov commented 1 month ago

as I do not use Django, not much experince with graphql

@itsklimov sorry about that! we recently release a change that disabled multipart uploads by default you can see it here: https://strawberry.rocks/docs/breaking-changes/0.243.0

you need to update your view config, here's how can do it with django: https://strawberry.rocks/docs/integrations/django#options (the param should be the same for all the integrations)

I'll keep this open so we can update that doc page :)

from fastapi import FastAPI, UploadFile, File
from strawberry.fastapi import GraphQLRouter

from src.config import settings
from src.schemas.graphql import schema
from src.utils.logger import logger

async def lifespan(app: FastAPI):
    logger.info("GraphQL File Server is starting...")
    yield
    logger.info("GraphQL File Server is shutting down...")

app = FastAPI(title="GraphQL File Server", lifespan=lifespan)

graphql_app = GraphQLRouter(schema)
app.include_router(graphql_app, prefix="/graphql")

if __name__ == "__main__":
    import uvicorn

    uvicorn.run(
        "app:app",
        host=settings.host,
        port=settings.port,
        reload=True,
    )

Can you advice on how to adjust app to enable it?

patrick91 commented 1 month ago

This should be enough to make it work:

-graphql_app = GraphQLRouter(schema)
+graphql_app = GraphQLRouter(schema, multipart_uploads_enabled=True)
itsklimov commented 1 month ago

thanks resolved, do you want me to resolve the ticket or leave it opened

patrick91 commented 1 month ago

let's leave it open, I think we should update the guide