我正在尝试遵循https://www.apollographql.com/docs/apollo-server/deployment/lambda/这个示例,以创建一个无服务器的GraphQL服务器,该服务器返回“ Hello world”。
根目录实际上仅包含两个文件graphql.js
和serverless.yml
:
> tree . -I node_modules
.
├── graphql.js
├── package-lock.json
├── package.json
└── serverless.yml
在此示例中,graphql.js
读取
const { ApolloServer, gql } = require('apollo-server-lambda');
const typeDefs = gql`
type Query {
hello: String
}
`;
const resolvers = {
Query: {
hello: () => 'Hello world!',
},
}
const server = new ApolloServer({ typeDefs, resolvers });
exports.graphqlHandler = server.createHandler();
和serverless.yml
读
service: apollo-lambda
provider:
name: aws
runtime: nodejs8.10
functions:
graphql:
handler: graphql.graphqlHandler
events:
- http:
path: graphql
method: post
cors: true
- http:
path: graphql
method: get
cors: true
这似乎已成功部署:
> serverless deploy
Serverless: Packaging service...
Serverless: Excluding development dependencies...
Serverless: Creating Stack...
Serverless: Checking Stack create progress...
.....
Serverless: Stack create finished...
Serverless: Uploading CloudFormation file to S3...
Serverless: Uploading artifacts...
Serverless: Uploading service apollo-lambda.zip file to S3 (4.55 MB)...
Serverless: Validating template...
Serverless: Updating Stack...
Serverless: Checking Stack update progress...
....................................
Serverless: Stack update finished...
Service Information
service: apollo-lambda
stage: dev
region: us-east-1
stack: apollo-lambda-dev
resources: 12
api keys:
None
endpoints:
POST - https://e9g6evoks0.execute-api.us-east-1.amazonaws.com/dev/graphql
GET - https://e9g6evoks0.execute-api.us-east-1.amazonaws.com/dev/graphql
functions:
graphql: apollo-lambda-dev-graphql
layers:
None
Serverless: Run the "serverless" command to setup monitoring, troubleshooting and testing.
但是,如果我去操场上询问“你好”,我会收到“禁止”消息:
就是我的
query {
hello
}
不会导致记录在https://www.apollographql.com/docs/apollo-server/essentials/server/上的响应,而是会导致
{
"error": {
"message": "Forbidden"
}
}
知道我在做什么错吗?
答案 0 :(得分:3)
好的,我发现了错误。 apollo-server-lamdba存在一个已知问题。
在浏览游乐场(在https://e9g6evoks0.execute-api.us-east-1.amazonaws.com/dev/graphql时),端点的url已被预先填充,但是错过了/dev/
部分(serverless.yml中定义的阶段部分)。只需添加它,它将正常工作。
cfr:https://github.com/apollographql/apollo-server/issues/2136#issuecomment-458465128
答案 1 :(得分:0)
其实原教程已经有了解决方案(Setting up GraphQL Playground)。只需按如下方式编辑您的代码。
const server = new ApolloServer({
typeDefs,
resolvers,
playground: {
endpoint: "/dev/graphql"
}
});