阿波罗联邦订阅在Node 8.11.1上的实现
创始人
2024-05-15 01:04:07
0次

阿波罗联邦订阅是Apollo GraphQL的一种特殊订阅方式,可以用于实现实时数据的订阅和推送。下面是一个在Node.js 8.11.1上实现阿波罗联邦订阅的示例代码:

  1. 首先,安装Apollo Server和相关依赖:
npm install apollo-server graphql graphql-subscriptions subscriptions-transport-ws
  1. 创建一个GraphQL服务器,并在其中启用阿波罗联邦订阅:
const { ApolloServer, gql } = require("apollo-server");
const { PubSub } = require("graphql-subscriptions");
const { execute, subscribe } = require("graphql");
const { createServer } = require("http");
const { SubscriptionServer } = require("subscriptions-transport-ws");

// 定义GraphQL类型和Resolvers
const typeDefs = gql`
  type Query {
    hello: String
  }

  type Mutation {
    publishMessage(message: String!): String
  }

  type Subscription {
    messagePublished: String
  }
`;

const resolvers = {
  Query: {
    hello: () => "Hello, world!"
  },
  Mutation: {
    publishMessage: (_, { message }, { pubsub }) => {
      pubsub.publish("MESSAGE_PUBLISHED", { messagePublished: message });
      return message;
    }
  },
  Subscription: {
    messagePublished: {
      subscribe: (_, __, { pubsub }) => pubsub.asyncIterator("MESSAGE_PUBLISHED")
    }
  }
};

// 创建PubSub实例
const pubsub = new PubSub();

// 创建Apollo Server
const server = new ApolloServer({
  typeDefs,
  resolvers,
  context: { pubsub }
});

// 创建HTTP服务器
const httpServer = createServer();
server.applyMiddleware({ app: httpServer });

// 启动HTTP服务器
httpServer.listen({ port: 4000 }, () => {
  console.log(`Server ready at http://localhost:4000${server.graphqlPath}`);
});

// 创建Subscription服务器
const subscriptionServer = SubscriptionServer.create(
  {
    schema: server.schema,
    execute,
    subscribe
  },
  {
    server: httpServer,
    path: server.graphqlPath
  }
);

console.log(`Subscription server ready at ws://localhost:4000${server.graphqlPath}`);
  1. 运行代码:
node server.js

现在,你可以使用GraphQL客户端来订阅messagePublished事件并接收实时推送的消息。例如,使用Apollo Client进行订阅:

import { ApolloClient, InMemoryCache, gql } from "@apollo/client";
import { WebSocketLink } from "@apollo/client/link/ws";

const client = new ApolloClient({
  link: new WebSocketLink({
    uri: "ws://localhost:4000/graphql",
    options: {
      reconnect: true
    }
  }),
  cache: new InMemoryCache()
});

const subscription = client.subscribe({
  query: gql`
    subscription {
      messagePublished
    }
  `
});

subscription.subscribe({
  next: ({ data }) => console.log("Received new message:", data.messagePublished)
});

这样,当你在Mutation中发布一条消息时,订阅会立即收到推送的消息。

相关内容

热门资讯

玻璃硬盘原理图 玻璃硬盘原理 玻璃硬盘,又称为磁头悬浮硬盘(Magnetic Head Flying Disk,MHFD),是一种...
Proton Mail 匿名注... 在数字监控无处不在的今天,你的邮箱可能比你想象的更「透明」——主流邮箱服务商在注册时就要求手机号验证...
闲鱼搜索规则与技巧 闲鱼最新特... 在闲鱼这个二手交易平台上,有很多用户都希望能够找到一些特殊的东西,比如一些罕见的收藏品、独特的手工艺...
家里监控最长能保存多少天的记录... 家里监控一般保存多久 随着科技的发展,家庭监控系统已经成为了许多家庭的必备设备,它不仅可以帮助我们...
华为tag有用吗 华为tag-... 华为Tag是华为手机中的一种功能,它可以帮助用户更好地管理自己的手机数据和应用,通过使用华为Tag,...
ps5手柄可用手机快充充电吗 ... PS5手柄,即PlayStation 5的DualSense手柄,是索尼公司为PlayStation...
QQ音乐提示代理模式可能无法正... QQ音乐提示代理模式可能无法正常访问,如上图所示,是怎么回事呢? 这个可能和你的网络设置有关系,首先...
网站服务器不稳定怎么解决 外网... 服务器稳定性是任何在线业务的关键,无论是网站、应用还是其他在线服务,有时候,即使我们尽了最大的努力,...
收到微信有提示音怎么去掉 微信... 微信收到信息没有提示音,可能是由多种原因导致的,以下是一些可能的原因及解决方法: 1. 手机静音或...
a100显卡对应的cuda版本 在进行GPU加速的编程中,CUDA是常用的架构和平台,其版本和显卡型号之间存在着一定的对应关系。本篇...