GrapheneDjangoSubscriptions通过Channels模块向graphenedjango添加订阅支持
gql-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)
订阅:
这是第一种在graphenedjango中添加Graphql订阅支持的方法,使用Channels api包。在
1-定义自定义订阅类:
必须为要定义订阅类的每个模型定义序列化程序类:
^{pr2}$将订阅定义添加到应用程序的架构中:
# 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-定义频道设置和自定义路由配置:
Note:有关此步骤的详细信息,请参阅频道文档。在
必须为要定义订阅类的每个模型定义DjangSerializerMation类:
我们定义应用程序路由,就像它们是应用程序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"),]
您应该将channels和channels\u api模块放入已安装的应用程序中,并且必须将项目的路由定义添加到CHANNEL_LAYERS设置中:
# 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_django_subscriptions.depromise_订阅'中间件位于设置.py公司名称:
# settings.py...GRAPHENE={'SCHEMA_INDENT':4,'MIDDLEWARE':[# Others middleware'graphene_django_subscriptions.depromise_subscription',]}...
3-订阅示例:
在WEB客户端中,必须将websocket连接定义为:“ws://主机:端口/自定义“websocket”路径’. 建立连接后,服务器会返回一个websocket消息,如下所示: {“channel_id”:“GthKdsYVrK!WxRCdJQMPi,“connect”:“success”},其中必须存储通道标识值,以便以后在graphql订阅请求中用于订阅或取消订阅操作。在
graphql的订阅请求接受五个可能的参数: 1operation:要执行的操作:订阅或取消订阅。(必需) 2action:要订阅的操作:创建、更新、删除或所有操作。(必需) 三。channelId:通过websocket标识连接。(必需) 4id:要订阅的对象的id字段值。(可选) 5data:要在订阅通知中显示的模型字段。(可选)
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=“”
- 这样就避免了石墨烯django的后部版本出现故障。在
v0.0.4:
- 修复了subscription\u resolver函数上的小错误。在
v0.0.3:
- 添加了depromise_sub脚本允许在graphene django>;=2.0上使用订阅的中间件。在
- 更新了对graphene django extras的设置依赖性,gt;=0.3.0。在
v0.0.2:
1。改变了变异_类对订阅元类定义的依赖性,改为序列化器_类,以获得更好的效果 整合。 2修正了一些小错误。在
v0.0.1:
- 首次提交
- 项目
标签: