Skip to content

Commit

Permalink
Execute queries in Fibers to support yielding APIs. (#92)
Browse files Browse the repository at this point in the history
* Execute queries in Fibers to support yielding APIs.

See also: https://github.com/meteor/promise/blob/master/promise.d.ts

* Improve comment about Promise.await.

* Don't make Promises aware of Fibers except in tests.

The Promise used by Meteor will already be Fiber-enabled, so calling
makeCompatible in the tests is just simulating running in a Meteor
environment.
  • Loading branch information
benjamn authored and helfer committed Aug 14, 2016
1 parent d0aea97 commit 4c82b8e
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 3 deletions.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,14 @@
"chai": "^3.5.0",
"connect": "^3.4.1",
"express": "^4.14.0",
"fibers": "^1.0.13",
"graphql": "^0.6.1",
"hapi": "^14.0.0",
"istanbul": "1.0.0-alpha.2",
"koa": "^2.0.0-alpha.4",
"koa-bodyparser": "^3.0.0",
"koa-router": "^7.0.1",
"meteor-promise": "^0.7.3",
"mocha": "^3.0.0",
"multer": "^1.1.0",
"remap-istanbul": "^0.6.4",
Expand Down
25 changes: 25 additions & 0 deletions src/core/runQuery.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ import {

import { runQuery } from './runQuery';

// Make the global Promise constructor Fiber-aware to simulate a Meteor
// environment.
import { makeCompatible } from 'meteor-promise';
import Fiber = require('fibers');
makeCompatible(Promise, Fiber);

const QueryType = new GraphQLObjectType({
name: 'QueryType',
fields: {
Expand Down Expand Up @@ -43,6 +49,15 @@ const QueryType = new GraphQLObjectType({
base: { type: new GraphQLNonNull(GraphQLInt) },
},
},
testAwaitedValue: {
type: GraphQLString,
resolve(root) {
// Calling Promise.await is legal here even though this is
// not an async function, because we are guaranteed to be
// running in a Fiber.
return 'it ' + (<any>Promise).await('works');
},
},
},
});

Expand Down Expand Up @@ -170,6 +185,16 @@ describe('runQuery', () => {
});
});

it('supports yielding resolver functions', () => {
return runQuery({
schema: Schema,
query: `{ testAwaitedValue }`,
}).then((res) => {
expect(res.data).to.deep.equal({
testAwaitedValue: 'it works',
});
});
});

it('runs the correct operation when operationName is specified', () => {
const query = `
Expand Down
7 changes: 7 additions & 0 deletions src/core/runQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,14 @@ export interface QueryOptions {
formatResponse?: Function;
}

const resolvedPromise = Promise.resolve();

function runQuery(options: QueryOptions): Promise<GraphQLResult> {
// Fiber-aware Promises run their .then callbacks in Fibers.
return resolvedPromise.then(() => doRunQuery(options));
}

function doRunQuery(options: QueryOptions): Promise<GraphQLResult> {
let documentAST: Document;

const logFunction = options.logFunction || function(){ return null; };
Expand Down
1 change: 0 additions & 1 deletion tslint.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@
"no-unused-variable": true,
"no-use-before-declare": true,
"no-var-keyword": true,
"no-var-requires": true,
"object-literal-sort-keys": false,
"one-line": [
true,
Expand Down
5 changes: 3 additions & 2 deletions typings.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@
"connect": "registry:dt/connect#3.4.0+20160317120654",
"cookies": "registry:dt/cookies#0.5.1+20160316171810",
"express": "registry:dt/express#4.0.0+20160708185218",
"express-serve-static-core": "registry:dt/express-serve-static-core#4.0.0+20160715232503",
"hapi": "registry:dt/hapi#13.0.0+20160709092105",
"express-serve-static-core": "registry:dt/express-serve-static-core#4.0.0+20160805091045",
"fibers": "registry:dt/fibers#0.0.0+20160317120654",
"hapi": "registry:dt/hapi#13.0.0+20160803202811",
"koa": "registry:dt/koa#2.0.0+20160724024233",
"koa-bodyparser": "registry:dt/koa-bodyparser#3.0.0+20160414124440",
"koa-router": "registry:dt/koa-router#7.0.0+20160314083221",
Expand Down

0 comments on commit 4c82b8e

Please sign in to comment.