Skip to content

Commit d42c840

Browse files
Fixed order of middlewares.
1 parent bde378d commit d42c840

File tree

2 files changed

+18
-7
lines changed

2 files changed

+18
-7
lines changed

README.md

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -503,8 +503,17 @@ processing. For that define `middleware` setting of your
503503
`GraphqlWsConsumer` subclass, like this:
504504

505505
```python
506-
async def threadpool_for_synch_resolvers(next_middleware, root, info, *args, **kwds):
507-
"""Offload synchronous resolvers to the threadpool."""
506+
async def threadpool_for_sync_resolvers(next_middleware, root, info, *args, **kwds):
507+
"""Offload synchronous resolvers to the threadpool.
508+
509+
This middleware should always be the last in the middlewares calls
510+
stack and the closest to the real resolver. If this middleware is
511+
not the last it will check the next middleware to call instead of
512+
real resolver.
513+
514+
The first middleware in the middlewares list will be the closest to
515+
the resolver.
516+
"""
508517
# Invoke next middleware.
509518
if asyncio.iscoroutinefunction(next_middleware):
510519
result = await next_middleware(root, info, *args, **kwds)
@@ -514,7 +523,7 @@ async def threadpool_for_synch_resolvers(next_middleware, root, info, *args, **k
514523

515524
class MyGraphqlWsConsumer(channels_graphql_ws.GraphqlWsConsumer):
516525
...
517-
middleware = [threadpool_for_synch_resolvers]
526+
middleware = [threadpool_for_sync_resolvers]
518527
```
519528

520529
It is recommended to write asynchronous middlewares. But synchronous

channels_graphql_ws/graphql_ws_consumer.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,8 @@ class GraphqlWsConsumer(ch_websocket.AsyncJsonWebsocketConsumer):
138138
# result = await result
139139
# return result
140140
# ```
141+
# The first middleware in the middlewares list will be the closest
142+
# to the resolver in the middlewares call stack.
141143
# For more information read docs:
142144
# - https://docs.graphene-python.org/en/latest/execution/middleware/#middleware
143145
# - https://graphql-core-3.readthedocs.io/en/latest/diffs.html#custom-middleware
@@ -604,12 +606,12 @@ async def unbound_root_middleware(*args, **kwds):
604606
)
605607

606608
# NOTE: Middlewares order is important, root middleware
607-
# should always be the closest to the real resolver (first
608-
# in the middleware list).
609-
middlewares = []
609+
# should always be the farest from the real resolver (last
610+
# in the middleware list). Because we want to calculate
611+
# resolver execution time with middlewares included.
612+
middlewares = list(self.middleware)
610613
if self.warn_resolver_timeout is not None:
611614
middlewares.append(unbound_root_middleware)
612-
middlewares.extend(self.middleware)
613615
middleware_manager: Optional[graphql.MiddlewareManager] = None
614616
if middlewares:
615617
middleware_manager = graphql.MiddlewareManager(*middlewares)

0 commit comments

Comments
 (0)