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

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

Merged
merged 1 commit into from
Dec 22, 2017
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
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