2
votes

I'm trying to following this example, https://www.apollographql.com/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.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.

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

enter image description here

That is, my

query {
  hello
}

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

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

Any idea what I'm doing wrong here?

2

2 Answers

5
votes

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.com/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.com/apollographql/apollo-server/issues/2136#issuecomment-458465128

0
votes

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"
    }
});