如何为 apollo-server-lambda 启用 cors

Fel*_*eno 5 cors aws-lambda apollo-server apollo-client apollo-angular

好吧,我看到了很多关于如何为 apollo-express 启用 cors 的答案,但我还没有找到真正适合 apollo-server-lambda 的答案。

这是我从 chrome 收到的错误:

Access to XMLHttpRequest at 'https://5j3gae3086.execute-api.us-east-2.amazonaws.com/alpha/'
from origin 'http://localhost:4200' has been blocked by CORS policy: Response to preflight
request doesn't pass access control check: The 'Access-Control-Allow-Origin' header
has a value 'https://example.com' that is not equal to the supplied origin.
Run Code Online (Sandbox Code Playgroud)

我不知道如何更改值“https://example.com”。这是我尝试创建服务器的代码:

Access to XMLHttpRequest at 'https://5j3gae3086.execute-api.us-east-2.amazonaws.com/alpha/'
from origin 'http://localhost:4200' has been blocked by CORS policy: Response to preflight
request doesn't pass access control check: The 'Access-Control-Allow-Origin' header
has a value 'https://example.com' that is not equal to the supplied origin.
Run Code Online (Sandbox Code Playgroud)

我还需要在这里做什么?

编辑 我不确定这是否相关,但这是我的 graphql.module.ts 文件。这就是我在前端设置 grahql 的方式:

const { ApolloServer } = require('apollo-server-lambda')
const typeDefs = require('./schema')
const resolvers = require ('./resolvers')

const server = new ApolloServer({
    typeDefs,
    resolvers,
    introspection: true,
    playground: {
        endpoint: "/alpha/graphql",
    },
  });

exports.graphqlHandler = server.createHandler({
    cors: {
        // origin: true,
        origin: "http://localhost:4200", // <-- This is not changing the header value. Do I need to do it from the frontend?
        credentials: true,
    },
  });
Run Code Online (Sandbox Code Playgroud)

另外,如果有人好奇,我正在使用 AWS Api Gateway 来使用 lambda,但我相信我已经正确添加了 cors 配置。

AWS Cors配置

我对此不知所措。我需要改变什么?

Ayd*_*hew 3

按照此处的 CORS 设置说明,我可以成功使用 apollo-Angular 返回简单查询的结果。不需要特殊的标头等。

https://www.apollographql.com/docs/apollo-server/deployment/lambda/

// serverless.yml
events:
  - http:
      path: graphql
      method: post
      cors: true
  - http:
      path: graphql
      method: get
      cors: true
Run Code Online (Sandbox Code Playgroud)
// graphql.js
exports.graphqlHandler = server.createHandler({
  cors: {
    origin: '*',
    credentials: true,
  },
});
Run Code Online (Sandbox Code Playgroud)
// graphql.module.ts
import {NgModule} from '@angular/core';
import {APOLLO_OPTIONS} from 'apollo-angular';
import {ApolloClientOptions, InMemoryCache} from '@apollo/client/core';
import {HttpLink} from 'apollo-angular/http';

const uri = 'https://xxx/dev/graphql';
export function createApollo(httpLink: HttpLink): ApolloClientOptions<any> {
  return {
    link: httpLink.create({uri}),
    cache: new InMemoryCache(),
  };
}

@NgModule({
  providers: [
    {
      provide: APOLLO_OPTIONS,
      useFactory: createApollo,
      deps: [HttpLink],
    },
  ],
})
export class GraphQLModule {}

Run Code Online (Sandbox Code Playgroud)
// In Angular 10
this.apollo
  .watchQuery({
    query: gql`
      {
        users {
          email
        }
      }
    `,
  })
  .valueChanges.subscribe(result => {
    console.log(result.data);
  });
Run Code Online (Sandbox Code Playgroud)