strawberry-graphql / strawberry

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

multipart/form-data requests broken in version 0.243.1 (returning `Unsupported content type`) #3648

Closed trannon closed 4 days ago

trannon commented 5 days ago

Describe the Bug

I recently updated from Strawberry version 0.237.2 to 0.243.1. Under the prior version, I was able to submit queries to my graphql endpoints using a multipart/form-data POST like the following:

curl --location '0.0.0.0:8000/graphql' \
--form 'operations="{\"query\":\"{books{title, author}}\"}"'

However, under the newer version I started receiving a "Unsupported content type" 400 error with the same request.

This format is needed to send files and more complicated request use cases (with variables).

System Information

Steps to reproduce

This can be reproduced by creating a simple strawberry endpoint by following the "Getting Started" docs to create a schema.py file like this:

import typing
import strawberry

@strawberry.type
class Book:
    title: str
    author: str

@strawberry.type
class Query:
    books: typing.List[Book]

def get_books():
    return [
        Book(
            title="The Great Gatsby",
            author="F. Scott Fitzgerald",
        ),
    ]

@strawberry.type
class Query:
    books: typing.List[Book] = strawberry.field(resolver=get_books)

schema = strawberry.Schema(query=Query)

Start the server by running strawberry server schema.

Under both versions (0.237.2 and 0.243.1), the following curl request WORKS:

curl --location '0.0.0.0:8000/graphql' \
--header 'Content-Type: application/json' \
--data '{"query": "{books{title, author}}"}'

The following request format works under the older version 0.237.2, but does NOT work under the newer version 0.243.1:

curl --location '0.0.0.0:8000/graphql' \
--form 'operations="{\"query\":\"{books{title, author}}\"}"'

Upvote & Fund

Fund with Polar

patrick91 commented 4 days ago

hi @trannon, sorry about that, but have disabled multipart uploads by default, you can check the breaking change here: https://strawberry.rocks/docs/breaking-changes/0.243.0

trannon commented 4 days ago

I see, thank you for the quick response.

In my case, I am using the FastAPI integration and therefore I was able to restore the prior behavior by setting the multipart_uploads_enabled flag as indicated in the new integration docs:

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

This is for an internal tool and therefore the security implications are not a factor.

patrick91 commented 4 days ago

@trannon thanks for getting back to us! Glad it's all good now 😊