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

didReceiveResponse hook no longer available, blocking Apollo v4 upgrade #143

Closed
JudgeGreg opened this issue Jan 19, 2023 · 6 comments · Fixed by #145
Closed

didReceiveResponse hook no longer available, blocking Apollo v4 upgrade #143

JudgeGreg opened this issue Jan 19, 2023 · 6 comments · Fixed by #145

Comments

@JudgeGreg
Copy link

We are upgrading an Apollo server to v4 and noticed that in datasource-rest v5 didReceiveResponse hook was removed https://github.com/apollographql/datasource-rest/releases/tag/v5.0.0 .

With our current codebase we use that hook for adding some headers which would be fine with moving to the parseBody hook however, we also do some logging there that relies on both the request and response objects that were available in didReceiveResponse previously and are not in parseBody.

4b2a6f9 This commit message implies that this hook could be reinstated and we found a valid use case for it too. At the moment this is a blocker for us - could you consider adding this hook back and if yes, approximately when can we expect it?

Thank you

@JudgeGreg JudgeGreg changed the title didReceiveResponse hook no longer available, blocking v4 upgrade didReceiveResponse hook no longer available, blocking Apollo v4 upgrade Jan 19, 2023
@dycor
Copy link

dycor commented Jan 19, 2023

It's a blocker for me too

@trevor-scheer
Copy link
Member

trevor-scheer commented Jan 19, 2023

Hey @grego-e and @dycor. Have you taken a look at #109? I do think overriding fetch in the way I've suggested is sufficient for your use case (headers and logging). I won't claim it's the ideal ergonomic but hopefully unblocking for now at least.

It seems like there's enough interest here that I'm tempted to reinstate the hook, but I'm a bit hesitant only because people upgrading need to be aware of the subtle-ish change in semantics. I guess that's why we read changelogs, especially through major version upgrades.

I'd be open to a PR which adds some form of the hook back. Maybe willSendResponsewillReturnResponse is really what people want? I'm imagining a hook into the final result before it's returned with the parsedBody and response made available to the hook.

cc @glasser

@glasser
Copy link
Member

glasser commented Jan 19, 2023

I do feel like if the use case is "do something different right at the end of fetch" then overriding fetch in your subclass seems reasonable. The main downside here is if your overrided method would need access to anything other than the return value from super.fetch or the initial arguments to fetch (ie, something computed internally). Is that the case here? We could return more things from fetch if that helps.

@dycor
Copy link

dycor commented Jan 20, 2023

I need to access to the headers response , and with didReceiveResponse I can do this

@trevor-scheer
Copy link
Member

@dycor did you read #109?

In my suggestion you can access the response headers like so:

class MyDataSource extends RESTDataSource {
  override async fetch<TResult>(
    path: string,
    incomingRequest: DataSourceRequest = {},
  ) {
    return super.fetch<TResult>(path, incomingRequest).then((result) => {
      const response = result.response;
      const headers = response.headers;

      if (!response.ok) {
        // mutate response object accordingly
      }
      return result;
    });
  }
}

@JudgeGreg
Copy link
Author

Thanks for the fetch suggestion @trevor-scheer, we ended up overriding it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants