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

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

Closed
goatyeahh opened this issue Jul 10, 2018 · 2 comments
Closed
Labels
⛲️ feature New addition or enhancement to existing solutions

Comments

@goatyeahh
Copy link
Contributor

goatyeahh commented Jul 10, 2018

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)

@ghost ghost added the ⛲️ feature New addition or enhancement to existing solutions label Jul 10, 2018
@goatyeahh
Copy link
Contributor Author

PR merged

@christrude
Copy link

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

trevor-scheer added a commit to apollographql/datasource-rest that referenced this issue Dec 3, 2022
The naming of this hook is deceiving; if this hook is overridden it becomes
responsible for returning the parsed body. It was originally introduced in
apollographql/apollo-server#1324, where the author
claims they implemented it due to lack of access to the complete response
(headers) in the fetch methods (get, post, ...). This approach isn't a type safe
way to acoomplish this.

This hook is now just an observability hook which receives a clone of the
response and the request that was sent.

A change following this will introduce the ability to fetch a complete response
(headers included) aside from the provided fetch methods which only return a
body, which will reinstate the functionality that the author of this hook had
originally intended.
trevor-scheer added a commit to apollographql/datasource-rest that referenced this issue Dec 5, 2022
The naming of this hook is deceiving; if this hook is overridden it becomes
responsible for returning the parsed body and handling errors if they occur. It
was originally introduced in
apollographql/apollo-server#1324, where the author
implemented it due to lack of access to the complete response (headers) in the
fetch methods (get, post, ...). This approach isn't a type safe way to
accomplish this and places the burden of body parsing and error handling on the
user.

Removing this hook is a prerequisite to a subsequent change that will introduce
the ability to fetch a complete response (headers included) aside from the
provided fetch methods which only return a body. This change will reinstate the
functionality that the author of this hook had originally intended in a more
direct manner.

We can consider reinstating this hook or something similar in the future with
well-documented and use-case-driven semantics. Without knowing other
reasons for why this hook should exist (aside from mentioned issue, which will
be addressed with an alternative approach), trying to guess what this hook
should do or mean is a bit pointless.
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 20, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
⛲️ feature New addition or enhancement to existing solutions
Projects
None yet
Development

No branches or pull requests

2 participants