Apollo-server: Apollo server 2.0. - RESTDataSource access to full response obj (headers,...)

Created on 10 Jul 2018  路  2Comments  路  Source: apollographql/apollo-server

In my datasource, I currently need access to the headers of the response.

To do that I added a protected method didReceiveResponse like didReceiveErrorResponse in the RESTDataSource class. I can then overwrite this method in an implementation of a RESTDataSource class where I need access to the headers (e.g to return the link in the headers).

Example

export class MovieAPI extends RESTDataSource {
  ...

  protected async didReceiveResponse(response) {
    const headersLink = response.headers.get("link")
    const body = await response.json()
    return { body, headersLink: headersLink }
  }

  ...

  public async getMovies(): Promise<String[]> {
    const { body, headersLink } = this.get(`movies`)
    // do something with headers link
    ...
    retrun body
  }

  ...
}

I have created a PR to add this method to RESTDataSource.ts (PR #1325)

鉀诧笍 feature

Most helpful comment

Are there docs on how to use this functionality? I am not able to get it to work as the explanation above leads me to believe

All 2 comments

PR merged

Are there docs on how to use this functionality? I am not able to get it to work as the explanation above leads me to believe

Was this page helpful?
0 / 5 - 0 ratings

Related issues

leinue picture leinue  路  3Comments

veeramarni picture veeramarni  路  3Comments

danilobuerger picture danilobuerger  路  3Comments

Magneticmagnum picture Magneticmagnum  路  3Comments

jpcbarros picture jpcbarros  路  3Comments