0

I am using subclass of @apollo/datasource-rest for fetching data from REST API's in my project. I need to add a client certificate in every request in made in my subclass. I know that It is possible to add certificate in the https https.Agent and it can used in the previous version of apollo datasource rest apollo-datasource-rest like this

this.get(<reqparams> , {agent : <https agent with certificate>})

Not sure how to use the same with new version of apollo datasouce (@apollo/datasource-rest)

J.R
  • 2,113
  • 19
  • 21

2 Answers2

1

If you want to use a custom agent, you need to pass a custom fetch to RESTDataSource. The lack of support for agent is explicitly called out in the FetcherRequestInit type:

  // We explicitly do not support non-portable options like `node-fetch`'s
  // `agent`.
aaronmoat
  • 86
  • 5
  • Thanks for your helpful answer! Based on it, I was able to find a solution and posted it separately as answer, giving credit to your original answer. – J.R Jun 22 '23 at 22:08
0

I could solve the issue by overriding the default node-fetch in RESTDataSource

eg :

import https from 'https';
import nodeFetch from 'node-fetch';


class MyAPI extends RESTDataSource {
    constructor() {
        super({
            fetch: (url, init) => nodeFetch(url, {
                ...init,
                agent: new https.Agent({

                    keepAlive: true,
                    pfx: < My PFX > ,
                    passphrase: < Passphrase > ,
                })
            })
        });
    }
}
J.R
  • 2,113
  • 19
  • 21