apollographql / datasource-rest

A caching data source for REST APIs
MIT License
39 stars 20 forks source link

HELP: Proper way to set http proxy #325

Closed MarvinXu closed 6 months ago

MarvinXu commented 6 months ago

I need to use a proxy to access a REST API, but I'm struggling to find a solution. First I found: https://github.com/apollographql/apollo-server/issues/2090#issuecomment-451926660, but it doesn't work anymore. Then I found this: https://github.com/apollographql/datasource-rest/issues/189#issuecomment-1524058536, with that and more searching for 'proxy agent' I finally piece together a working solution:

import { HttpsProxyAgent } from 'https-proxy-agent';
import { Fetcher } from '@apollo/utils.fetcher';
import fetch from 'node-fetch';

const agent = new HttpsProxyAgent('http://LAPTOP-TK2S7QTO.local:7897')
const customFetcher: Fetcher = (url, init) => {
  return fetch(url, {...init, agent})
}

const { url } = await startStandaloneServer(server, {
  context: async () => {
    const { cache } = server;
    return {
      dataSources: {
        spotifyAPI: new SpotifyAPI({ cache, fetch: customFetcher }),
      },
    };
  },
});

My questions are:

MarvinXu commented 6 months ago

Found the official doc Configuring the proxy using environment variables