我正在使用无服务器框架将 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
中。特别是在功能规范中。 path
和 method
的组合以及函数名称对于每个函数必须是唯一的。
因此,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/