From ef4ad720ed59d92f6ebc6e628bca2034ec999eef Mon Sep 17 00:00:00 2001 From: Jesse Rosenberger Date: Thu, 16 Jul 2020 16:20:39 +0000 Subject: [PATCH] feat(gateway): Pass `fetch.Response` to `didEncounterError` after `request` Relates to https://github.com/apollographql/apollo-server/pull/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@8ff56090e26ab1f6aa8c1ab1a8cec680811ca507 --- gateway-js/CHANGELOG.md | 2 +- .../src/datasources/RemoteGraphQLDataSource.ts | 12 +++++++++--- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/gateway-js/CHANGELOG.md b/gateway-js/CHANGELOG.md index 3ff531803..ca762eede 100644 --- a/gateway-js/CHANGELOG.md +++ b/gateway-js/CHANGELOG.md @@ -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 diff --git a/gateway-js/src/datasources/RemoteGraphQLDataSource.ts b/gateway-js/src/datasources/RemoteGraphQLDataSource.ts index 700e78e2f..9a92be9f4 100644 --- a/gateway-js/src/datasources/RemoteGraphQLDataSource.ts +++ b/gateway-js/src/datasources/RemoteGraphQLDataSource.ts @@ -145,9 +145,11 @@ export class RemoteGraphQLDataSource = 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); @@ -164,7 +166,7 @@ export class RemoteGraphQLDataSource = Reco http: httpResponse, }; } catch (error) { - this.didEncounterError(error, httpRequest); + this.didEncounterError(error, httpRequest, httpResponse); throw error; } } @@ -183,7 +185,11 @@ export class RemoteGraphQLDataSource = Reco >, ): ValueOrPromise; - public didEncounterError(error: Error, _request: Request) { + public didEncounterError( + error: Error, + _request: Request, + _response?: Response + ) { throw error; }