Fix stack trace when using apollo-server-testing #3148
Closed
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Using apollo-server-testing, I'm running into an issue where an error thrown in a resolver function while executing a query provides no stack trace. My test code that call's
apollo-server-testing
'squery()
gets a response withresponse.errors
present, but there is no stack trace in the response. Additionally, my ApolloServer'sformatError()
function gets called, buterror.extensions.exception.stacktrace
is not present.I'm using apollo-server-testing v2.8.1 (latest as of writing).
Reproducible example
This example demonstrates the current problem. Save the following as
test.js
:Then run:
Expected behavior
If a resolver throws an error, I expect my
formatError()
function to be called witherror.extensions.exception.stacktrace
present.Actual behavior
error.extensions.exception.stacktrace
is not present. Program output:Actual behavior when fixed
And here's the behavior I get after I apply one of the fixes I describe below:
What I'm trying to accomplish
My schema test code uses this approach to execute queries against the schema during tests. If there's a programming error in a resolver that results in an error being thrown, I want the developer running tests to see the resolver error and its stack trace.
I'm wondering if, aside from this issue, logging
error.extensions.exception.stacktrace
informatError()
the best approach to accomplish this in the first place?What I've found so far
The reason
extensions.exception.stacktrace
gets omitted is becauserequestContext.debug
if falsy here:apollo-server/packages/apollo-server-core/src/requestPipeline.ts
Lines 533 to 536 in d5015f4
However,
config.debug
is in the same scopetrue
.The call site that's passing
requestContext
into that scope does not passdebug
at all:apollo-server/packages/apollo-server-core/src/ApolloServer.ts
Lines 769 to 780 in d5015f4
I'm not sure what would be setting
requestContext.debug
or why the function looks there, I'd expectdebug
to be present in the serverconfig
object, notrequestContext
.Potential solutions
I came across two straightforward ways to fix this:
1) Read from
config.debug
Change the following line:
apollo-server/packages/apollo-server-core/src/requestPipeline.ts
Line 535 in d5015f4
To:
2) Pass
requestContext.debug
Alternatively, pass
debug
from the call site that createsrequestContext
:apollo-server/packages/apollo-server-core/src/ApolloServer.ts
Line 780 in d5015f4
const requestCtx: GraphQLRequestContext = { request, context: options.context || Object.create(null), cache: options.cache!, response: { http: { headers: new Headers(), }, }, + debug: options.debug, }; return processGraphQLRequest(options, requestCtx);
This PR
This PR applies the first fix described above. I'm looking for:
error.extensions.exception.stacktrace
informatError()
the best approach to get stack traces in the first place