graphql-python/graphql-ws được TopGit xếp vào nhóm dự án mã nguồn mở, với 270 sao trên GitHub, viết chủ yếu bằng Python. GraphQL websockets
Tóm tắt dựng từ metadata GitHub của chính dự án — chưa có bài review TopGit. Trang sẽ tự động cập nhật khi bài review đầy đủ được xuất bản.
VÌ SAO CHƯA CÓ REVIEW
TopGit viết bài đầy đủ cho repo có nhiều sao nhất và được yêu cầu nhiều nhất. Trang này là snapshot trong thời gian chờ — xem README gốc ở tab READ ME.
* `aiohttp`_
* `websockets compatible servers`_ such as Sanic
(via `websockets <https://github.com/aaugustin/websockets/>`__ library)
* `Django v2+`_
Python 2 application servers:
* `Gevent compatible servers`_ such as Flask
* `Django v1.x`_
(via `channels v1.x <https://channels.readthedocs.io/en/1.x/inshort.html>`__)
Installation instructions
For instaling graphql-ws, just run this command in your shell
.. code:: bash
pip install graphql-ws
Examples
Python 3 servers
Create a subscribable schema like this:
.. code:: python
import asyncio
import graphene
class Query(graphene.ObjectType):
hello = graphene.String()
@staticmethod
def resolve_hello(obj, info, **kwargs):
return "world"
class Subscription(graphene.ObjectType):
count_seconds = graphene.Float(up_to=graphene.Int())
async def resolve_count_seconds(root, info, up_to):
for i in range(up_to):
yield i
await asyncio.sleep(1.)
yield up_to
schema = graphene.Schema(query=Query, subscription=Subscription)
aiohttp
Then just plug into your aiohttp server.
.. code:: python
from graphql_ws.aiohttp import AiohttpSubscriptionServer
from .schema import schema
subscription_server = AiohttpSubscriptionServer(schema)
async def subscriptions(request):
ws = web.WebSocketResponse(protocols=('graphql-ws',))
await ws.prepare(request)
await subscription_server.handle(ws)
return ws
app = web.Application()
app.router.add_get('/subscriptions', subscriptions)
web.run_app(app, port=8000)
You can see a full example here:
https://github.com/graphql-python/graphql-ws/tree/master/examples/aiohttp
websockets compatible servers
Works with any framework that uses the websockets library for its websocket
implementation. For this example, plug in your Sanic server.
Set up with Django Channels just takes three steps:
Install the apps
Set up your schema
Configure the channels router application
First pip install channels and it to your INSTALLED_APPS. If you
want graphiQL, install the graphql_ws.django app before
graphene_django to serve a graphiQL template that will work with
websockets:
Finally, you can set up channels routing yourself (maybe using
graphql_ws.django.routing.websocket_urlpatterns in your
URLRouter), or you can just use one of the preset channels
applications:
.. code:: python
ASGI_APPLICATION = 'graphql_ws.django.routing.application'
# or
ASGI_APPLICATION = 'graphql_ws.django.routing.auth_application'
Run ./manage.py runserver and go to
http://localhost:8000/graphql <http://localhost:8000/graphql>__ to test!
Then just plug into your Gevent server, for example, Flask:
.. code:: python
from flask_sockets import Sockets
from graphql_ws.gevent import GeventSubscriptionServer
from schema import schema
subscription_server = GeventSubscriptionServer(schema)
app.app_protocol = lambda environ_path_info: 'graphql-ws'
@sockets.route('/subscriptions')
def echo_socket(ws):
subscription_server.handle(ws)
return []
You can see a full example here:
https://github.com/graphql-python/graphql-ws/tree/master/examples/flask_gevent
Django v1.x
~~~~~~~~~~~
For Django v1.x and Django Channels v1.x, setup your schema in ``settings.py``
.. code:: python
GRAPHENE = {
'SCHEMA': 'yourproject.schema.schema'
}
Then ``pip install "channels<1"`` and it to your django apps, adding the
following to your ``settings.py``
.. code:: python
CHANNELS_WS_PROTOCOLS = ["graphql-ws", ]
CHANNEL_LAYERS = {
"default": {
"BACKEND": "asgiref.inmemory.ChannelLayer",
"ROUTING": "django_subscriptions.urls.channel_routing",
},
}
And finally add the channel routes
.. code:: python
from channels.routing import route_class
from graphql_ws.django_channels import GraphQLSubscriptionConsumer
channel_routing = [
route_class(GraphQLSubscriptionConsumer, path=r"^/subscriptions"),
]
You can see a full example here:
https://github.com/graphql-python/graphql-ws/tree/master/examples/django_subscriptions
Trang TopGit này là một snapshot — tab "Readme" hiển thị nguyên văn README của repo (đã bỏ link, giữ ảnh). Repo GitHub ở github.com/graphql-python/graphql-ws là nguồn chính thức.
graphql-python/graphql-ws có bao nhiêu sao?
graphql-python/graphql-ws có 270 sao GitHub — tải lại trang để xem số mới nhất, hoặc xem trực tiếp github.com/graphql-python/graphql-ws. TopGit phản chiếu số sao của GitHub nhưng không cam kết đến từng phút.
graphql-python/graphql-ws có tag gì không?
Bản đồng bộ chưa ghi nhận topic GitHub nào cho graphql-python/graphql-ws. GitHub topics hiển thị ở thanh bên phải trang repo — đó là nơi đáng kiểm tra nhất.
graphql-python/graphql-ws còn đang phát triển không?
Commit gần nhất trên graphql-python/graphql-ws là 2.8 năm trước (theo timestamp GitHub). Repo có 84 fork — một chỉ báo về mức độ quan tâm của cộng đồng.
graphql-python/graphql-ws viết bằng ngôn ngữ gì?
graphql-python/graphql-ws chủ yếu viết bằng Python. Trường "language" của GitHub dựa trên phần lớn byte ở nhánh mặc định.
Đọc đầy đủ README ở tab phía trên.
graphql-ws có đáng để bạn bỏ thời gian?
ChatGPT, Claude và Perplexity đều đọc được trang này. Hỏi thử xem họ nghĩ gì về graphql-ws.