javascript - Error with "message": "Forbidden" trying to query an Apollo Serverless Lambda s

I'm trying to following this example, , to create a serverless GraphQL server which returns '

I'm trying to following this example, /, to create a serverless GraphQL server which returns 'Hello world'.

The root directory contains essentially just two files, graphql.js and serverless.yml:

> tree . -I node_modules
.
├── graphql.js
├── package-lock.json
├── package.json
└── serverless.yml

where, as in the example, graphql.js reads

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();

and serverless.yml reads

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

This seems to deploy successfully:

> 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 - 
  GET - 
functions:
  graphql: apollo-lambda-dev-graphql
layers:
  None
Serverless: Run the "serverless" mand to setup monitoring, troubleshooting and testing.

However, if I go to the playground and query 'hello', I get a 'forbidden' message:

That is, my

query {
  hello
}

rather than resulting in the response documented at /, results in a

{
  "error": {
    "message": "Forbidden"
  }
}

Any idea what I'm doing wrong here?

I'm trying to following this example, https://www.apollographql./docs/apollo-server/deployment/lambda/, to create a serverless GraphQL server which returns 'Hello world'.

The root directory contains essentially just two files, graphql.js and serverless.yml:

> tree . -I node_modules
.
├── graphql.js
├── package-lock.json
├── package.json
└── serverless.yml

where, as in the example, graphql.js reads

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();

and serverless.yml reads

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

This seems to deploy successfully:

> 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./dev/graphql
  GET - https://e9g6evoks0.execute-api.us-east-1.amazonaws./dev/graphql
functions:
  graphql: apollo-lambda-dev-graphql
layers:
  None
Serverless: Run the "serverless" mand to setup monitoring, troubleshooting and testing.

However, if I go to the playground and query 'hello', I get a 'forbidden' message:

That is, my

query {
  hello
}

rather than resulting in the response documented at https://www.apollographql./docs/apollo-server/essentials/server/, results in a

{
  "error": {
    "message": "Forbidden"
  }
}

Any idea what I'm doing wrong here?

Share Improve this question asked Aug 31, 2019 at 20:27 Kurt PeekKurt Peek 58k104 gold badges346 silver badges565 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 5

Ok, i found the error. There is a known issue with apollo-server-lamdba.

While browsing the playground (at https://e9g6evoks0.execute-api.us-east-1.amazonaws./dev/graphql), the url of the endpoint is prefiled but it misses the /dev/ part (the stage part defined in serverless.yml). Just add it and this will work fine.

cfr: https://github./apollographql/apollo-server/issues/2136#issuement-458465128

Actually, the original tutorial already has the solution (Setting up GraphQL Playground). Just edit your code as below.

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745112665a4611938.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信