Skip to content

Commit

Permalink
Use Object.fromEntries to convert Headers into a plain object.
Browse files Browse the repository at this point in the history
As helpfully debugged and identified by @dsanders11 (Thank you!), this was a
mistake in my original implementation that incorrectly spread the iterable
`Headers` into the `headers` object of the response.

While I would have loved to create a regression test for this, there was no
place which was currently wired up to support handler specific options (like
`cors`), which is sometimes on the top-level constructor to `ApolloServer`,
and sometimes on `createHandler` or `applyMiddleware`.  I did try to futz
with it for a full hour to no avail.  I'm already aware of a number of
limitations with the current so-called `integration-testsuite`, and I'm
putting that on the list of things to address in an upcoming overhaul of
that testing infra.

That said, I've manually tested this and I think it's working properly.  An
upcoming release should hopefully confirm that.

Ref: https://github.com/apollographql/apollo-server/pull/2674/files#r288053103
Ref: #2718 (comment)
  • Loading branch information
abernix committed May 28, 2019
1 parent dc2d4ed commit 7e78a23
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions packages/apollo-server-lambda/src/ApolloServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,13 +138,19 @@ export class ApolloServer extends ApolloServerBase {
}
}

// Convert the `Headers` into an object which can be spread into the
// various headers objects below.
const requestCorsHeadersObject = Object.fromEntries(
Array.from(requestCorsHeaders),
);

if (event.httpMethod === 'OPTIONS') {
context.callbackWaitsForEmptyEventLoop = false;
return callback(null, {
body: '',
statusCode: 204,
headers: {
...requestCorsHeaders,
...requestCorsHeadersObject,
},
});
}
Expand All @@ -167,7 +173,7 @@ export class ApolloServer extends ApolloServerBase {
statusCode: 200,
headers: {
'Content-Type': 'text/html',
...requestCorsHeaders,
...requestCorsHeadersObject,
},
});
}
Expand All @@ -180,7 +186,7 @@ export class ApolloServer extends ApolloServerBase {
...result,
headers: {
...result.headers,
...requestCorsHeaders,
...requestCorsHeadersObject,
},
},
);
Expand Down

0 comments on commit 7e78a23

Please sign in to comment.