Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is there any way to specify a proxy on RESTDataSource? #2090

Closed
hiucimon opened this issue Dec 12, 2018 · 3 comments
Closed

Is there any way to specify a proxy on RESTDataSource? #2090

hiucimon opened this issue Dec 12, 2018 · 3 comments

Comments

@hiucimon
Copy link

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?

@sbrichardson
Copy link
Contributor

sbrichardson commented Dec 13, 2018

Here are a few resources about using additional options with node-fetch (the library being used to make the HTTP requests).

  1. Details about passing options to node-fetch with RESTDataSource: RESTDataSource - fetch timeout #1594
  2. Options for node-fetch
  3. Specifying a proxy with node-fetch

An 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.

@sbrichardson
Copy link
Contributor

sbrichardson commented Dec 23, 2018

@hiucimon Were you able to test?

@jfreyre
Copy link

jfreyre commented Jan 7, 2019

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 

@abernix abernix closed this as completed Sep 1, 2019
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 21, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants