graphql-python / graphql-python/graphene-django

Middlewares are executing in reverse order

Open
#1,499 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
Python
Stars
4.4k
Forks
760
PR merge metrics
No merged PRs in 30d

Description

* **What is the current behavior?**

I am integrating it with the Django and middleware list is not obeying the ascending order of the list. I have to add `[::-1]` to make it coherent.

* **If the current behavior is a bug, please provide the steps to reproduce and if possible a minimal demo of the problem** via
a github repo, https://repl.it or similar.

Create two middleware and add print statements in it. 1st indexed middleware will execute first and then followed by 0th index middleware

```py
GRAPHENE = {
"SCHEMA": "backend.graphql.schema",
"MIDDLEWARE": [
"backend.graphql.DebugMiddleware",
"backend.graphql.RequestAuthInjectorMiddleware",
],
}
```

These two middlewares are defined as following

```py
class RequestAuthInjectorMiddleware:
def resolve(self, next, root, info, **kwargs):
print("Request Auth Injector")
return next(root, info, **kwargs)

class DebugMiddleware(DjangoDebugMiddleware):
def resolve(self, next, root, info, *args, **kwargs):
print("Debug Middleware")
return super().resolve(next, root, info, **kwargs)
```
**Output**
```
Request Auth Injector
Debug Middleware
```

* **What is the expected behavior?**

It should run the middleware in same order as provided in the list. For example, here is the hotfix

```diff
GRAPHENE = {
"SCHEMA": "viewgur_backend.graphql.schema",
"MIDDLEWARE": [
"viewgur_backend.graphql.DebugMiddleware",
"viewgur_backend.graphql.RequestAuthInjectorMiddleware",
- ],
+ ][::-1],
}
```

And now the output will be accurate

```
Debug Middleware
Request Auth Injector
```

* **What is the motivation / use case for changing the behavior?**

It is confusing to anyone newbie (like I was just few mins ago) to find out the apparent nature

* **Please tell us about your environment:**

- Version: v3.3
- Platform: Linux

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.