Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow resolver fn to be async #220

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
[@cursorsdottsx](https://github.com/cursorsdottsx) in [#245](https://github.com/apollographql/graphql-subscriptions/pull/245)
- Support `readonly` arrays of event names. <br/>
[@rh389](https://github.com/rh389) in [#234](https://github.com/apollographql/graphql-subscriptions/pull/234)
- Support returning a Promise of an `AsyncIterator` as the `withFilter` resolver function. <br/>
[@maclockard](https://github.com/maclockard) in [#220](https://github.com/apollographql/graphql-subscriptions/pull/220)

### 2.0.1 (not yet released)

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ You can use it with any GraphQL client and server (not only Apollo).

If you are developing a project that uses this module with TypeScript:

* ensure that your `tsconfig.json` `lib` definition includes `"esnext.asynciterable"`
* ensure that your `tsconfig.json` `lib` definition includes `"es2018.asynciterable"`
* `npm install @types/graphql` or `yarn add @types/graphql`

### Getting started with your first subscription
Expand Down
5 changes: 3 additions & 2 deletions src/test/asyncIteratorSubscription.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ const testFiniteAsyncIterator: AsyncIterableIterator<number> = (async function *

describe('withFilter', () => {
it('works properly with finite asyncIterators', async () => {
const filteredAsyncIterator = withFilter(() => testFiniteAsyncIterator, isEven)();
const filteredAsyncIterator = await withFilter(() => testFiniteAsyncIterator, isEven)();

for (let i = 1; i <= 4; i++) {
const result = await filteredAsyncIterator.next();
Expand Down Expand Up @@ -228,7 +228,8 @@ describe('withFilter', () => {
},
};

const filteredAsyncIterator = withFilter(() => asyncIterator, () => stopped)();
const filteredAsyncIterator =
await withFilter(() => asyncIterator, () => stopped)();

global.gc();
const heapUsed = process.memoryUsage().heapUsed;
Expand Down
2 changes: 1 addition & 1 deletion src/test/tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ describe('AsyncIterator', () => {
it('register to multiple events', done => {
const eventName = 'test2';
const ps = new PubSub();
const iterator = ps.asyncIterator(['test', 'test2'] as const);
const iterator = ps.asyncIterableIterator(['test', 'test2'] as const);
const spy = sinon.spy();

iterator.next().then(() => {
Expand Down
7 changes: 3 additions & 4 deletions src/with-filter.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@

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

interface IterallAsyncIterator<T> extends AsyncIterableIterator<T> {
[Symbol.asyncIterator](): IterallAsyncIterator<T>;
Expand All @@ -15,8 +14,8 @@ export function withFilter<TSource = any, TArgs = any, TContext = any>(
asyncIteratorFn: ResolverFn<TSource, TArgs, TContext>,
filterFn: FilterFn<TSource, TArgs, TContext>
): ResolverFn<TSource, TArgs, TContext> {
return (rootValue: TSource, args: TArgs, context: TContext, info: any): IterallAsyncIterator<any> => {
const asyncIterator = asyncIteratorFn(rootValue, args, context, info);
return async (rootValue: TSource, args: TArgs, context: TContext, info: any): Promise<IterallAsyncIterator<any>> => {
const asyncIterator = await asyncIteratorFn(rootValue, args, context, info);

const getNextPromise = () => {
return new Promise<IteratorResult<any>>((resolve, reject) => {
Expand Down