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

Execute queries in Fibers to support yielding APIs. #92

Merged
merged 3 commits into from
Aug 14, 2016
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 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));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Having this in apollo-server core is probably OK, because it's not specific to fibers.

Copy link
Member Author

@benjamn benjamn Aug 12, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually I think this may be the key to making everything work in Meteor, since the Promise that Meteor uses already knows how to run its callbacks in Fibers. In other words, I don't think there's any additional work to do on the https://github.com/apollostack/meteor-integration side!

Copy link
Member Author

@benjamn benjamn Aug 12, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can confirm this change is necessary and sufficient to accommodate Meteor developers who want to use Fiber-based APIs.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Amazing!

}

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