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

Remove didReceiveResponse hook #107

Merged
merged 5 commits into from
Dec 5, 2022

Conversation

trevor-scheer
Copy link
Member

@trevor-scheer trevor-scheer commented Dec 3, 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.

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.
@codesandbox-ci
Copy link

codesandbox-ci bot commented Dec 3, 2022

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Copy link
Member

@glasser glasser left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've actually forgotten which ticket we're trying to close with this — or is it just kinda a prereq for #49?

return await this.didReceiveResponse(response, outgoingRequest);

if (this.didReceiveResponse) {
await this.didReceiveResponse(response.clone(), outgoingRequest);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thought 1: why a clone? Would it be valuable to be able to use this hook to mutate the response if you want? (And it does affect performance negatively to do so.)

return await this.didReceiveResponse(response, outgoingRequest);

if (this.didReceiveResponse) {
await this.didReceiveResponse(response.clone(), outgoingRequest);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thought 2: Seems like another case where we need to include url alongside outgoingRequest (not new in this PR, just while we're here)

await this.didReceiveResponse(response.clone(), outgoingRequest);
}

if (response.ok) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While I do think that didReceiveResponse was the wrong place for this logic, it does kinda feel like it would be helpful for users to be able to differentiate between the error and not cases (eg, maybe you have an API where you want to do non-error for some specific 4xx response, idk). Not sure if this means adding yet another hook, or leaving didReceiveResponse as kinda similar to current semantics but with a better name, or what. (Or just waiting until a user says they can't upgrade without this feature.)

if (response.ok) {
return (await this.parseBody(response)) as TResult;
} else {
throw await this.errorFromResponse(response);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

completely unrelated: the body of errorFromResponse could be rewritten to just pass the response extension directly to the error constructor instead of being written how it is now (and maybe drop the subclasses and put the error extensions directly into the GraphQLError call, idk)

@trevor-scheer trevor-scheer changed the title Reduce responsibility of didReceiveResponse hook Remove didReceiveResponse hook Dec 5, 2022
@trevor-scheer trevor-scheer marked this pull request as ready for review December 5, 2022 22:04
Copy link
Member

@glasser glasser left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is a step in the right direction. Maybe let's track somewhere the fact that we took away the ability to differentiate error from non-error. Though maybe the #49 solution could fix that?

@trevor-scheer
Copy link
Member Author

@glasser I opened #109

@trevor-scheer trevor-scheer merged commit 4b2a6f9 into main Dec 5, 2022
@trevor-scheer trevor-scheer deleted the trevor/inline-did-receive-response branch December 5, 2022 22:27
@github-actions github-actions bot mentioned this pull request Dec 5, 2022
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 this pull request may close these issues.

2 participants