[英]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, path
和method
的组合以及 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.