Skip to content

Commit

Permalink
Allow resolver fn to be async
Browse files Browse the repository at this point in the history
  • Loading branch information
maclockard committed Mar 25, 2020
1 parent 9ea2d87 commit 86a5007
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 8 deletions.
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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
"sinon": "^6.1.4",
"sinon-chai": "^3.2.0",
"tslint": "^5.11.0",
"typescript": "^2.3.2"
"typescript": "^3.8.3"
},
"typings": "dist/index.d.ts",
"typescript": {
Expand Down
6 changes: 4 additions & 2 deletions src/test/asyncIteratorSubscription.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,11 @@ describe('GraphQL-JS asyncIterator', () => {

const schema = buildSchema(origIterator, filterFn);

Promise.resolve(subscribe(schema, query)).then((results: AsyncIterator<ExecutionResult>) => {
Promise.resolve(subscribe(schema, query)).then((results: AsyncIterator<ExecutionResult> | ExecutionResult) => {
expect(isAsyncIterable(results)).to.be.true;

results = results as AsyncIterator<ExecutionResult>; // above check will fail if not an async iterator

results.next();
results.return();

Expand Down Expand Up @@ -190,7 +192,7 @@ let testFiniteAsyncIterator: AsyncIterator<number> = createAsyncIterator([1, 2,

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

for (let i = 1; i <= 4; i++) {
let result = await filteredAsyncIterator.next();
Expand Down
8 changes: 4 additions & 4 deletions src/with-filter.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { $$asyncIterator } from 'iterall';

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 type ResolverFn = (rootValue?: any, args?: any, context?: any, info?: any) => AsyncIterator<any> | Promise<AsyncIterator<any>>;

export const withFilter = (asyncIteratorFn: ResolverFn, filterFn: FilterFn): ResolverFn => {
return (rootValue: any, args: any, context: any, info: any): AsyncIterator<any> => {
const asyncIterator = asyncIteratorFn(rootValue, args, context, info);
return async (rootValue: any, args: any, context: any, info: any): Promise<AsyncIterator<any>> => {
const asyncIterator = await asyncIteratorFn(rootValue, args, context, info);

const getNextPromise = () => {
return asyncIterator
Expand Down Expand Up @@ -41,6 +41,6 @@ export const withFilter = (asyncIteratorFn: ResolverFn, filterFn: FilterFn): Res
[$$asyncIterator]() {
return this;
},
};
} as AsyncIterator<any>; // needed since TS doesn't allow the iterall async iterator symbol
};
};

0 comments on commit 86a5007

Please sign in to comment.