Fairly new to creating Apollo servers, and trying to learn to create them properly, so working on a class using RESTDataSource to fetch data from several API's, but even internal, all my requests have to use our corporate proxies. I can't seem to see a way to get it to use them. Do I need to fall back to fetch?
Here are a few resources about using additional options with node-fetch (the library being used to make the HTTP requests).
node-fetch with RESTDataSource: https://github.com/apollographql/apollo-server/issues/1594node-fetchnode-fetchAn example from ^1 above to specify a timeout:
import { RequestInit } from 'apollo-server-env'
...
const requestInit: RequestInit = {
timeout: 1000,
compress: true,
}
...
const result = await this.get<Item>('item', params, requestInit)
So to expand on that example, here is an excerpt of available options for node-fetch extensions:
{
// The following properties are node-fetch extensions
follow: 20, // maximum redirect count. 0 to not follow redirect
timeout: 0, // req/res timeout in ms, it resets on redirect. 0 to disable (OS limit applies). Signal is recommended instead.
compress: true, // support gzip/deflate content encoding. false to disable
size: 0, // maximum response body size in bytes. 0 to disable
agent: null // http(s).Agent instance, allows custom proxy, certificate, dns lookup etc.
}
An (untested) example adding an agent setting:
# https://github.com/TooTallNate/node-https-proxy-agent
npm install https-proxy-agent
import HttpsProxyAgent from 'https-proxy-agent'
const requestInit: RequestInit = {
timeout: 1000,
compress: true,
agent: new HttpsProxyAgent('http://127.0.0.1:8580')
}
Let me know if this works for you, I'll be needing this soon for a project.
@hiucimon Were you able to test?
Hi guys,
I tried to @sbrichardson suggestion in my an extension of RestDataSource I'm using in all my datasources and it seems to work
const { RESTDataSource } = require('apollo-datasource-rest');
const HttpsProxyAgent = require('https-proxy-agent')
const uuid = require('uuid/v1');
class MyAwesomeBaseApi extends RESTDataSource {
willSendRequest(request) {
request.headers.set('x-request-origin', this.context.requestOrigin);
request.headers.set('va-request-id', this.context.requestId || uuid());
request.agent = new HttpsProxyAgent('http://127.0.0.1:8888/')
}
}
module.exports = MyAwesomeBaseApi
Most helpful comment
Hi guys,
I tried to @sbrichardson suggestion in my an extension of RestDataSource I'm using in all my datasources and it seems to work