Skip to content

Inability to Set Cookies in didReceiveResponse for Lambda Contexts #182

Open
@TimJLee

Description

@TimJLee

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.resand 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!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions