Skip to content

Commit

Permalink
feat(gateway): Pass fetch.Response to didEncounterError after `re…
Browse files Browse the repository at this point in the history
…quest`

Relates to apollographql/apollo-server#4380, but in
a non-breaking change, but also possibly just as a short-term band-aid while
we figure out the details.

Apollo-Orig-Commit-AS: apollographql/apollo-server@8ff5609
  • Loading branch information
abernix committed Jul 16, 2020
1 parent fe6c114 commit ef4ad72
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
2 changes: 1 addition & 1 deletion gateway-js/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

> The changes noted within this `vNEXT` section have not been released yet. New PRs and commits which introduce changes should include an entry in this `vNEXT` section as part of their development. When a release is being prepared, a new header will be (manually) created below and the appropriate changes within that release will be moved into the new section.
- _Nothing yet! Stay tuned!_
- The `RemoteGraphQLDataSource`'s `didEncounterError` method will now receive [`Response`](https://github.com/apollographql/apollo-server/blob/43470d6561bee31101f3afc56bdd154db3f92b30/packages/apollo-server-env/src/fetch.d.ts#L98-L111) as the third argument when it is available, making its signature `(error: Error, request: Request, response?: Response)`. This compliments the existing [`Request`](https://github.com/apollographql/apollo-server/blob/43470d6561bee31101f3afc56bdd154db3f92b30/packages/apollo-server-env/src/fetch.d.ts#L37-L45) type it was already receiving. Both of these types are [HTTP WHATWG Fetch API](https://fetch.spec.whatwg.org/) types, not `GraphQLRequest`, `GraphQLResponse` types.

## v0.17.0

Expand Down
12 changes: 9 additions & 3 deletions gateway-js/src/datasources/RemoteGraphQLDataSource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,9 +145,11 @@ export class RemoteGraphQLDataSource<TContext extends Record<string, any> = Reco
body: JSON.stringify(requestWithoutHttp),
});

let httpResponse: Response | undefined;

try {
// Use our local `fetcher` to allow for fetch injection
const httpResponse = await this.fetcher(httpRequest);
httpResponse = await this.fetcher(httpRequest);

if (!httpResponse.ok) {
throw await this.errorFromResponse(httpResponse);
Expand All @@ -164,7 +166,7 @@ export class RemoteGraphQLDataSource<TContext extends Record<string, any> = Reco
http: httpResponse,
};
} catch (error) {
this.didEncounterError(error, httpRequest);
this.didEncounterError(error, httpRequest, httpResponse);
throw error;
}
}
Expand All @@ -183,7 +185,11 @@ export class RemoteGraphQLDataSource<TContext extends Record<string, any> = Reco
>,
): ValueOrPromise<GraphQLResponse>;

public didEncounterError(error: Error, _request: Request) {
public didEncounterError(
error: Error,
_request: Request,
_response?: Response
) {
throw error;
}

Expand Down

0 comments on commit ef4ad72

Please sign in to comment.