0

How can I accept self signed local certificates when creating a RESTDataSource class extend and developing locally?

Mel Macaluso
  • 3,250
  • 2
  • 12
  • 26

2 Answers2

0

Apollo's apollo-datasource-rest's RESTDataSource accepts node-fetch parameters in its third argument from its magic methods (ie. this.get()).

This allows us to accept self signed certificates locally.

In order to be able to do that we need to leverage's node's https module so that we can pass later on the agent:

import https from 'https'


const agent = new https.Agent({
  rejectUnauthorized: !process.env.IS_OFFLINE // this will work for serverless-offline but feel free to pass anything that detects you are working locally,
})

After that all we need to do is pass the agent as a option:

class dataSourceApiExample extends RESTDataSource {

  ...
  
  this.get() {
   'some-endpoint',
   {},
   {
     agent
   }
  }

  ...
}

Mel Macaluso
  • 3,250
  • 2
  • 12
  • 26
0

The Answer from Mel Macaluso does not work for me.

For development I´m using dotenv (a .env file). Like Mel said, Apollo is using node-fetch under the hood and reads the .env file.

To set this worked for me in dev. Be careful in production though, you are open for man-in-the-middle attacks now.

add in .env

NODE_TLS_REJECT_UNAUTHORIZED=0
mobin
  • 318
  • 2
  • 6