@aaronpowell/graphql-signalr-subscriptions 中文文档教程
SignalR GraphQL Subscriptions
此软件包包含对 Apollo GraphQL 订阅 的支持,使用 Azure SignalR 服务。
Installation
通过 npm 或 GitHub Packages 安装:
$> npm install --save @aaronpowell/graphql-signalr-subscriptions
Usage
您需要一个 SignalR 服务帐户(如果您没有 Azure 帐户 免费注册)。 复制连接字符串并在创建 SignalRPubSub
实例时提供它:
import { SignalRPubSub } from "@aaronpowell/graphql-signalr-subscriptions";
const signalrPubSub = new SignalRPubSub("<connection string>");
当您的服务器准备就绪时,您可以启动 pubsub 客户端:
signalrPubSub
.start()
.then(() => console.log("SignalR PubSub is up and running"))
.catch((error) => console.error(error));
在解析器中,您可以像使用任何其他 pubsub 实现一样使用它:
export const resolvers = {
Query: {
hello() {
signalrPubSub.publish("TESTING", { getMessage: "Hello I'm a message" });
return "Some message";
},
},
Subscription: {
getMessage: {
subscribe: () => signalrPubSub.asyncIterator(["TESTING"]),
},
},
};
SignalR GraphQL Subscriptions
This package contains support for Apollo GraphQL Subscriptions, using the Azure SignalR Service.
Installation
Install via npm or GitHub Packages:
$> npm install --save @aaronpowell/graphql-signalr-subscriptions
Usage
You'll need a SignalR Service account (if you don't have an Azure account sign up for free). Copy the connection string and provide it when you create an instance of SignalRPubSub
:
import { SignalRPubSub } from "@aaronpowell/graphql-signalr-subscriptions";
const signalrPubSub = new SignalRPubSub("<connection string>");
When your server is ready, you can start the pubsub client:
signalrPubSub
.start()
.then(() => console.log("SignalR PubSub is up and running"))
.catch((error) => console.error(error));
From within a resolver, you use it the same as any other pubsub implementation:
export const resolvers = {
Query: {
hello() {
signalrPubSub.publish("TESTING", { getMessage: "Hello I'm a message" });
return "Some message";
},
},
Subscription: {
getMessage: {
subscribe: () => signalrPubSub.asyncIterator(["TESTING"]),
},
},
};