Returning a promise in the method willSendResponse of an extension should be supported
If the method willSendResponse returns a promise or is async the server crashes on every calls and the client receives the errors: [{"message": "Cannot read property 'errors' of undefined"}]
Create a simple logging extension for an Apollo server 2:
const apolloServer = new ApolloServer({
//...
extensions: [ () => new Logger(), ],
})
And the logger:
export class Logger {
async willSendResponse() {
//...
}
}
I'm assuming this is unintended and this sould be an easy fix ;)
Am I the only one in need of async actions in extensions?
✋🏻In need too
Yes, facing the same issue.
Actually this bug prevents us to use Apollo Server. We have the following case: for some queries we want to have export files that contains the same data as result of the query. To send export file id we use extensions field in the response. But to create and publish file we need to do async actions during the construction of the extensions field. But making willSendResponse as async crashes the server.
I spent entire day to find workaround but failed. Any suggestions?
@nick-keller what about adding label tag: graphql-extensions?
✋🏻 it would be very useful for us also! We have a use case in which we need to do some async post procesing in willSendResponse. Is there any workaround?
✋🏻 Same here
This is supported by the new request pipeline plugin API. The graphql-extensions API is not being actively maintained anymore and is being replaced by in favor of the new request pipeline which first surfaced in https://github.com/apollographql/apollo-server/pull/1795 and aims to cover a wider range of functionality than graphql-extensions which was never documented nor intended to be public API.
The new API (which can be utilized by providing plugins to the ApolloServer constructor) supports more integration points (e.g. server startup, where as extensions were only ever per-request), and in the future will support schema generation hooks and more, all with a strongly-typed API.
While the new API is still growing, the documentation is taking form in https://github.com/apollographql/apollo-server/pull/2008 (there's a preview link on that PR so you can view them in a more glorious form than the raw markdown) and building out the API further is on our roadmap for Apollo Server 3.0, as noted in #2360.
I'll close this, but if you take a look at the documentation and the source types in apollo-server-plugin-base (used by package authors to implement plugins, for example), you should be able to get an idea, and we intend on supporting this longer-term.
In the case of willSendResponse, you'll find it _does_ support returning a Promise and can be used _today_:
I didn't finish writing the text blurb for willSendResponse in those docs previews, but I think you'll get the idea. Let me know if you have any questions.