在本地 Apollo 的 RESTDataSource 上接受自签名证书

问题描述

在本地创建 RESTDataSource 类扩展和开发时如何接受自签名本地证书?

解决方法

Apolloapollo-datasource-rest's RESTDataSource 在其魔法方法(即 this.get())的第三个参数中接受节点获取参数。

这允许我们在本地接受自签名证书。

为了能够做到这一点,我们需要利用节点的 https 模块,以便我们稍后可以通过代理:

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,})

之后我们需要做的就是将代理作为选项传递:

class dataSourceApiExample extends RESTDataSource {

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

  ...
}