Skip to content

Commit

Permalink
Fix apollo-server-core runQuery breaks async_hook tracking (#733)
Browse files Browse the repository at this point in the history
By creating a promise out of the execution flow the ability to trace the
async call stack is lost.
  • Loading branch information
domarmstrong authored and martijnwalraven committed Dec 22, 2017
1 parent 2c99653 commit a8ae904
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Changelog

### vNEXT
* Fix apollo-server-core runQuery breaks async_hooks tracking [PR #733](https://github.com/apollographql/apollo-server/pull/733)

### v1.3.0
* Added support for the vhost option for Hapi [PR #611](https://github.com/apollographql/apollo-server/pull/611)
Expand Down
44 changes: 44 additions & 0 deletions packages/apollo-server-core/src/runQuery.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -315,4 +315,48 @@ describe('runQuery', () => {
},
});
});

describe('async_hooks', () => {
let asyncHooks;
let asyncHook;
const ids: number[] = [];

try {
asyncHooks = require('async_hooks');
} catch (err) {
return; // async_hooks not present, give up
}

before(() => {
asyncHook = asyncHooks.createHook({ init: (asyncId) => ids.push(asyncId) });
asyncHook.enable();
});

after(() => {
asyncHook.disable();
});

it('does not break async_hook call stack', async () => {
const query = `
query Q1 {
testObject {
testString
}
}
`;

await runQuery({
schema,
query: query,
operationName: 'Q1',
});

// this is the only async process so we expect the async ids to be a sequence
ids.forEach((id, i) => {
if (i > 0) {
expect(id).to.equal(ids[i - 1] + 1);
}
});
});
});
});
4 changes: 1 addition & 3 deletions packages/apollo-server-core/src/runQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,9 @@ export interface QueryOptions {
cacheControl?: boolean;
}

const resolvedPromise = Promise.resolve();

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

function doRunQuery(options: QueryOptions): Promise<GraphQLResponse> {
Expand Down

0 comments on commit a8ae904

Please sign in to comment.