-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Expose request Object to formatError #614
Comments
You cannot access it in the |
Rebinding the format error function on every request seems very wasteful. Would be great if it took request, or context, or something that exposed this, a second parameter. (My usecase is stackdriver error reporting, which can make use of requests and formatError is a convenient place to log all graphql errors) |
I have the same requirement as @majelbstoat . When logging and reporting errors, I need to get some info from the request in order to create a relation between the errors and the requester. I am using Has anyone made any progress for this or found work around? |
I also have this requirement. The best workaround I found was to extend class MyApolloServer extends ApolloServer {
async createGraphQLServerOptions(req) {
return {
schema,
rootValue,
context,
// formatError: e => handleError(e, req) // doesn't get called
formatResponse: graphqlResponse => {
if (graphqlResponse.errors) {
errors = graphqlResponse.errors.map(e => handleError(e, req));
}
return { ...graphqlResponse, errors };
},
};
}
} |
Is there any update on this? Like others, I also have a requirement to log some information to correlate between events that happen in the request. |
A bit of a blocker for me too, since our current server initialisation utilises |
Also blocker for me. As a very dirty workaround I ended up wrapping all resolvers into a try catch so I could access the context in case of an error, just before passing them to the Apollo server...
|
* Pass the context along to all the extension methods Addresses #1343 With this change you should now be able to implement an extension like so: ```javascript class MyErrorTrackingExtension extends GraphQLExtension { willSendResponse(o) { const { context, graphqlResponse } = o context.trackErrors(graphqlResponse.errors) return o } } ``` Edit by @evans : fixes #1343 fixes #614 as the request object can be taken from context or from requestDidStart fixes #631 "" * Remove context from extra extension functions The context shouldn't be necessary for format, parse, validation, and execute. Format generally outputs the state of the extension. Parse and validate don't depend on the context. Execution includes the context argument as contextValue * Move change entry under vNext
* Pass the context along to all the extension methods Addresses #1343 With this change you should now be able to implement an extension like so: ```javascript class MyErrorTrackingExtension extends GraphQLExtension { willSendResponse(o) { const { context, graphqlResponse } = o context.trackErrors(graphqlResponse.errors) return o } } ``` Edit by @evans : fixes #1343 fixes #614 as the request object can be taken from context or from requestDidStart fixes #631 "" * Remove context from extra extension functions The context shouldn't be necessary for format, parse, validation, and execute. Format generally outputs the state of the extension. Parse and validate don't depend on the context. Execution includes the context argument as contextValue * Move change entry under vNext
@robrichard I also meet up with the problem, how to resolve it? |
The linked code change above kind of solves the problem, although the context is still not available in the |
This is blocking my upgrade to 2.0 as well. In 1.0 I used code like this: graphqlExpress(request => ({
schema,
context: request => ({ request }),
formatError: request => error => {
// Log Error to Sentry with extra request context
// If appropriate, change error message to obscure real error
// from non-admin users.
}
})) But now upgrading to 2.0 I see no way to get the request information to add context to my logs. It seems like philsch's code might be a workaround and I also found this example. Is implementing a custom graphql-extension going to remain the only supported way to accomplish this? Or is there a chance we can get the graphql context passed as a parameter to the formatError function? |
I'd very much like to get the request context in I have localizable error objects, and I'd like to localize the error message in Should I definitely forget about getting the context in |
For anyone stumbling upon this, here's a gist of how to apply @robrichard proposal https://gist.github.com/eladchen/00251cfd96a3fc4fe0c986d83fd48bc2 |
I would like to log the request body when an error occurs. This requires access to the request object.
In general, request object often contains additional details that might be desired to be logged, e.g. user session ID.
The text was updated successfully, but these errors were encountered: