Skip to content

Commit

Permalink
Merge pull request #636 from williamrijksen/feat/graphql-subscription…
Browse files Browse the repository at this point in the history
…s-v3

feat: support graphql subscriptions v3
  • Loading branch information
davidyaha authored Dec 12, 2024
2 parents 5a53a85 + c0b02bc commit b678b91
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 42 deletions.
35 changes: 9 additions & 26 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
"prepublishOnly": "npm run test"
},
"peerDependencies": {
"graphql-subscriptions": "^1.0.0 || ^2.0.0"
"graphql-subscriptions": "^1.0.0 || ^2.0.0 || ^3.0.0"
},
"devDependencies": {
"@istanbuljs/nyc-config-typescript": "^1.0.1",
Expand All @@ -53,7 +53,7 @@
"chai-as-promised": "^7.1.1",
"eslint": "^8.23.0",
"graphql": "^16.6.0",
"graphql-subscriptions": "^2.0.0",
"graphql-subscriptions": "^3.0.0",
"ioredis": "^5.3.2",
"mocha": "^10.0.0",
"nyc": "^15.1.0",
Expand Down
4 changes: 2 additions & 2 deletions src/pubsub-async-iterator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import { PubSubEngine } from 'graphql-subscriptions';
*/
export class PubSubAsyncIterator<T> implements AsyncIterableIterator<T> {

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

private pullQueue: Array<(data: { value: unknown, done: boolean }) => void>;
private pushQueue: any[];

Check warning on line 63 in src/pubsub-async-iterator.ts

View workflow job for this annotation

GitHub Actions / test (lts/*)

Unexpected any. Specify a different type
private eventsArray: string[];
private eventsArray: readonly string[];
private subscriptionIds: Promise<number[]> | undefined;
private listening: boolean;
private pubsub: PubSubEngine;
Expand Down
6 changes: 5 additions & 1 deletion src/redis-pubsub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,11 @@ export class RedisPubSub implements PubSubEngine {
delete this.subscriptionMap[subId];
}

public asyncIterator<T>(triggers: string | string[], options?: unknown): AsyncIterator<T> {
public asyncIterator<T>(triggers: string | string[], options?: unknown) {
return new PubSubAsyncIterator<T>(this, triggers, options);
}

public asyncIterableIterator<T>(triggers: string | string[], options?: unknown) {
return new PubSubAsyncIterator<T>(this, triggers, options);
}

Expand Down
6 changes: 3 additions & 3 deletions src/test/integration-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ describe('PubSubAsyncIterator', function() {
`);

const pubsub = new RedisPubSub();
const origIterator = pubsub.asyncIterator(FIRST_EVENT);
const origPatternIterator = pubsub.asyncIterator('SECOND*', { pattern: true });
const origIterator = pubsub.asyncIterableIterator(FIRST_EVENT);
const origPatternIterator = pubsub.asyncIterableIterator('SECOND*', { pattern: true });
const returnSpy = mock(origIterator, 'return');
const schema = buildSchema(origIterator, origPatternIterator);

Expand Down Expand Up @@ -128,7 +128,7 @@ describe('Subscribe to buffer', () => {
// when using messageBuffer, with redis instance the channel name is not a string but a buffer
const pubSub = new RedisPubSub({ messageEventName: 'messageBuffer'});
const payload = 'This is amazing';

pubSub.subscribe('Posts', message => {
try {
expect(message).to.be.instanceOf(Buffer);
Expand Down
16 changes: 8 additions & 8 deletions src/test/tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -480,17 +480,17 @@ describe('PubSubAsyncIterator', () => {
it('should expose valid asyncItrator for a specific event', () => {
const pubSub = new RedisPubSub(mockOptions);
const eventName = 'test';
const iterator = pubSub.asyncIterator(eventName);
const iterator = pubSub.asyncIterableIterator(eventName);
// tslint:disable-next-line:no-unused-expression
expect(iterator).to.exist;
// tslint:disable-next-line:no-unused-expression
expect(iterator[Symbol.asyncIterator]).not.to.be.undefined;
});

it('should trigger event on asyncIterator when published', done => {
it('should trigger event on asyncIterableIterator when published', done => {
const pubSub = new RedisPubSub(mockOptions);
const eventName = 'test';
const iterator = pubSub.asyncIterator(eventName);
const iterator = pubSub.asyncIterableIterator(eventName);

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

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

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

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

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

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

0 comments on commit b678b91

Please sign in to comment.