Gustavo Lopes
Gustavo Lopes

Reputation: 4174

How to use middleware after resolver in graphene?

Using Graphql in Node, you can use middlewares BEFORE or AFTER the resolver, using, for example, Prisma.

In Python, using Graphene, I could only find a way to use middleware BEFORE the resolver.

Is there a way to use middlewares AFTER the resolver in Python?

Upvotes: 5

Views: 2297

Answers (2)

Victor Arnaud
Victor Arnaud

Reputation: 21

You can use Django middlewares to format output graphql query or mutation

class FormatOutputMiddleware:
    def __init__(self, get_response):
        self.get_response = get_response

    def __call__(self, request):
        response = self.get_response(request)
        # Get the graphql response
        data = json.loads(response.content.decode("UTF-8"))

        # do something with data

        response.content = json.dumps(data).encode("UTF-8")
        return response

Upvotes: 0

frozenOne
frozenOne

Reputation: 554

How about:

class SomeMiddleware(object):
    def resolve(self, next, root, info, **args):
        next_node = next(root, info, **args)
        ...logic...
        return next_node

Upvotes: 2

Related Questions