Skip to content

Commit

Permalink
Merge pull request #118 from PhiloInc/backport
Browse files Browse the repository at this point in the history
Minor fixes to package
  • Loading branch information
NeoPhi authored Nov 21, 2017
2 parents 83ecda2 + 4750103 commit e57f4eb
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 10 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

### vNEXT

### 0.5.5
- FilterFn can return a Promise<boolean>
- Allow passing in a custom `EventEmitter` to `PubSub`

### 0.5.4
- Better define `withFilter` return type [PR #111](https://github.com/apollographql/graphql-subscriptions/pull/111)

Expand Down
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,8 @@ schema {
}
```

Now, let's create a simple `PubSub` instance - it is a simple pubsub implementation, based on `EventEmitter`.

To create a simple `PubSub` instance, do the following:
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.

```js
import { PubSub } from 'graphql-subscriptions';
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "graphql-subscriptions",
"version": "0.5.4",
"version": "0.5.5",
"description": "GraphQL subscriptions for node.js",
"main": "dist/index.js",
"repository": {
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export { PubSubEngine } from './pubsub-engine';
export { PubSub } from './pubsub';
export { PubSub, PubSubOptions } from './pubsub';
export { withFilter, ResolverFn, FilterFn } from './with-filter';
8 changes: 6 additions & 2 deletions src/pubsub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,17 @@ import { EventEmitter } from 'events';
import { PubSubEngine } from './pubsub-engine';
import { eventEmitterAsyncIterator } from './event-emitter-to-async-iterator';

export interface PubSubOptions {
eventEmitter?: EventEmitter;
}

export class PubSub implements PubSubEngine {
protected ee: EventEmitter;
private subscriptions: { [key: string]: [string, (...args: any[]) => void] };
private subIdCounter: number;

constructor() {
this.ee = new EventEmitter();
constructor(options: PubSubOptions = {}) {
this.ee = options.eventEmitter || new EventEmitter();
this.subscriptions = {};
this.subIdCounter = 0;
}
Expand Down
29 changes: 27 additions & 2 deletions src/test/asyncIteratorSubscription.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import * as sinonChai from 'sinon-chai';

import { isAsyncIterable } from 'iterall';
import { PubSub } from '../pubsub';
import { withFilter } from '../with-filter';
import { withFilter, FilterFn } from '../with-filter';
import { ExecutionResult } from 'graphql';

chai.use(chaiAsPromised);
Expand All @@ -27,7 +27,7 @@ const FIRST_EVENT = 'FIRST_EVENT';

const defaultFilter = (payload) => true;

function buildSchema(iterator, filterFn = defaultFilter) {
function buildSchema(iterator, filterFn: FilterFn = defaultFilter) {
return new GraphQLSchema({
query: new GraphQLObjectType({
name: 'Query',
Expand Down Expand Up @@ -82,6 +82,31 @@ describe('GraphQL-JS asyncIterator', () => {
return r;
});

it('should allow async filter', async () => {
const query = parse(`
subscription S1 {
testSubscription
}
`);
const pubsub = new PubSub();
const origIterator = pubsub.asyncIterator(FIRST_EVENT);
const schema = buildSchema(origIterator, () => Promise.resolve(true));

const results = await subscribe(schema, query) as AsyncIterator<ExecutionResult>;
const payload1 = results.next();

expect(isAsyncIterable(results)).to.be.true;

const r = payload1.then(res => {
expect(res.value.data.testSubscription).to.equal('FIRST_EVENT');
});

pubsub.publish(FIRST_EVENT, {});

return r;
});

it('should detect when the payload is done when filtering', (done) => {
const query = parse(`
subscription S1 {
Expand Down
2 changes: 1 addition & 1 deletion src/with-filter.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { $$asyncIterator } from 'iterall';

export type FilterFn = (rootValue?: any, args?: any, context?: any, info?: any) => boolean;
export type FilterFn = (rootValue?: any, args?: any, context?: any, info?: any) => boolean | Promise<boolean>;
export type ResolverFn = (rootValue?: any, args?: any, context?: any, info?: any) => AsyncIterator<any>;

export const withFilter = (asyncIteratorFn: ResolverFn, filterFn: FilterFn): ResolverFn => {
Expand Down

0 comments on commit e57f4eb

Please sign in to comment.