Skip to content

Middlewares are executing in reverse order #1499

Open
@tbhaxor

Description

@tbhaxor
  • 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

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

These two middlewares are defined as following

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

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions