Apollo-server: Apollo 2 serverless giving 403

Created on 1 Mar 2019  路  15Comments  路  Source: apollographql/apollo-server

I followed this tutorial https://www.apollographql.com/docs/apollo-server/deployment/lambda.html and when i hit api endpoint, i get following error

"error": "Response not successful: Received status code 403"

Here is my code
graphql.js

`const { ApolloServer, gql } = require('apollo-server-lambda');

// Construct a schema, using GraphQL schema language
const typeDefs = gql type Query { hello: String } ;

// Provide resolver functions for your schema fields
const resolvers = {
Query: {
hello: () => 'Hello world!',
},
};

const server = new ApolloServer({
typeDefs,
resolvers,
context: ({ event, context }) => ({
headers: event.headers,
functionName: context.functionName,
event,
context,
}),
introspection: true,
playground: true,
});

exports.graphqlHandler = server.createHandler({
cors: {
origin: '*',
credentials: true,
},
});`

My function definition looks like following
functions: graphql: handler: src/graphql.graphqlHandler environment: SLS_DEBUG: true events: - http: path: graphql method: post cors: true integration: lambda-proxy graphiql: handler: src/graphql.graphqlHandler events: - http: path: graphiql method: get integration: lambda-proxy

When i hit my API endpoint https://hu0pzhs59i.execute-api.us-east-1.amazonaws.com/dev/graphiql gives error

Most helpful comment

@jatinvmehta Ok, I think I figured out the issue. The code in this repo is deployed to:

The problem you were running into is that by default, the GraphQL playground app served by your lambda was attempting to hit /graphiql as the API endpoint. It really needed to be hitting /dev/graphql. For this reason, it makes sense we saw the 403s because the playground was attempting to make requests against a bad endpoint (/graphiql is not an endpoint, /dev/graphiql is though).

tl;dr this looks to be a bug/feature of GraphQL Playground. Anyway, hopefully your service works as currently deployed and its just a matter of updating the URL in the UI. Let me know if that doesn't work.

playground

All 15 comments

What happens if you remove the integration: lambda-proxy lines from the serverless.yml?

@kyledetella - Still get same error

I also see "Server cannot be reached" on playground on https://hu0pzhs59i.execute-api.us-east-1.amazonaws.com/dev/graphiql

@jatinvmehta I just copied your JS and your serverless.yml and deployed it with no problem (link: https://r8icox2qce.execute-api.us-east-1.amazonaws.com/dev/graphiql). I pushed it to a repo you can check out here: https://github.com/kyledetella/apollo-lambda-issue. Maybe try cloning my repo and deploying with your credentials to see if you still get the same error?

--

Edit: I deployed that previous function using a slightly different config. Once I used your exact config, it indeed began to error out for me.

@kyledetella i get forbidden on your api link ? i will try cloning your repo and see

@kyledetella - i am getting same error even after cloning your repo. Is there any special setting require for aws configure ? i do have other lambda which works fine so i don't think that's the issue

@jatinvmehta Ok, I think I figured out the issue. The code in this repo is deployed to:

The problem you were running into is that by default, the GraphQL playground app served by your lambda was attempting to hit /graphiql as the API endpoint. It really needed to be hitting /dev/graphql. For this reason, it makes sense we saw the 403s because the playground was attempting to make requests against a bad endpoint (/graphiql is not an endpoint, /dev/graphiql is though).

tl;dr this looks to be a bug/feature of GraphQL Playground. Anyway, hopefully your service works as currently deployed and its just a matter of updating the URL in the UI. Let me know if that doesn't work.

playground

@kyledetella - Aaaah.... This works now. Thank you very much for your help. Is there a way this can be fixed with future version ?

I think a good first step would be to look at the graphql-playground repo and see if changing this behavior fits with the vision of that project. It may or may not.

There may also be something inside of Apollo Server that could be done to configure this, but I can't promise that I will be looking into it.

@kyledetella - Thanks. Also we are using MongoDB as data source and couldn't find a reliable example to use it with Apollo server and serverless. Can you point me to right direction ? I will close this issue for now

@jatinvmehta I don't know of any examples off the top of my head. In general though, if you need to connect to a DB from a serverless function, I would consider the overhead of establishing the connection and make sure you don't max out your limit. Since functions are spun up and down on-demand, managing these connections (and making sure they are closed to properly exit the function) can trip some people up.

Thanks @kyledetella . I was searching for hours to solve this problem.
Imho this should be mentioned in the Deploying with AWS Lambda tutorial.

@jatinvmehta In the Apollo Server constructor you can pass some playground options, including the endpoint, but you need to work out the URL you will be deploying to ahead of time.

Thanks @kyledetella, this tripped me up too. IMO apollo-server-lambda should automagically fix this, or as @illing2005 mentioned the tutorial should mention this point.

Hi all, I ran into this same issue following along the apollo-serverless guide today. Happy I found @kyledetella's comment.

I ran into this issue - even though it said Server cannot be reached and Response not successful: Received status code 500, I decided to run a query anyways and it turns out my database was not connected:

{
  "error": {
    "errors": [
      {
        "message": "Context creation failed: connect ECONNREFUSED 127.0.0.1:5432",
        "extensions": {
          "code": "INTERNAL_SERVER_ERROR",
          "exception": {
            "stacktrace": [
              "Error: Context creation failed: connect ECONNREFUSED 127.0.0.1:5432",
              "    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1144:16)"
            ]
          }
        }
      }
    ]
  }
}

It works after I removed the DB from my apollo-server configuration (I'm going to add the DB back in later)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Magneticmagnum picture Magneticmagnum  路  3Comments

danilobuerger picture danilobuerger  路  3Comments

veeramarni picture veeramarni  路  3Comments

stevezau picture stevezau  路  3Comments

dupski picture dupski  路  3Comments