Fix apollo-server-core runQuery breaks async_hook tracking (#733)

By creating a promise out of the execution flow the ability to trace the
async call stack is lost.
This commit is contained in:
Dom Armstrong 2017-12-22 04:46:02 +00:00 committed by Martijn Walraven
parent 2c9965336a
commit a8ae9042f5
3 changed files with 46 additions and 3 deletions

View file

@ -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)

View file

@ -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);
}
});
});
});
});

View file

@ -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> {