'Api gateway and lambda proxy cors problem

I have a problem with CORS in my API gateway lambda proxy. I was trying to struggle with this but still, I get No 'Access-Control-Allow-Origin' header is present on the requested resource or Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.

I use apollo client on frontend where I put 'Access-Control-Allow-Origin': '*', header:

const authLink = setContext((operation, { headers }) => {
 const token = window.localStorage.getItem('accessToken');
return {
headers: {
  ...headers,
  authorization: `Bearer ${token}`,
  'Access-Control-Allow-Origin': '*',
 }}}

Next, as a proxy, I use 'apollo-server-lambda' where I have below handler config:

const handler = server.createHandler({
expressGetMiddlewareOptions: {
cors: {
  origin: '*',
  credentials: true,
},}});

My graphql API gateway invokes some lambdas, every lambda is wrapped in below wrapper:

export const middyfy = (handler: any) => {
return middy(handler).use(middyJsonBodyParser()).use(cors());
};

My graphql proxy serverless configuration looks that:

 events: [
{
  http: {
    method: 'post',
    path: '/',
    integration: 'lambda-proxy',
    cors: true,
  },
},
{
  http: {
    method: 'get',
    path: '/',
    cors: true,
    integration: 'lambda-proxy',
  },
},
],

My API gateway OPTIONS configuration: enter image description here

I will be glad for any help



Solution 1:[1]

Problem solved, browser message was confusing. In the end, I had the error: No 'Access-Control-Allow-Origin' header is present on the requested resource. It was tricky because It wasn't any problem with CORS. I was passing wrong a Cognito JWT Token in my authorization header. Finally, I didn't need to pass 'Access-Control-Allow-Origin': '*', on the frontend side and use cors middleware on the lambda side.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Emil Buszy?o