我正在使用无服务器框架将graphql-nodejs包部署到lambda函数。
我当前的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
我的手柄看起来是这样的。
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;
我现在想做的是有三条不同的道路。1用于安全,1用于公共,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
它不起作用,并引发了一个错误duplicated mapping key in "/Users/nihit/Desktop/serverless/cvtre/serverless.yml" at line 50, column -135:
graphql:
我尝试了不同的方法,但我真的不确定哪一种是获得两个不同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