Oneshot middleware
See original GitHub issueHi!
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:
- I set a variable
_skip=False
in the middleware which is toggled toTrue
before the firstnext(...)
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.
- 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:
- Created 4 years ago
- Reactions:1
- Comments:9
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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.@richin13 @MrFoxes This issue is fixed and is now available in graphql-utilities>=0.2.0.