Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set CORS headers #39

Closed
glasser opened this issue Oct 18, 2022 · 6 comments
Closed

Set CORS headers #39

glasser opened this issue Oct 18, 2022 · 6 comments

Comments

@glasser
Copy link

glasser commented Oct 18, 2022

Because the handler exposed by this package is not a modular piece of middleware, it probably makes sense for it to function like startStandaloneServer and have the default behavior of setting access-control-allow-origin: * and friends. (Then potentially layer some mechanism for CORS configuration on top of that, or just tell people who need more configuration to do something like #38.)

@glasser
Copy link
Author

glasser commented Oct 20, 2022

Or is the theory that if you're using API Gateway you would just enable CORS via API Gateway rather than in the function?

@poad
Copy link

poad commented Oct 22, 2022

We suspect that this issue is caused by the fact that Apollo Server v4 no longer has an interface to directly link express middleware.

Currently, the cheapest solution is to wrap startServerAndCreateLambdaHandler(server) with Access-Control-Allow-Origin header instead of directly with the Lambda Handler, as shown below. header?

async function handler(event: GatewayEvent, context: Context, callback: Callback<APIGatewayProxyStructuredResultV2 | APIGatewayProxyResult>) {
  const apolloHandler = startServerAndCreateLambdaHandler(server);
  const resp = await apolloHandler(event, context, callback);
  return {
    ...resp,
    headers: {
      ...resp?.headers,
      'Access-Control-Allow-Origin': '*',
    },
  };
}

export default handler;

If possible, it would be great if startServerAndCreateLambdaHandler() could specify a callback function to customize the response.

@glasser
Copy link
Author

glasser commented Oct 22, 2022

Yes, though if you enjoyed that interface, you can see #38 for an alternate approach that doesn't use this package (and which is powered by the same package that powered apollo-server-lambda in AS3).

@poad
Copy link

poad commented Oct 22, 2022

@glasser Thx!
I suggested a different approach from #38 because the response tends to be slower with #38's approach and I wanted to support CORS with the Lambda Handler using less description.

@BlenderDude
Copy link
Member

You can now use middleware to mutate headers on the way out with v2.0.0! See README for more info!

@BlenderDude
Copy link
Member

We suspect that this issue is caused by the fact that Apollo Server v4 no longer has an interface to directly link express middleware.

Currently, the cheapest solution is to wrap startServerAndCreateLambdaHandler(server) with Access-Control-Allow-Origin header instead of directly with the Lambda Handler, as shown below. header?

async function handler(event: GatewayEvent, context: Context, callback: Callback<APIGatewayProxyStructuredResultV2 | APIGatewayProxyResult>) {
  const apolloHandler = startServerAndCreateLambdaHandler(server);
  const resp = await apolloHandler(event, context, callback);
  return {
    ...resp,
    headers: {
      ...resp?.headers,
      'Access-Control-Allow-Origin': '*',
    },
  };
}

export default handler;

If possible, it would be great if startServerAndCreateLambdaHandler() could specify a callback function to customize the response.

@poad

This "callback function" is now implemented as middlware in the new version!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants