简体   繁体   English

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

[英]Multiple url path option error using graphql, aws lambda and serverless framework

I am using serverless framework to deploy a graphql nodejs package to lambda function.我正在使用无服务器框架将 graphql nodejs package 部署到 lambda ZC1C425268E683A945D1

My current serverless.yml file involves with a POST method for all communication and also another one for playground which looks like below.我当前的serverless.yml文件涉及一个用于所有通信的POST方法以及另一个用于游乐场的方法,如下所示。

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

And my handler.ts looks like this.我的 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;

What I would like to do now is have 3 different paths.我现在想做的是有3条不同的路径。 1 for secure and 1 for public and 1 for admin. 1 表示安全,1 表示公共,1 表示管理员。 So basically the URL would be something like.所以基本上 URL 就像这样。 localhost/public localhost/secre localhost/admin The secure path will use aws cognito pool to identify the API user api and and the other one would be open. localhost/public localhost/secre localhost/admin安全路径将使用 aws cognito 池来识别 API 用户 api 并且另一个将打开。 The admin will use another aws cognito admin pool.管理员将使用另一个 aws cognito 管理池。

So first what I did was add it like this for a secure one.所以首先我所做的就是像这样添加它以获得安全的。

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;

Then in my serverless.yml file tried to put it like this.然后在我的 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

It din't work and threw an error duplicated mapping key in "/Users/nihit/Desktop/serverless/cvtre/serverless.yml" at line 50, column -135: graphql:它不起作用并duplicated mapping key in "/Users/nihit/Desktop/serverless/cvtre/serverless.yml" at line 50, column -135: graphql:

I tried it in different ways but I am not really sure which one is the right way to get two different URL paths.我以不同的方式尝试过,但我不确定哪一种是获得两条不同的 URL 路径的正确方法。

The problem appears to be in your serverless.yml .问题似乎出在您的serverless.yml中。 In particular in the functions specification.特别是在功能规范中。 The combination of path and method as well as the function name must be unique for each function.对于每个 function, pathmethod的组合以及 function 名称必须是唯一的。

So, the serverless.yml should look like:因此, 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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用无服务器框架将 Python package 部署到 AWS lambda 时出错 - Error deploying Python package to AWS lambda using Serverless framework 带有 AWS Lambda 错误“找不到模块”的无服务器框架 - Serverless Framework with AWS Lambda error "Cannot find module" 错误请使用 AWS Lambda 无服务器框架手动安装 pg 包 - Error Please install pg package manually with AWS Lambda serverless framework 如何在不停机的情况下使用无服务器框架更新 AWS lambda? - How to update AWS lambda using serverless framework without downtime? 使用无服务器框架时如何在aws lambda函数中打包可执行文件? - How to package executables in aws lambda function when using serverless framework? 如何在使用无服务器框架时从AWS Lambda访问DynamoDB? - How to access DynamoDB from AWS Lambda when using the Serverless Framework? 在AWS Lambda无服务器框架中使用自定义vpc而不是默认vpc - using custom vpc instead of default vpc in aws lambda serverless framework 如何使用无服务器框架将 Streamlit(前端)Web 应用程序部署到 AWS Lambda? - How to deploy a Streamlit (frontend) webapp to AWS Lambda using Serverless Framework? 使用无服务器框架为 AWS Lambda 构建和使用本地包 - Build and use local package for AWS Lambda using serverless framework 使用 API 网关和无服务器框架在 AWS Lambda 上超过了速率 - Rate Exceeded on AWS Lambda Using API Gateway and serverless framework
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM