amazon-web-services - 使用 graphql、aws lambda 和无服务器框架的多个 url 路径选项错误

标签 amazon-web-services aws-lambda graphql serverless-framework serverless

我正在使用无服务器框架将 graphql nodejs 包部署到 lambda 函数。

我当前的 serverless.yml 文件涉及一个用于所有通信的 POST 方法以及另一个用于 playground 的方法,如下所示。

functions:
  graphql:
    handler: handler.server
    events:
      - http:
          path: /
          method: post
          cors: true
  playground:
    handler: handler.playground
    events:
      - http:
          path: /
          method: get
          cors: true

我的 handler.ts 看起来像这样。

const { GraphQLServerLambda } = require("graphql-yoga");
const {documentSubmissionMutation} = require('./mutations/documentMutation');
const {signUpMutation, whatever} = require('./mutations/signUpMutation');

const typeDefs = `
  type Query {
    hello(name: String): String!
  },
  type Mutation {
    signUp(
      email: String!
      password: String!
    ): String

    sendDocuments(
      user_id: String!
      documents: String!
    ): String!
  }
`

const resolvers = {
  Query : {
    hello : whatever
  },
  Mutation: {
    sendDocuments: documentSubmissionMutation,
    signUp: signUpMutation,
  }
}

const lambda = new GraphQLServerLambda({
    typeDefs,
    resolvers
});

exports.server = lambda.graphqlHandler;
exports.playground = lambda.playgroundHandler;

我现在想做的是拥有 3 条不同的路径。 1 个用于安全,1 个用于公共(public),1 个用于管理员。 所以基本上 URL 应该是这样的。 localhost/public localhost/secre localhost/admin 安全路径将使用 aws cognito 池来识别 API 用户 api,另一个将打开。管理员将使用另一个 aws cognito 管理池。

所以我首先做的是像这样添加它以确保安全。

const lambda = new GraphQLServerLambda({
    typeDefs,
    resolvers,
    context: req => ({ ...req })
});

const lambdaSecure = new GraphQLServerLambda({
  typeDefsSecure,
  resolversSecure,
  context: req => ({ ...req })
});


exports.server = lambda.graphqlHandler;
exports.playground = lambda.playgroundHandler;

exports.serverSecure = lambdaSecure.graphqlHandler;
exports.playgroundSecure = lambdaSecure.playgroundHandler;

然后在我的 serverless.yml 文件中尝试这样写。

functions:
  graphql:
    handler: handler.server
    events:
      - http:
          path: /
          method: post
          cors: true
  graphql:
    handler: handler.serverSecure
    events:
      - http:
          path: /
          method: post
          cors: true

  playground:
    handler: handler.playground
    events:
      - http:
          path: /
          method: get
          cors: true
  playground:
    handler: handler.playgroundSecure
    events:
      - http:
          path: /
          method: get
          cors: true

它不起作用并抛出错误 “/Users/nihit/Desktop/serverless/cvtre/serverless.yml”中第 50 行第 -135 列的重复映射键: 图形数据库:

我尝试了不同的方法,但我不确定哪一种方法是获得两个不同 URL 路径的正确方法。

最佳答案

问题似乎出在您的 serverless.yml 中。特别是在功能规范中。 pathmethod 的组合以及函数名称对于每个函数必须是唯一的。

因此,serverless.yml 应该如下所示:

functions:
  graphqlServer:
    handler: handler.server
    events:
      - http:
          path: server/public
          method: post
          cors: true
  graphqlServerSecure:
    handler: handler.serverSecure
    events:
      - http:
          path: server/secure
          method: post
          cors: true

  playground:
    handler: handler.playground
    events:
      - http:
          path: playground/public
          method: get
          cors: true
  playgroundSecure:
    handler: handler.playgroundSecure
    events:
      - http:
          path: playground/secure
          method: get
          cors: true

关于amazon-web-services - 使用 graphql、aws lambda 和无服务器框架的多个 url 路径选项错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58871398/

相关文章:

amazon-web-services - 使用 CDK 时,如何使我的 CloudFormation/CodePipeline 更新域名以指向 S3 存储桶?

python - 将输入(参数)从阶跃函数传递并使用到 lambda 任务

python - 执行 Lambda python 函数时模块导入错误

node.js - 如何使用无服务器框架将环境变量传递给 AWS Lambda 函数?

postgresql - 我应该在 PostgreSQL 还是服务器后端过滤数据?

javascript - GraphQL 突变的响应应该是什么?

python - Graphite 烯 graphql 字典作为一种类型

codeigniter - 将 AWS SDK 作为库集成到 Codeigniter 中

mysql - 在 .NET AWS Lambda 中使用 MySql.Data 和 D.I

amazon-web-services - 错误: Unexpected server response: 502 on trying to connect to a lambda function through Amazon API gateway Websocket API