From 62b9abfa5b2706487f344665f237de2a33ef738d Mon Sep 17 00:00:00 2001 From: Dom Armstrong Date: Thu, 21 Dec 2017 17:38:37 +0000 Subject: [PATCH] Fix apollo-server-core runQuery breaks async_hook tracking By creating a promise out of the execution flow the ability to trace the async call stack is lost. --- CHANGELOG.md | 1 + .../apollo-server-core/src/runQuery.test.ts | 44 +++++++++++++++++++ packages/apollo-server-core/src/runQuery.ts | 4 +- 3 files changed, 46 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 690a1357198..d1ca8b75f47 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) diff --git a/packages/apollo-server-core/src/runQuery.test.ts b/packages/apollo-server-core/src/runQuery.test.ts index 8743f986658..b7933aa813c 100644 --- a/packages/apollo-server-core/src/runQuery.test.ts +++ b/packages/apollo-server-core/src/runQuery.test.ts @@ -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); + } + }); + }); + }); }); diff --git a/packages/apollo-server-core/src/runQuery.ts b/packages/apollo-server-core/src/runQuery.ts index 6fdeedec9f9..1aeeaf68abe 100644 --- a/packages/apollo-server-core/src/runQuery.ts +++ b/packages/apollo-server-core/src/runQuery.ts @@ -62,11 +62,9 @@ export interface QueryOptions { cacheControl?: boolean; } -const resolvedPromise = Promise.resolve(); - function runQuery(options: QueryOptions): Promise { // 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 {