-
Notifications
You must be signed in to change notification settings - Fork 86
/
example.py
253 lines (191 loc) · 8.32 KB
/
example.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
# Copyright (C) DATADVANCE, 2010-2023
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
"""Simple example of the DjangoChannelsGraphqlWs."""
import pathlib
from collections import defaultdict
from typing import Any, DefaultDict, Dict, List
import channels
import channels.auth
import channels.db
import channels.routing
import django
import django.contrib.admin
import django.contrib.auth
import django.core.asgi
import django.urls
import graphene
import graphene_django.views
import graphql
import channels_graphql_ws
# It is OK, Graphene works this way.
# pylint: disable=unsubscriptable-object,invalid-name
# Fake storage for the chat history. Do not do this in production, it
# lives only in memory of the running server and does not persist.
chats: DefaultDict[str, List[Dict[str, Any]]] = defaultdict(list)
# ---------------------------------------------------------------------- TYPES & QUERIES
class Message( # type: ignore
graphene.ObjectType, default_resolver=graphene.types.resolver.dict_resolver
):
"""Message GraphQL type."""
chatroom = graphene.String()
text = graphene.String()
sender = graphene.String()
class Query(graphene.ObjectType):
"""Root GraphQL query."""
history = graphene.List(Message, chatroom=graphene.String())
def resolve_history(self, info, chatroom):
"""Return chat history."""
del info
return chats[chatroom] if chatroom in chats else []
# ---------------------------------------------------------------------------- MUTATIONS
class SendChatMessage(graphene.Mutation, name="SendChatMessagePayload"): # type: ignore
"""Send chat message."""
ok = graphene.Boolean()
class Arguments:
"""Mutation arguments."""
chatroom = graphene.String()
text = graphene.String()
def mutate(self, info, chatroom, text):
"""Mutation "resolver" - store and broadcast a message."""
# Here we identify the sender by `sessionid` cookie.
user_session_key = info.context.session.session_key
# Store a message.
chats[chatroom].append(
{"chatroom": chatroom, "text": text, "sender": user_session_key}
)
# Notify subscribers.
OnNewChatMessage.new_chat_message(
chatroom=chatroom, text=text, sender=user_session_key
)
return SendChatMessage(ok=True)
class Mutation(graphene.ObjectType):
"""Root GraphQL mutation."""
send_chat_message = SendChatMessage.Field()
# ------------------------------------------------------------------------ SUBSCRIPTIONS
class OnNewChatMessage(channels_graphql_ws.Subscription):
"""Subscription triggers on a new chat message."""
sender = graphene.String()
chatroom = graphene.String()
text = graphene.String()
class Arguments:
"""Subscription arguments."""
chatroom = graphene.String()
def subscribe(self, info, chatroom=None):
"""Client subscription handler."""
del info
# Specify the subscription group client subscribes to.
return [chatroom] if chatroom is not None else None
def publish(self, info, chatroom=None):
"""Called to prepare the subscription notification message."""
# The `self` contains payload delivered from the `broadcast()`.
new_msg_chatroom = self["chatroom"]
new_msg_text = self["text"]
new_msg_sender = self["sender"]
# Method is called only for events on which client explicitly
# subscribed, by returning proper subscription groups from the
# `subscribe` method. So he either subscribed for all events or
# to particular chatroom.
assert chatroom is None or chatroom == new_msg_chatroom
# Avoid self-notifications.
# The sender is identified by a `sessionid` cookie.
user_session_key = info.context.channels_scope["session"].session_key
if user_session_key == new_msg_sender:
return None
return OnNewChatMessage(
chatroom=chatroom, text=new_msg_text, sender=new_msg_sender
)
@classmethod
def new_chat_message(cls, chatroom, text, sender):
"""Auxiliary function to send subscription notifications.
It is generally a good idea to encapsulate broadcast invocation
inside auxiliary class methods inside the subscription class.
That allows to consider a structure of the `payload` as an
implementation details.
"""
cls.broadcast(
group=chatroom,
payload={"chatroom": chatroom, "text": text, "sender": sender},
)
class Subscription(graphene.ObjectType):
"""GraphQL subscriptions."""
on_new_chat_message = OnNewChatMessage.Field()
# ----------------------------------------------------------- GRAPHQL WEBSOCKET CONSUMER
async def demo_middleware(next_middleware, root, info, *args, **kwds):
"""Demo GraphQL middleware.
For more information read:
https://docs.graphene-python.org/en/latest/execution/middleware/#middleware
"""
print("Demo middleware report")
print(" operation :", info.operation.operation)
print(" name :", info.operation.name.value)
# Invoke next middleware.
result = next_middleware(root, info, *args, **kwds)
if graphql.pyutils.is_awaitable(result):
result = await result
return result
class MyGraphqlWsConsumer(channels_graphql_ws.GraphqlWsConsumer):
"""Channels WebSocket consumer which provides GraphQL API."""
send_ping_every = 1
schema = graphene.Schema(query=Query, mutation=Mutation, subscription=Subscription)
middleware = [demo_middleware]
# ------------------------------------------------------------------------- ASGI ROUTING
# NOTE: Please note `channels.auth.AuthMiddlewareStack` wrapper, for
# more details about Channels authentication read:
# https://channels.readthedocs.io/en/latest/topics/authentication.html
application = channels.routing.ProtocolTypeRouter(
{
"http": django.core.asgi.get_asgi_application(),
"websocket": channels.auth.AuthMiddlewareStack(
channels.routing.URLRouter(
[django.urls.path("graphql-ws/", MyGraphqlWsConsumer.as_asgi())]
)
),
}
)
# -------------------------------------------------------------------- URL CONFIGURATION
def graphiql(request):
"""Trivial view to serve the `graphiql.html` file."""
# It is important to create a session if it hasn't already been
# created, because `sessionid` cookie is used to identify the
# sender.
if not request.session.session_key:
request.session.create()
del request
graphiql_filepath = pathlib.Path(__file__).absolute().parent / "graphiql.html"
# It is better to specify an encoding when opening documents.
# Using the system default implicitly can create problems on other
# operating systems.
with open(graphiql_filepath, encoding="utf-8") as f:
return django.http.response.HttpResponse(f.read())
urlpatterns = [
django.urls.path("", graphiql),
# `GraphQLView` used to handle mutations and queries requests
# sended by http.
django.urls.path(
"graphql/",
graphene_django.views.GraphQLView.as_view(
schema=graphene.Schema(
query=Query, mutation=Mutation, subscription=Subscription
)
),
),
django.urls.path("admin", django.contrib.admin.site.urls),
]