Middleware

You can use middleware to affect the evaluation of fields in your schema.

A middleware is any object or function that responds to resolve(next_middleware, *args).

You can read more about middleware on the Graphene docs.

You must enable the GrappleMiddleware for grapple’s field level middleware to work.

GrappleMiddleware

class grapple.middleware.GrappleMiddleware(object)
# settings.py
GRAPHENE = {
    # ...
    "MIDDLEWARE": ["grapple.middleware.GrappleMiddleware"],
}

Examples

Authenticated Middleware

class grapple.middleware.IsAuthenticatedMiddleware(object)

This middleware only continues evaluation if the request is authenticated.

class IsAuthenticatedMiddleware(object):
    def resolve(self, next, root, info, **args):
        if not info.context.user.is_authenticated:
            return None
        return next(root, info, **args)

Anonymous Middleware

class grapple.middleware.IsAnonymousMiddleware(object)

This middleware only continues evaluation if the request is not authenticated.

class IsAnonymousMiddleware(object):
    def resolve(self, next, root, info, **args):
        if not info.context.user.is_anonymous:
            return None
        return next(root, info, **args)