Closed blazewicz closed 1 year ago
Correct, documentation should be fixed to show that middlewares should be passed to manager as list. Could you please contribute a PR fixing this?
Starting with Ariadne 0.18 we split middleware option into middlewares (which we expect to be a list of middlewares) and middleware manager class. I'll put this for that release so I remember to check if MyPy is complaining after changes.
I've got this fixed in the PR, but there's a gotcha in mypy that it compares argument names on Middleware
prototype:
class Middleware(Protocol):
def __call__(
self,
resolver: Resolver,
obj: Any,
info: GraphQLResolveInfo,
**kwargs: Any,
) -> Any:
...
# Works
def example_middleware(resolver, obj: Any, info: GraphQLResolveInfo, **kwargs: Any):
return resolver(obj, info, **kwargs)
# Produces error:
def example_middleware(next_, parent: Any, info: GraphQLResolveInfo, **kwargs: Any):
return resolver(obj, info, **kwargs)
incompatible type "Callable[[Any, Any, GraphQLResolveInfo, KwArg(Any)], Any]"; expected "Middleware"
Mypy has a way for specifying arg as positional only with double underscore, but if you mix it with **kwargs
its ignored 😞
Adding Middlewares to ASGI GraphQL as documented in https://ariadnegraphql.org/docs/middleware#custom-middleware-example doesn't work.
This is the example from docs at the time of writing this issue:
Firts,
mypy
will throw following error:Second, running this code will throw following error:
By looking at the expected type I've tried:
It does satisfy
mypy
, but when the code is run the middlewares aren't ever getting called.By looking at the test suite:
https://github.com/mirumee/ariadne/blob/9a6cd62c9172b5e43cc62cccdec9f28294d7238d/tests/asgi/test_configuration.py#L589..L621
For example in this part:
I've noted, that you use a different syntax. This one is working, but it contradicts the doc and doesn't work with static type checking.
From
mypy
you will get following error:Workaround that I've found is to do:
I'd suggest adding type annotations to the test suite and run mypy on it.