dougmoscrop / serverless-http

Use your existing middleware framework (e.g. Express, Koa) in AWS Lambda 🎉
Other
1.72k stars 166 forks source link

Incompatibility with express #140

Open cefn opened 4 years ago

cefn commented 4 years ago

Hi and thanks for your work on serverless-http!

I have found some kind of variance which breaks the GraphiQL express service when hosted through express-graphql. The same code which works in express and in aws-serverless-express breaks in serverless-http.

I have found the problem hard to trace so instead I made a 'gist' repo dedicated to recreating the issue. It hosts defines a super-simple GraphQL schema (with two items of hard-coded data) and spins up a minimal GraphQL and GraphiQL express endpoint. You can see the repo and instructions to recreate the problem at https://github.com/cefn/serverless-graphql-gist.

The aws-serverless-express handler factory is at https://github.com/cefn/serverless-graphql-gist/blob/master/hostAwsServerlessExpress.js and works fine. GraphQL queries can be authored and handled by the endpoint.

For example if you enter...

{
  user(id: "a") {
    name
  }
}

The endpoint responds with...

{
  "data": {
    "user": {
      "name": "alice"
    }
  }
}

The corresponding serverless-http handler factory is at https://github.com/cefn/serverless-graphql-gist/blob/master/hostServerlessHttp.js

The serverless-http version looks good initially (the Lambda can load the GraphiQL interface). However, no queries can be successfully sent. The result is always the following, as if there's some problem handling the HTTP method or somehow the routing, parameter-handling or request deserialisation is wrong...

{
  "errors": [
    {
      "message": "Must provide query string."
    }
  ]
}

I hope it helps to record the issue in this way. For now, I will be using aws-serverless-express since it works for me, but I didn't want to cut and run without recording the issue.

avin-kavish commented 4 years ago

Can you provide more details on this? Initially it looks like you are trying to use GET requests with graphql but not providing a query string. If that's not what you are doing, a log of the http requests that fail could help identify the issue.

As an aside, you should use POST requests with graphql as GET is limited.

jgcmarins commented 4 years ago

facing same issue with GraphQL: BadRequestError: Must provide query string.

cefn commented 4 years ago

@avin-kavish I'm not doing anything explicitly. Just running GraphiQL. It's a graphical data endpoint tool.

In express and aws-serverless-express (on Lambda) the tool works (it can send the query above and resolve it to data). In serverless-http it doesn't work. I have not got into the guts of what methods are used, and was finding it hard to trace, but the only change between the three configurations is the hosting environment...

https://github.com/cefn/serverless-graphql-gist/blob/master/hostExpress.js https://github.com/cefn/serverless-graphql-gist/blob/master/hostAwsServerlessExpress.js https://github.com/cefn/serverless-graphql-gist/blob/master/hostServerlessHttp.js

...hence the claim of incompatibility.

jgcmarins commented 4 years ago

@cefn I am receiving a 404 when accessing your links

Screen Shot 2020-02-27 at 18 15 38
avin-kavish commented 4 years ago

The two frameworks are implemented differently. aws-serverless-express opens a socket and essentially proxies requests to a http server. This one directly transforms the lambda event into an express compatible format. It could be a transformation bug causing it. Can't you provide a screenshot/dump of chrome dev tools with the failing request?

cefn commented 4 years ago

The two frameworks are implemented differently. aws-serverless-express opens a socket and essentially proxies requests to a http server. This one directly transforms the lambda event into an express compatible format. It could be a transformation bug causing it. Can't you provide a screenshot/dump of chrome dev tools with the failing request?

@avin-kavish see below, with request/response headers and JS stack expanded...

Screenshot 2020-02-28 at 11 40 07 Screenshot 2020-02-28 at 11 40 12

avin-kavish commented 4 years ago

@cefn Hmm...there is a question mark at the end of your request url. i.e an empty query string. Try without it.

Also, we still can't see your code. Is it a private repo?

cefn commented 4 years ago

I just saw that it must have defaulted to private. Of course that defeats pretty much the whole purpose of me making the repository in the first place. It's now public.

cefn commented 4 years ago

I tried intercepting the fetch in a debugger and changing the 'fetchURL' which was previously '?' to be '' (empty string) instead. I believe I am intercepting it at this line...

https://github.com/graphql/express-graphql/blob/master/src/renderGraphiQL.js#L136

When running in aws-serverless-express both styles of fetch call worked. When running in serverless-http both styles of fetch call failed.

dougmoscrop commented 4 years ago

Thanks for the Gist, I was able to reproduce it, but it was late and I did not finish debugging it. I will try to get to this the next time I have cycles for open-source stuff!

Streeterxs commented 3 years ago

Having the same issue

manoj-rajendran commented 3 years ago

Having the same issue