@@ -104,3 +104,78 @@ schema = graphene.Schema(query=Query, subscription=Subscription)
104104```
105105
106106You can see a full example here: https://github.com/graphql-python/graphql-ws/tree/master/examples/flask_gevent
107+
108+
109+ ### Django Channels
110+
111+
112+ First ` pip install channels ` and it to your django apps
113+
114+ Then add the following to your settings.py
115+
116+ ``` python
117+ CHANNELS_WS_PROTOCOLS = [" graphql-ws" , ]
118+ CHANNEL_LAYERS = {
119+ " default" : {
120+ " BACKEND" : " asgiref.inmemory.ChannelLayer" ,
121+ " ROUTING" : " django_subscriptions.urls.channel_routing" ,
122+ },
123+
124+ }
125+ ```
126+
127+ Setup your graphql schema
128+
129+ ``` python
130+ import graphene
131+ from rx import Observable
132+
133+
134+ class Query (graphene .ObjectType ):
135+ hello = graphene.String()
136+
137+ def resolve_hello (self , info , ** kwargs ):
138+ return ' world'
139+
140+ class Subscription (graphene .ObjectType ):
141+
142+ count_seconds = graphene.Int(up_to = graphene.Int())
143+
144+
145+ def resolve_count_seconds (
146+ root ,
147+ info ,
148+ up_to = 5
149+ ):
150+ return Observable.interval(1000 )\
151+ .map(lambda i : " {0} " .format(i))\
152+ .take_while(lambda i : int (i) <= up_to)
153+
154+
155+
156+ schema = graphene.Schema(
157+ query = Query,
158+ subscription = Subscription
159+ )
160+
161+
162+ ````
163+
164+ Setup your schema in settings.py
165+
166+ ```python
167+ GRAPHENE = {
168+ ' SCHEMA' : ' path.to.schema'
169+ }
170+ ```
171+
172+ and finally add the channel routes
173+
174+ ``` python
175+ from channels.routing import route_class
176+ from graphql_ws.django_channels import GraphQLSubscriptionConsumer
177+
178+ channel_routing = [
179+ route_class(GraphQLSubscriptionConsumer, path = r " ^ /subscriptions" ),
180+ ]
181+ ```
0 commit comments