Little help with subscription middleware
@drsjmiddleton y travaille déjà.
Depuis le 6/3/2020.
Évaluation
- Difficulté
- 4/5
- Temps estimé
- 3-5 jours
- Accessibilité débutants
- 22/100
- Type d'issue
- Bug
- Clarté
- À clarifier
- Activité
- À l'abandon
- Stack technique
- python
- Domaine
- api, backend-api-design
Piste de recherche
Commencez par dixtionary/middleware/authentication.py au niveau de authorize_ws et par dixtionary/extensions/graphql.py autour du monkey-patching du serveur de subscriptions WebSocket. Comparez les points d’ancrage Query, Mutation et Subscription tout en reproduisant la requête de subscription sur la branche liée. C’est terminé lorsque l’authentification WebSocket fonctionne sans objets rx transformés en chaînes et sans contourner l’autorisation pour les champs de Subscription.
Rédigé par le modèle d'indexation à partir du texte de l'issue.
Description
Hi,
I've been playing around with graphql in this little test server. It's the server side implementation for a pictionary type game. I've managed to get queries, mutations and subscriptions working with python 3 style defs, asyncdefs and asyncgenerators.
I've used middleware on the http query/mutation requests without issue, however, getting middleware to play nice with the subscriptions has proven difficult for me and I was wondering if I could ask for some help.
I have been doing authentication for websocket connections by requiring a token to be provided along with any subscriptions that require auth. However, I'd like to move this to middleware and have the token handed up in connection params in the auth. This is the branch I stash the connection_params and pass them into the context.
In that same spot I add a auth middleware to handle authenticating websocket connections via those connection_params. Wrapping in a MiddlewareManager as suggested by @dfee in this issue.
The implantation of the authorize_ws middleware looks like this:
async def _authorize_ws_subscription(next, root, info, **args):
token = info.context["connection_params"].get("authorization")
await _authorize(token=token, info=info)
async for msg in next(root, info, **args):
yield msg
async def _authorize_ws_query_mutation(next, root, info, **args):
token = info.context["connection_params"].get("authorization")
await _authorize(token=token, info=info)
result = next(root, info, **args)
if inspect.isawaitable(result):
return await result
else:
return result
def authorize_ws(next, root, info, **args):
if info.operation.operation == 'subscription':
return _authorize_ws_subscription(next, root, info, **args)
else:
return _authorize_ws_query_mutation(next, root, info, **args
However, this info.operation.operation == 'subscription' check does not appear to be sufficient for determining how to handle the request and results in my subscription websocket spitting out stringified rx objects for values e.g.:
{
"id": "6",
"type": "start",
"payload": {
"variables": {
"uuid": "<rx.core.anonymousobservable.AnonymousObservable object at 0x106d8c7f0>"
},
"extensions": {},
"operationName": "roomDeleted",
"query": "subscription roomDeleted($uuid: String!) {\n roomDeleted(uuids: [$uuid]) {\n uuid\n __typename\n }\n}\n"
}
}
What it appears to be is that not all subscription operations expect a asyncgen as some subscription calls are for attr_resolve and not the base subscription itself. Anyway, putting in this hacky line fixes it... (where my base Subscription graphene.ObjectType is named Subscription). Though of course this then skips authentication for those calls.
def authorize_ws(next, root, info, **args):
if info.operation.operation == 'subscription':
if not str(next).startswith('Subscription.'):
return next(root, info, **args)
return _authorize_ws_subscription(next, root, info, **args)
else:
return _authorize_ws_query_mutation(next, root, info, **args)
Would appreciate any pointers. Sorry for posting links to a codebase instead of a smaller snippet, I'm not sure however where in the chain I'm doing things incorrectly (as maybe it's my resolvers that are written incorrectly for the middleware and not vice-versa).
Here's a few anchors around the codebase:
Query: dixtionary.model.query.Query
Mutation: dixtionary.model.mutations.Mutation
Subscription: dixtionary.model.subscriptions.Subscription
Middleware: dixtionary.middleware.authentication.authorize_ws
WsLibSubscriptionServer Monkey-patching: dixtionary.extensions.graphql
Thanks to anyone that indulges such a long post, and Sorry.
- Langage dominant
- Python
- Étoiles
- 371
- Forks
- 174
- Métriques de merge des PR
- Aucune PR mergée en 30 j
Préparer son environnement
Ce projet ne fournit ni conteneur de développement, ni Dockerfile, ni guide de contribution : l'installation est à votre charge. Commencez par son README, et consultez notre guide de la première contribution pour les étapes générales.
Par où commencer
- Lisez l'issue en entier, puis le guide de contribution du projet.
- Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
- Forkez le dépôt et travaillez sur une branche.
- Ouvrez une pull request qui référence le numéro de l'issue.
Autres issues de graphql-python/graphql-core-legacy
-
Difficulté 2/5 1-3 heures Accessibilité débutants 68/100
-
Quiver sourceOuverte
Difficulté 5/5 Plus d'une semaine Accessibilité débutants 20/100
graphql-python/graphql-core-legacy#286 · 3 réactions ·
-
Difficulté 4/5 3-5 jours Accessibilité débutants 35/100
-
Difficulté 3/5 1-2 jours Accessibilité débutants 35/100
graphql-python/graphql-core-legacy#280 · 2 commentaires · 1 réaction ·
-
Add support for custom validators in default GraphQL backendPeut-être pris @JoaRiski l’a pris il y a 2441 jours. Ouverte
Difficulté 4/5 3-5 jours Accessibilité débutants 35/100
Toutes les issues de graphql-python/graphql-core-legacy
Issues similaires
-
Difficulté 2/5 1-3 heures Accessibilité débutants 75/100
sususweet/midea_auto_cloud#285 ·
Les mainteneurs répondent en général sous 1 jour
-
Short positive Table maxlen values produce oversized cellsPeut-être pris @Gambit-Checkmate l’a pris aujourd’hui. Ouverte
Difficulté 2/5 1-3 heures Accessibilité débutants 72/100
Les mainteneurs répondent en général sous 18 jours
-
Suggestion: Add requirements.txtOuverte
Difficulté 1/5 1-3 heures Accessibilité débutants 68/100
MangasMango/bound-to-rot#41 · 1 commentaire ·
-
breaking change
Difficulté 2/5 1-3 heures Accessibilité débutants 82/100
Les mainteneurs répondent en général sous 1 jour
-
Word viper missingOuverte
Difficulté 1/5 Moins d'une heure Accessibilité débutants 68/100
dwyl/english-words#244 ·