Skip to content

Commit

Permalink
started on some docs for django channels
Browse files Browse the repository at this point in the history
  • Loading branch information
colanconnon committed Dec 22, 2017
1 parent b6f6fb0 commit 6110b29
Showing 1 changed file with 75 additions and 0 deletions.
75 changes: 75 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,3 +104,78 @@ schema = graphene.Schema(query=Query, subscription=Subscription)
```

You can see a full example here: https://github.com/graphql-python/graphql-ws/tree/master/examples/flask_gevent


### Django Channels


First `pip install channels` and it to your django apps

Then add the following to your settings.py

```python
CHANNELS_WS_PROTOCOLS = ["graphql-ws", ]
CHANNEL_LAYERS = {
"default": {
"BACKEND": "asgiref.inmemory.ChannelLayer",
"ROUTING": "django_subscriptions.urls.channel_routing",
},

}
```

Setup your graphql schema

```python
import graphene
from rx import Observable


class Query(graphene.ObjectType):
hello = graphene.String()

def resolve_hello(self, info, **kwargs):
return 'world'

class Subscription(graphene.ObjectType):

count_seconds = graphene.Int(up_to=graphene.Int())


def resolve_count_seconds(
root,
info,
up_to=5
):
return Observable.interval(1000)\
.map(lambda i: "{0}".format(i))\
.take_while(lambda i: int(i) <= up_to)



schema = graphene.Schema(
query=Query,
subscription=Subscription
)


````

Setup your schema in settings.py

```python
GRAPHENE = {
'SCHEMA': 'path.to.schema'
}
```

and finally add the channel routes

```python
from channels.routing import route_class
from graphql_ws.django_channels import GraphQLSubscriptionConsumer

channel_routing = [
route_class(GraphQLSubscriptionConsumer, path=r"^/subscriptions"),
]
```

0 comments on commit 6110b29

Please sign in to comment.