【问题标题】:Error with "message": "Forbidden" trying to query an Apollo Serverless Lambda server“消息”错误:“禁止”尝试查询 Apollo Serverless Lambda 服务器
【发布时间】:2020-01-04 13:44:57
【问题描述】:

我正在尝试按照这个示例 https://www.apollographql.com/docs/apollo-server/deployment/lambda/ 创建一个返回“Hello world”的无服务器 GraphQL 服务器。

根目录基本上只包含两个文件,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"
  }
}

知道我在这里做错了什么吗?

【问题讨论】:

    标签: javascript node.js graphql apollo serverless


    【解决方案1】:

    好的,我发现了错误。 apollo-server-lamdba 存在一个已知问题。

    在浏览 Playground(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

    【讨论】:

    • 我仍然缺少一些东西。你做了什么来解决这个问题?
    • 即使在添加舞台开发后我也面临同样的问题。有什么建议吗?
    【解决方案2】:

    其实原教程已经有了解决方案(Setting up GraphQL Playground)。只需如下编辑您的代码。

    const server = new ApolloServer({ 
        typeDefs, 
        resolvers, 
        playground: {
            endpoint: "/dev/graphql"
        }
    });
    

    【讨论】:

      猜你喜欢
      • 2020-09-04
      • 1970-01-01
      • 2023-03-29
      • 1970-01-01
      • 2020-06-06
      • 1970-01-01
      • 1970-01-01
      • 2017-08-14
      • 2019-05-13
      相关资源
      最近更新 更多