Skip to content

Commit b678b91

Browse files
authored
Merge pull request #636 from williamrijksen/feat/graphql-subscriptions-v3
feat: support graphql subscriptions v3
2 parents 5a53a85 + c0b02bc commit b678b91

File tree

6 files changed

+29
-42
lines changed

6 files changed

+29
-42
lines changed

package-lock.json

Lines changed: 9 additions & 26 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
"prepublishOnly": "npm run test"
3838
},
3939
"peerDependencies": {
40-
"graphql-subscriptions": "^1.0.0 || ^2.0.0"
40+
"graphql-subscriptions": "^1.0.0 || ^2.0.0 || ^3.0.0"
4141
},
4242
"devDependencies": {
4343
"@istanbuljs/nyc-config-typescript": "^1.0.1",
@@ -53,7 +53,7 @@
5353
"chai-as-promised": "^7.1.1",
5454
"eslint": "^8.23.0",
5555
"graphql": "^16.6.0",
56-
"graphql-subscriptions": "^2.0.0",
56+
"graphql-subscriptions": "^3.0.0",
5757
"ioredis": "^5.3.2",
5858
"mocha": "^10.0.0",
5959
"nyc": "^15.1.0",

src/pubsub-async-iterator.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ import { PubSubEngine } from 'graphql-subscriptions';
3131
*/
3232
export class PubSubAsyncIterator<T> implements AsyncIterableIterator<T> {
3333

34-
constructor(pubsub: PubSubEngine, eventNames: string | string[], options?: unknown) {
34+
constructor(pubsub: PubSubEngine, eventNames: string | readonly string[], options?: unknown) {
3535
this.pubsub = pubsub;
3636
this.options = options;
3737
this.pullQueue = [];
@@ -61,7 +61,7 @@ export class PubSubAsyncIterator<T> implements AsyncIterableIterator<T> {
6161

6262
private pullQueue: Array<(data: { value: unknown, done: boolean }) => void>;
6363
private pushQueue: any[];
64-
private eventsArray: string[];
64+
private eventsArray: readonly string[];
6565
private subscriptionIds: Promise<number[]> | undefined;
6666
private listening: boolean;
6767
private pubsub: PubSubEngine;

src/redis-pubsub.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,11 @@ export class RedisPubSub implements PubSubEngine {
168168
delete this.subscriptionMap[subId];
169169
}
170170

171-
public asyncIterator<T>(triggers: string | string[], options?: unknown): AsyncIterator<T> {
171+
public asyncIterator<T>(triggers: string | string[], options?: unknown) {
172+
return new PubSubAsyncIterator<T>(this, triggers, options);
173+
}
174+
175+
public asyncIterableIterator<T>(triggers: string | string[], options?: unknown) {
172176
return new PubSubAsyncIterator<T>(this, triggers, options);
173177
}
174178

src/test/integration-tests.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,8 @@ describe('PubSubAsyncIterator', function() {
6464
`);
6565

6666
const pubsub = new RedisPubSub();
67-
const origIterator = pubsub.asyncIterator(FIRST_EVENT);
68-
const origPatternIterator = pubsub.asyncIterator('SECOND*', { pattern: true });
67+
const origIterator = pubsub.asyncIterableIterator(FIRST_EVENT);
68+
const origPatternIterator = pubsub.asyncIterableIterator('SECOND*', { pattern: true });
6969
const returnSpy = mock(origIterator, 'return');
7070
const schema = buildSchema(origIterator, origPatternIterator);
7171

@@ -128,7 +128,7 @@ describe('Subscribe to buffer', () => {
128128
// when using messageBuffer, with redis instance the channel name is not a string but a buffer
129129
const pubSub = new RedisPubSub({ messageEventName: 'messageBuffer'});
130130
const payload = 'This is amazing';
131-
131+
132132
pubSub.subscribe('Posts', message => {
133133
try {
134134
expect(message).to.be.instanceOf(Buffer);

src/test/tests.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -480,17 +480,17 @@ describe('PubSubAsyncIterator', () => {
480480
it('should expose valid asyncItrator for a specific event', () => {
481481
const pubSub = new RedisPubSub(mockOptions);
482482
const eventName = 'test';
483-
const iterator = pubSub.asyncIterator(eventName);
483+
const iterator = pubSub.asyncIterableIterator(eventName);
484484
// tslint:disable-next-line:no-unused-expression
485485
expect(iterator).to.exist;
486486
// tslint:disable-next-line:no-unused-expression
487487
expect(iterator[Symbol.asyncIterator]).not.to.be.undefined;
488488
});
489489

490-
it('should trigger event on asyncIterator when published', done => {
490+
it('should trigger event on asyncIterableIterator when published', done => {
491491
const pubSub = new RedisPubSub(mockOptions);
492492
const eventName = 'test';
493-
const iterator = pubSub.asyncIterator(eventName);
493+
const iterator = pubSub.asyncIterableIterator(eventName);
494494

495495
iterator.next().then(result => {
496496
// tslint:disable-next-line:no-unused-expression
@@ -505,10 +505,10 @@ describe('PubSubAsyncIterator', () => {
505505
pubSub.publish(eventName, { test: true });
506506
});
507507

508-
it('should not trigger event on asyncIterator when publishing other event', async () => {
508+
it('should not trigger event on asyncIterableIterator when publishing other event', async () => {
509509
const pubSub = new RedisPubSub(mockOptions);
510510
const eventName = 'test2';
511-
const iterator = pubSub.asyncIterator('test');
511+
const iterator = pubSub.asyncIterableIterator('test');
512512
const triggerSpy = spy(() => undefined);
513513

514514
iterator.next().then(triggerSpy);
@@ -519,7 +519,7 @@ describe('PubSubAsyncIterator', () => {
519519
it('register to multiple events', done => {
520520
const pubSub = new RedisPubSub(mockOptions);
521521
const eventName = 'test2';
522-
const iterator = pubSub.asyncIterator(['test', 'test2']);
522+
const iterator = pubSub.asyncIterableIterator(['test', 'test2']);
523523
const triggerSpy = spy(() => undefined);
524524

525525
iterator.next().then(() => {
@@ -530,10 +530,10 @@ describe('PubSubAsyncIterator', () => {
530530
pubSub.publish(eventName, { test: true });
531531
});
532532

533-
it('should not trigger event on asyncIterator already returned', done => {
533+
it('should not trigger event on asyncIterableIterator already returned', done => {
534534
const pubSub = new RedisPubSub(mockOptions);
535535
const eventName = 'test';
536-
const iterator = pubSub.asyncIterator<any>(eventName);
536+
const iterator = pubSub.asyncIterableIterator<any>(eventName);
537537

538538
iterator.next().then(result => {
539539
// tslint:disable-next-line:no-unused-expression

0 commit comments

Comments
 (0)