generated from apollographql/typescript-repo-template
-
Notifications
You must be signed in to change notification settings - Fork 20
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
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
d346bda
Reduce responsibility of `didReceiveResponse` hook
trevor-scheer 259b65f
prettier
trevor-scheer 8d2ed68
test cleanup / correctness
trevor-scheer 62a5896
Remove didReceiveResponse hook altogether
trevor-scheer 3208973
remove unused import
trevor-scheer File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
--- | ||
'@apollo/datasource-rest': major | ||
--- | ||
|
||
Remove `didReceiveResponse` hook | ||
|
||
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 | ||
https://github.com/apollographql/apollo-server/issues/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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -176,17 +176,6 @@ export abstract class RESTDataSource { | |
request: FetcherRequestInit, | ||
): CacheOptions | undefined; | ||
|
||
protected async didReceiveResponse<TResult = any>( | ||
response: FetcherResponse, | ||
_request: RequestOptions, | ||
): Promise<TResult> { | ||
if (response.ok) { | ||
return this.parseBody(response) as any as Promise<TResult>; | ||
} else { | ||
throw await this.errorFromResponse(response); | ||
} | ||
} | ||
|
||
protected didEncounterError(error: Error, _request: RequestOptions) { | ||
throw error; | ||
} | ||
|
@@ -353,9 +342,15 @@ export abstract class RESTDataSource { | |
cacheKey, | ||
cacheOptions, | ||
}); | ||
return await this.didReceiveResponse(response, outgoingRequest); | ||
|
||
if (response.ok) { | ||
return (await this.parseBody(response)) as TResult; | ||
} else { | ||
throw await this.errorFromResponse(response); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
} | ||
} catch (error) { | ||
this.didEncounterError(error as Error, outgoingRequest); | ||
throw error; | ||
} | ||
}); | ||
}; | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.)