RestDatasource connection pooling

I’m implementing apollo server with typescript. I wonder if there is a configuration to have some connection pooling for RestDataSource calling external rest Apis.

Hi @Hakob_Hakobyan, the RESTDataSource allows you to provide your own custom fetch as the first argument to the constructor.

We use node-fetch internally, which you’re welcome to use as well. It allows you to provide a custom HTTP(S) agent which affords you settings like keepAlive and maxSockets.

http(s).Agent options:

hi @trevor.scheer
thank you for your response. I tried providing my own fetch to the constructor like this

super((input, init) => {
            return fetch(input, {
                agent: new http.Agent({keepAlive:true})

but when logging request and response in didReceiveResponse method, I don’t see the agent in the request
but when I pass agent to get request
await this.get(${this.baseURL}, params, { agent: new http.Agent({keepAlive:true}) }); I see the log with agent in the request log.
So the question here is how to set agent globally, so for every request of datasource there was no need to specify it in the particular request.

@Hakob_Hakobyan are you sure the fetch you’re providing to the constructor isn’t being used “globally”? It’s not necessarily expected that the agent is accessible to you in the didReceiveResponse method.

Can you add a console.log within your custom fetch function to see if that’s being called on every request?

If you’re certain that your fetch isn’t being used when you call this.get etc., can you please open an issue on the apollo-server repo and provide a reproduction for the problem?