graphene django订阅通过通道模块向graphene django添加订阅支持
graphene-django-subscriptions的Python项目详细描述
此包添加了对订阅请求的支持,并使用channels包将其与websockets集成。您可以使用这个迷你web工具测试websockets通知。它直观而简单:websocket_example_client
安装:
要安装graphene django订阅,只需在shell中运行此命令:
pip install "graphene-django-subscriptions"
文档:
额外功能(订阅):
- Subscription (Abstract class to define subscriptions to a DjangoSerializerMutation)
- GraphqlAPIDemultiplexer (Custom WebSocket consumer subclass that handles demultiplexing streams)
订阅:
第一种方法是在graphene django中添加对通道的graphql订阅支持,使用channels api包。
1-定义自定义订阅类:
必须为要定义订阅类的每个模型定义序列化程序类:
# app/graphql/subscriptions.pyimportgraphenefromgraphene_django_subscriptions.subscriptionimportSubscriptionfrom.serializersimportUserSerializer,GroupSerializerclassUserSubscription(Subscription):classMeta:serializer_class=UserSerializerstream='users'description='User Subscription'classGroupSubscription(Subscription):classMeta:serializer_class=GroupSerializerstream='groups'description='Group Subscription'
将订阅定义添加到应用程序的架构中:
# app/graphql/schema.pyimportgraphenefrom.subscriptionsimportUserSubscription,GroupSubscriptionclassSubscriptions(graphene.ObjectType):user_subscription=UserSubscription.Field()GroupSubscription=PersonSubscription.Field()
将应用程序的架构添加到项目根架构中:
# schema.pyimportgrapheneimportcustom.app.route.graphql.schemaclassRootQuery(custom.app.route.graphql.schema.Query,graphene.ObjectType):classMeta:description='The project root query definition'classRootMutation(custom.app.route.graphql.schema.Mutation,graphene.ObjectType):classMeta:description='The project root mutation definition'classRootSubscription(custom.app.route.graphql.schema.Subscriptions,graphene.ObjectType):classMeta:description='The project root subscription definition'schema=graphene.Schema(query=RootQuery,mutation=RootMutation,subscription=RootSubscription)
2-定义频道设置和自定义路由配置:
注意:有关此步骤的详细信息,请参阅频道文档。
必须为要定义订阅类的每个模型定义了djangoSerializerMutation类:
我们定义应用程序路由,就像它们是应用程序URL一样:
# app/routing.pyfromgraphene_django_subscriptions.consumersimportGraphqlAPIDemultiplexerfromchannels.routingimportroute_classfrom.graphql.subscriptionsimportUserSubscription,GroupSubscriptionclassCustomAppDemultiplexer(GraphqlAPIDemultiplexer):consumers={'users':UserSubscription.get_binding().consumer,'groups':GroupSubscription.get_binding().consumer}app_routing=[route_class(CustomAppDemultiplexer)]
定义项目路由,如自定义根项目url:
# project/routing.pyfromchannelsimportincludeproject_routing=[include("custom.app.folder.routing.app_routing",path=r"^/custom_websocket_path"),]
您应该将频道和频道api模块放入已安装的频道应用程序中,并且必须将项目的路由定义添加到频道层设置中:
# settings.py...INSTALLED_APPS=('django.contrib.auth','django.contrib.contenttypes','django.contrib.sessions','django.contrib.sites',...'channels','channels_api','custom_app')CHANNEL_LAYERS={"default":{"BACKEND":"asgiref.inmemory.ChannelLayer","ROUTING":"myproject.routing.project_routing",# Our project routing},}...
您必须在设置的graphene dict配置末尾添加'graphene django_subscription.depromise_subscription'中间件。py:
# settings.py...GRAPHENE={'SCHEMA_INDENT':4,'MIDDLEWARE':[# Others middleware'graphene_django_subscriptions.depromise_subscription',]}...
3-订阅示例:
在Web客户端中,必须将WebSocket连接定义为:“ws://host:port/custom\u WebSocket\u path”。 建立连接后,服务器将返回WebSocket消息,如下所示: {“频道id”:“gthkdsyvrk!wxrcdjqmpi,“connect”:“success”},其中必须存储通道id值,以便以后在graphql订阅请求中用于订阅或取消订阅操作。
graphql的订阅请求接受五个可能的参数: 一。操作:要执行的操作:订阅或取消订阅。(必需) 2.action:要订阅的操作:创建、更新、删除或所有操作。(必需) 三。channelid:通过websocket标识连接。(必需) 四。id:要订阅的对象的id字段值。(可选) 5个。data:要在订阅通知中显示的模型字段。(可选)
subscription{userSubscription(action:UPDATE,operation:SUBSCRIBE,channelId:"GthKdsYVrK!WxRCdJQMPi",id:5,data:[ID,USERNAME,FIRST_NAME,LAST_NAME,EMAIL,IS_SUPERUSER]){okerrorstream}}
在这种情况下,发送的订阅请求将向客户端返回一条WebSocket消息,如下所示:{“action”:“update”,“operation”:“subscribe”,“ok”:true,“stream”:“users”,“error”:null},从那时起,每次修改ID=5的用户时,您将通过WebSocket的连接接收以下格式的消息:
{"stream":"users","payload":{"action":"update","model":"auth.user","data":{"id":5,"username":"meaghan90","first_name":"Meaghan","last_name":"Ackerman","email":"meaghan@gmail.com","is_superuser":false}}}
要取消订阅,您必须发送这样的graphql请求:
subscription{userSubscription(action:UPDATE,operation:UNSUBSCRIBE,channelId:"GthKdsYVrK!WxRCdJQMPi",id:5){okerrorstream}}
note:每次在graphql的服务器重新启动之前,必须重新建立websocket连接,并使用新的websocket连接id重新发送graphql的订阅请求。
更改日志:
v0.0.6:
- 修复了在序列化程序类中定义字段时生成model_fields_enum的小错误:fields=“uuu all_uuu”
- 这避免了石墨烯django的后版本出现故障。
v0.0.4:
- 修复了subscription\u resolver函数上的小错误。
v0.0.3:
- 添加了depromise\u sub脚本允许在Graphene django上使用订阅的中间件>;=2.0。
- 已更新对Graphene Django Extras的设置依赖性>;=0.3.0。
v0.0.2:
一。将对订阅元类定义的变异类依赖更改为序列化程序类以获得更好的结果 整合。 2.修正了一些小错误。
v0.0.1:
- 首次提交