Apollo-server: How to include headers in RESTDataSource request?

Created on 29 Apr 2019  路  5Comments  路  Source: apollographql/apollo-server

I'm trying to use apollo RESTDataSource to wrap my rest api. I need to pass some headers to the api call.

I'm following the example from the docs: https://www.apollographql.com/docs/apollo-server/features/data-sources#intercepting-fetches

This is my code:

  willSendRequest(request: RequestOptions) {
    console.log(`request 1: ${JSON.stringify(request)}`);
    request.headers.set('Authorization', this.context.authorization);
    console.log(`request 2: ${JSON.stringify(request)}`);
  }

I'm expecting the headers to contain 'Authorization'. But it's always empty.

The log from the above code:

request 1: {"method":"POST","path":"partnerinvoices","body":{"command": "input","params":{},"headers":{}}
request 2: {"method":"POST","path":"partnerinvoices","body":{"command":"input","params":{},"headers":{}}

I can override body and params in willSendRequest method without any problem.

Most helpful comment

The third option to, e.g. post, put, delete, etc. allows passing of fetch options. For example:

class MoviesAPI extends RESTDataSource {
  constructor() {
    super();
    this.baseURL = 'https://movies-api.example.com/';
  }

  async getMovie(id) {
    return this.get(`movies/${id}`, undefined, {
      headers: {
        'Authorization': 'Bearer 123abc',
      },
    });
  }
}

All 5 comments

That is exactly how I'm setting the header myself, so I'm not sure what's wrong. Are you certain the value in this.context.authorization is not undefined?

I'm using it like as follows, for which I'm certain it works.
willSendRequest (request) { request.headers.set('csrftoken', this.context.token) }

It's really hard to tell what the issue here without your complete setup. Do you have any repo that we can take a look at ? I have the save logic for willSendRequest and it working just fine.

The third option to, e.g. post, put, delete, etc. allows passing of fetch options. For example:

class MoviesAPI extends RESTDataSource {
  constructor() {
    super();
    this.baseURL = 'https://movies-api.example.com/';
  }

  async getMovie(id) {
    return this.get(`movies/${id}`, undefined, {
      headers: {
        'Authorization': 'Bearer 123abc',
      },
    });
  }
}

The third option to, e.g. post, put, delete, etc. allows passing of fetch options. For example:

class MoviesAPI extends RESTDataSource {
  constructor() {
    super();
    this.baseURL = 'https://movies-api.example.com/';
  }

  async getMovie(id) {
    return this.get(`movies/${id}`, undefined, {
      headers: {
        'Authorization': 'Bearer 123abc',
      },
    });
  }
}

@abernix Thanks for your answer, my code worked after adding undefined to the GET request, but I'm confused that what is undefined in the syntax? Any documentation for reference? Thank you!

@abernix Thanks for your answer, my code worked after adding undefined to the GET request, but I'm confused that what is undefined in the syntax? Any documentation for reference? Thank you!

Second function param is to add (url)query parameters, so you don't have to manually add it to the path yourself.
Its usage and source can be seen here.
I'm not sure if there are extensive official docs on it, but you can quite easily look in the source.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

dupski picture dupski  路  3Comments

jpcbarros picture jpcbarros  路  3Comments

nevyn-lookback picture nevyn-lookback  路  3Comments

bryanerayner picture bryanerayner  路  3Comments

espoal picture espoal  路  3Comments