-
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
Changes from 3 commits
d346bda
259b65f
8d2ed68
62a5896
3208973
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
--- | ||
'@apollo/datasource-rest': major | ||
--- | ||
|
||
Reduce responsibility of `didReceiveResponse` hook | ||
|
||
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 | ||
https://github.com/apollographql/apollo-server/issues/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. |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -176,16 +176,10 @@ export abstract class RESTDataSource { | |
request: FetcherRequestInit, | ||
): CacheOptions | undefined; | ||
|
||
protected async didReceiveResponse<TResult = any>( | ||
protected async didReceiveResponse?( | ||
response: FetcherResponse, | ||
_request: RequestOptions, | ||
): Promise<TResult> { | ||
if (response.ok) { | ||
return this.parseBody(response) as any as Promise<TResult>; | ||
} else { | ||
throw await this.errorFromResponse(response); | ||
} | ||
} | ||
request: RequestOptions, | ||
): Promise<void>; | ||
|
||
protected didEncounterError(error: Error, _request: RequestOptions) { | ||
throw error; | ||
|
@@ -353,9 +347,19 @@ export abstract class RESTDataSource { | |
cacheKey, | ||
cacheOptions, | ||
}); | ||
return await this.didReceiveResponse(response, outgoingRequest); | ||
|
||
if (this.didReceiveResponse) { | ||
await this.didReceiveResponse(response.clone(), outgoingRequest); | ||
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. Thought 2: Seems like another case where we need to include |
||
} | ||
|
||
if (response.ok) { | ||
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. While I do think that |
||
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; | ||
} | ||
}); | ||
}; | ||
|
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.
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.)