Description
Dear Apollo Team,
Previously, in an ECS-based setup(using NestJS), it was possible to set cookies in the didReceiveResponse
method by accessing context.req.res
and modifying the response headers. This allowed cookies received from subgraphs (via set-cookie headers) to be forwarded to the client.
However, in a Lambda-based setup(not using NestJS), the context object does not provide access to the response object (context.req.res
). Instead, cookies need to be sent back to the client by attaching them to the result.cookies
property using middleware.
Unfortunately, I have not been able to find a straightforward way to bridge the cookies retrieved in didReceiveResponse
to the middleware where result.cookies
is set.
Here is our code
// DataSource
export class AuthenticatedDataSource extends RemoteGraphQLDataSource {
didReceiveResponse({ request, response, context }: any): typeof response {
const cookies = response.http.headers?.raw()['set-cookie'] as string[] | null;
if (cookies) {
context?.req.res.append('set-cookie', cookies); // This line is not working on Lambda
}
return response;
}
}
const gateway = new ApolloGateway({
// Other configurations omitted for brevity
buildService({ name, url }) {
return new AuthenticatedDataSource({ url });
},
});
// cookieMiddleware
const cookieMiddleware: middleware.MiddlewareFn<typeof requestHandler> = async (event) => {
const cookie = event.cookies; // This is not the cookie from subgraph
return async (result) => {
if (cookie) {
result.cookies = cookie; // I want to set the cookies received from the subgraph into result.cookies
}
};
};
Versions we are using:
"@apollo/gateway": "2.9.1",
"@apollo/server": "4.11.0",
"@as-integrations/aws-lambda": "3.1.0",
Is there a way to address this issue? Any guidance would be greatly appreciated.
Thank you!