question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Oneshot middleware

See original GitHub issue

Hi! Currently I’m trying to add some one-shot-middlewares to the graphene schema (e.g. version check, policy check). The problem is, that the middleware get’s fired for each field instead for a single request. I can’t use the DjangoMiddlewares because the error doesn’t get wrapped into the graphql response error field

I found 2 solutions for this problem:

  1. I set a variable _skip=False in the middleware which is toggled to True before the first next(...) call, e.g.:
lass LatestPolicyAcceptedMiddleware:

    def __init__(self, *args, **kwargs):
        super(LatestPolicyAcceptedMiddleware, self).__init__(*args, **kwargs)
        self._skip = False

    def resolve(self, next, root, info, **kwargs):
        if self._skip:
            return next(root, info, **kwargs)
        ...
        self._skip = True
        return next(root, info, **kwargs)

Well - that works but feels really odd.

  1. Patching the GraphQLView.get_response method like this:
    def get_response(self, request, data, show_graphiql=False):
        try:
            check_min_native_version(request)
            latest_policy_check(request)
            return super(PatchedGraphQLView, self).get_response(request, data, show_graphiql)
        except (PolicyRequiredException, UpgradeNeededException) as e:
            result = self.json_encode(request, {"errors": [self.format_error(e)], "data": None}, pretty=show_graphiql)
            status_code = e.code
            return result, status_code

This feels also odd, but a way cleaner.

So - what’s the best way to integrate a one-time middleware? It feels like the framework is missing this essential feature.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:9

github_iconTop GitHub Comments

4reactions
melvinkcxcommented, Dec 23, 2019

Graphene middleware is called at a field-level, each field resolution would trigger calls to all middleware. I’m not sure if graphene will support request-level middleware, I just created an issue: https://github.com/graphql-python/graphene/issues/1117

For now, what I do is add an attribute into info.context object and toggle that to indicate should the middleware logic be executed.

class AuthMiddleware:
    def resolve(self, next, root, info, **kwargs):
        if not hasattr(info.context, "auth_checked") and info.context.auth_checked:
            # Check user auth
            info.context.auth_checked = True
            
        return next(root, info, **kwargs)
0reactions
melvinkcxcommented, Feb 19, 2020

@richin13 @MrFoxes This issue is fixed and is now available in graphql-utilities>=0.2.0.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Redux-middleware-oneshot NPM | npm.io
Create Redux actions from arbitraty sources out of middlewares. Installation. npm install --save redux-middleware-oneshot. Usage. // In this example, we want to ...
Read more >
michaelcontento/redux-middleware-oneshot - GitHub
Create Redux actions from arbitraty sources out of middlewares. Deprecated - No longer maintained. My focus has left the node / react ecosystem...
Read more >
redux-middleware-oneshot - npm package - Snyk
Is redux-middleware-oneshot safe to use? The npm package redux-middleware-oneshot was scanned for known vulnerabilities and missing license, and ...
Read more >
Operation-level/One-shot Middleware
Operation-level/One-shot Middleware¶. The problem of using middleware in graphql-core in Python is it run at field-level. If you use a middleware to perform ......
Read more >
michaelcontento/redux-middleware-oneshot - Code Climate
Projects with Technical Debt Ratios below 5% are rated A. 0 mins. Estimated time to resolve technical debt issues. Test coverage.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found