apollographql/graphql-subscriptions hiện có 1.6k sao trên GitHub, viết chủ yếu bằng TypeScript. :newspaper: A small module that implements GraphQL subscriptions for Node.js
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.
Now, let's create a simple PubSub instance - it is a simple pubsub implementation, based on EventEmitter. Alternative EventEmitter implementations can be passed by an options object
to the PubSub constructor.
import { PubSub } from "graphql-subscriptions";
export const pubsub = new PubSub();
If you're using TypeScript you can use the optional generic parameter for added type-safety:
Subscriptions resolvers are not a function, but an object with subscribe method, that returns AsyncIterable.
The GraphQL engine now knows that somethingChanged is a subscription, and every time we use pubsub.publish it will publish content using our chosen transport layer:
Note that the default PubSub implementation is intended for demo purposes. It only works if you have a single instance of your server and doesn't scale beyond a couple of connections.
For production usage you'll want to use one of the PubSub implementations backed by an external store. (e.g. Redis)
Filters
When publishing data to subscribers, we need to make sure that each subscriber gets only the data it needs.
To do so, we can use withFilter helper from this package, which wraps AsyncIterator with a filter function, and lets you control each publication for each user.
withFilter API:
asyncIteratorFn: (rootValue, args, context, info) => AsyncIterator<any> : A function that returns AsyncIterator you got from your pubsub.asyncIterableIterator.
filterFn: (payload, variables, context, info) => boolean | Promise<boolean> - A filter function, executed with the payload (the published value), variables, context and operation info, must return boolean or Promise<boolean> indicating if the payload should pass to the subscriber.
For example, if somethingChanged would also accept a variable with the ID that is relevant, we can use the following code to filter according to it:
Note that when using withFilter, you don't need to wrap your return value with a function.
Channels Mapping
You can map multiple channels into the same subscription, for example when there are multiple events that trigger the same subscription in the GraphQL engine.
You can also manipulate the published payload, by adding resolve methods to your subscription:
const SOMETHING_UPDATED = "something_updated";
export const resolvers = {
Subscription: {
somethingChanged: {
resolve: (payload, args, context, info) => {
// Manipulate and return the new value
return payload.somethingChanged;
},
subscribe: () => pubsub.asyncIterableIterator(SOMETHING_UPDATED),
},
},
};
Note that resolve methods execute aftersubscribe, so if the code in subscribe depends on a manipulated payload field, you will need to factor out the manipulation and call it from both subscribe and resolve.
Usage with callback listeners
Your database might have callback-based listeners for changes, for example something like this:
The callback function would be called every time a new message is saved in the database. Unfortunately, that doesn't play very well with async iterators out of the box because callbacks are push-based, where async iterators are pull-based.
We recommend using the callback-to-async-iterator module to convert your callback-based listener into an async iterator:
The value you should return from your subscribe resolver must be an AsyncIterable.
You can wrap an AsyncIterator with custom logic for your subscriptions. For compatibility with APIs that require AsyncIterator or AsyncIterable, your wrapper can return an AsyncIterableIterator to comply with both.
For example, the following implementation manipulates the payload by adding some static fields:
You can also take a look at withFilter for inspiration.
PubSub Implementations
It can be easily replaced with some other implementations of PubSubEngine abstract class. Here are a few of them:
Use Redis with https://github.com/davidyaha/graphql-redis-subscriptions
Use Google PubSub with https://github.com/axelspringer/graphql-google-pubsub
Use MQTT enabled broker with https://github.com/aerogear/graphql-mqtt-subscriptions
Use RabbitMQ with https://github.com/cdmbase/graphql-rabbitmq-subscriptions
Use AMQP (RabbitMQ) with https://github.com/Surnet/graphql-amqp-subscriptions
Use Kafka with https://github.com/ancashoria/graphql-kafka-subscriptions
Use Kafka (using Kafkajs) with https://github.com/tomasAlabes/graphql-kafkajs-subscriptions
Use Postgres with https://github.com/GraphQLCollege/graphql-postgres-subscriptions
Use NATS with https://github.com/moonwalker/graphql-nats-subscriptions
Use Mongoose (MongoDB) with https://github.com/Nickolasmv/graphql-mongoose-subscriptions
Use multiple backends with https://github.com/jcoreio/graphql-multiplex-subscriptions
Use Ably for multi-protocol support with https://github.com/ably-labs/graphql-ably-pubsub
Use Google Firestore with https://github.com/m19c/graphql-firestore-subscriptions
Use Amazon's Simple Notification Service (SNS) and Simple Queue Service (SQS) with https://github.com/sagahead-io/graphql-snssqs-subscriptions
Add your implementation...
You can also implement a PubSub of your own, by using the exported abstract class PubSubEngine from this package. By using extends PubSubEngine you use the default asyncIterator method implementation; by using implements PubSubEngine you must implement your own AsyncIterator.
SubscriptionManager @deprecated
SubscriptionManager is the previous alternative for using graphql-js subscriptions directly, and it's now deprecated.
If you are looking for its API docs, refer to a previous commit of the repository
apollographql/graphql-subscriptions có bao nhiêu sao?
apollographql/graphql-subscriptions có 1.6k sao GitHub — tải lại trang để xem số mới nhất, hoặc xem trực tiếp github.com/apollographql/graphql-subscriptions. TopGit phản chiếu số sao của GitHub nhưng không cam kết đến từng phút.
apollographql/graphql-subscriptions có những chủ đề gì?
GitHub topics của apollographql/graphql-subscriptions: "graphql", "graphql-subscriptions", "real-time". TopGit xếp repo vào nhóm API.
apollographql/graphql-subscriptions có website riêng không?
TopGit chưa ghi nhận URL trang chủ cho apollographql/graphql-subscriptions. Phần README ở tab phía trên thường có link demo, hoặc xem mô tả GitHub của repo.
apollographql/graphql-subscriptions còn đang phát triển không?
Commit gần nhất trên apollographql/graphql-subscriptions là 1.8 năm trước (theo timestamp GitHub). Repo có 130 fork — một chỉ báo về mức độ quan tâm của cộng đồng.
apollographql/graphql-subscriptions dùng license gì?
apollographql/graphql-subscriptions phát hành theo license MIT. Nên mở file LICENSE trên GitHub để xác nhận — license metadata đôi khi lệch với thực tế dự án.
apollographql/graphql-subscriptions viết bằng ngôn ngữ gì?
apollographql/graphql-subscriptions chủ yếu viết bằng TypeScript. Trường "language" của GitHub dựa trên phần lớn byte ở nhánh mặc định.
Đọc thêm về apollographql/graphql-subscriptions ở đâu?
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/apollographql/graphql-subscriptions là nguồn chính thức.
Đọc đầy đủ README ở tab phía trên.
Chưa chắc graphql-subscriptions có hợp với bạn?
Để ChatGPT, Claude hoặc Perplexity tìm hiểu giúp — bấm bên dưới và xem AI nói gì về graphql-subscriptions.