0

I tried to using below code, but it is failing to reach api with exception.

class CountriesAPI extends RESTDataSource {
  constructor() {
    super();
    this.baseURL = "http://localhost:9321";
  }
// this is the area i want to add custom headers before graphql calls rest-api
  willSendRequest(request) {
    request.headers.set(

      "name", `Tat`
    );
    console.log(request);
  }

  async getAllCountries() {
    // return this.get("/getData");

    try {
      return this.get(`/getData`);
    } catch (error) {
      console.error(error);
    }
  }

}

I have tried to get response from rest-api using custom headers concept but with the above code, apollo-server is failed to trigger request to rest-api with custom headers.

Tat
  • 19
  • 1
  • 4

1 Answers1

1

Just needed to do the below:

willSendRequest(path,request) { request.headers['whatever'] ='whatever'; request.headers['whatever'] ='whatever'; } 
Blue Robin
  • 847
  • 2
  • 11
  • 31
Tat
  • 19
  • 1
  • 4
  • It is good that you have answered your own question. However, It would be helpful to provide resources as to *why* this worked. Information as to where you solved this problem is crucial. Code-only answers without explanation are poorly received by the community. Please see [here](https://stackoverflow.com/help/how-to-answer) – Blue Robin Jun 05 '23 at 02:35