strawberry
strawberry copied to clipboard
Using the ASGI app as a Starlette route throws typing errors
Describe the Bug
The following code is more or less a copy of what appears in the docs:
from starlette.applications import Starlette
from strawberry.asgi import GraphQL
from .schema import schema
app = Starlette()
graphql = GraphQL[None, None](schema)
app.add_route("/graphql", route=graphql)
However, it raises the following typing error:
app.py:8: error: Argument 2 to "add_route" of "Starlette" has incompatible type "GraphQL[Any, Any]"; expected "Callable[[Request], Awaitable[Response] | Response]" [arg-type]
app.py:8: note: "GraphQL[Any, Any].__call__" has type "Callable[[Arg(MutableMapping[str, Any], 'scope'), Arg(Callable[[], Awaitable[MutableMapping[str, Any]]], 'receive'), Arg(Callable[[MutableMapping[str, Any]], Awaitable[None]], 'send')], Coroutine[Any, Any, None]]"
While the code does actually run and appears to function correctly, Mypy is flagging incompatible signatures.
The signature for the route
parameter for the add_route
method:
(Request) -> (Awaitable[Response] | Response)
The signature of the GraphQL.__call__
method:
(Scope, Receive, Send) -> (None)
The latter signature is the same as the signature for Starlette middleware. I'm presuming Mypy is getting confused, or something isn't properly deliniated to Mypy and it's assuming something it shouldn't be, as the code does work.
It's also worth noting that the documentation should be updated to use full type annotations.
System Information
- Operating system: MacOS
- Strawberry version (if applicable): 0.235.0
Additional Context
Starlette version 0.37.2.
Upvote & Fund
- We're using Polar.sh so you can upvote and help fund this issue.
- We receive the funding once the issue is completed & confirmed by you.
- Thank you in advance for helping prioritize & fund our backlog.