graphene-subscriptions | play GraphQL subscription implementation for Graphene | GraphQL library
kandi X-RAY | graphene-subscriptions Summary
kandi X-RAY | graphene-subscriptions Summary
A plug-and-play GraphQL subscription implementation for Graphene + Django built using Django Channels. Provides support for model creation, mutation and deletion subscriptions out of the box.
Support
Quality
Security
License
Reuse
Top functions reviewed by kandi - BETA
- Handle a websocket message
- Send a result
- Convert to a dictionary
- Send this event
- Return the value of an item
- Initialize an operation from a json dictionary
- Sends subscription event
- Forward websocket connection
- Post deletion of a subscription
- Close websocket connection
graphene-subscriptions Key Features
graphene-subscriptions Examples and Code Snippets
Community Discussions
Trending Discussions on graphene-subscriptions
QUESTION
I'm using Graphene, Django and graphene-subscriptions to define a GraphQL Subscription. I'm trying to receive updates whenever a new Book
with a specific Author
is created. I've followed the getting started guide, and I'm trying to use the following code:
ANSWER
Answered 2020-Jan-22 at 02:31One of the common problems I've run into when defining subscriptions like this is having a type mismatch when filtering by id
.
Because Author
is a string, if event.instance.AuthorID
is a Django primary key (an int
value), then
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install graphene-subscriptions
Install graphene-subscriptions $ pip install graphene-subscriptions
Add graphene_subscriptions to INSTALLED_APPS: # your_project/settings.py INSTALLED_APPS = [ # ... 'graphene_subscriptions' ]
Add Django Channels to your project (see: Django Channels installation docs) and set up Channel Layers. If you don't want to set up a Redis instance in your dev environment yet, you can use the in-memory Channel Layer: # your_project/settings.py CHANNEL_LAYERS = { "default": { "BACKEND": "channels.layers.InMemoryChannelLayer" } }
Add GraphqlSubscriptionConsumer to your routing.py file. # your_project/routing.py from channels.routing import ProtocolTypeRouter, URLRouter from django.urls import path from graphene_subscriptions.consumers import GraphqlSubscriptionConsumer application = ProtocolTypeRouter({ "websocket": URLRouter([ path('graphql/', GraphqlSubscriptionConsumer) ]), })
Connect signals for any models you want to create subscriptions for # your_app/signals.py from django.db.models.signals import post_save, post_delete from graphene_subscriptions.signals import post_save_subscription, post_delete_subscription from your_app.models import YourModel post_save.connect(post_save_subscription, sender=YourModel, dispatch_uid="your_model_post_save") post_delete.connect(post_delete_subscription, sender=YourModel, dispatch_uid="your_model_post_delete") # your_app/apps.py from django.apps import AppConfig class YourAppConfig(AppConfig): name = 'your_app' def ready(self): import your_app.signals
Define your subscriptions and connect them to your project schema #your_project/schema.py import graphene from your_app.graphql.subscriptions import YourSubscription class Query(graphene.ObjectType): base = graphene.String() class Subscription(YourSubscription): pass schema = graphene.Schema( query=Query, subscription=Subscription )
Support
Reuse Trending Solutions
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesStay Updated
Subscribe to our newsletter for trending solutions and developer bootcamps
Share this Page