Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

hono/aws-lambda + @hono/graphql-server GET not working! #2723

Open
mthmcalixto opened this issue May 19, 2024 · 12 comments · May be fixed by #2726
Open

hono/aws-lambda + @hono/graphql-server GET not working! #2723

mthmcalixto opened this issue May 19, 2024 · 12 comments · May be fixed by #2726
Labels

Comments

@mthmcalixto
Copy link

mthmcalixto commented May 19, 2024

What version of Hono are you using?

4.3.7

What runtime/platform is your app running on?

Lambda

What steps can reproduce the bug?

Any idea why the GET method doesn't work?

import rootResolver from "./resolvers/resolvers"
import { GraphQLFileLoader } from "@graphql-tools/graphql-file-loader"
import { loadSchemaSync } from "@graphql-tools/load"
import { addResolversToSchema } from "@graphql-tools/schema"
import { graphqlServer } from "@hono/graphql-server"
import { serve } from "@hono/node-server"
import { Hono } from "hono"
import { handle } from "hono/aws-lambda"
import { compress } from "hono/compress"
import { cors } from "hono/cors"
import { logger } from "hono/logger"
import { prettyJSON } from "hono/pretty-json"
import path from "path"

const app = new Hono()

app.use(logger())
app.use(cors())
app.use(compress())
app.use(prettyJSON())

const schemaPath = path.join(__dirname, "schemas/schema.graphql")

const schema = loadSchemaSync(schemaPath, {
  loaders: [new GraphQLFileLoader()],
})

const schemaWithResolvers = addResolversToSchema({
  schema,
  resolvers: rootResolver,
})

app.use(
  "/graphql",
  graphqlServer({
    schema: schemaWithResolvers,
  }),
)

if (process.env.AWS_LAMBDA_FUNCTION_NAME) {
  console.log("Running in AWS Lambda environment.")
  exports.handler = handle(app)
} else {
  console.log("Server is running on http://localhost:3000")
  serve(app)
}

What is the expected behavior?

image

What do you see instead?

image

image

Additional information

No response

@NamesMT
Copy link
Contributor

NamesMT commented May 20, 2024

I have noticed (and fixed) this issue a while back when updating my fork to align with hono's current update, you could patch the package for a workaround in the time being.
Advertising alert - or you could use my fork: hono-adapter-aws-lambda, it also comes with a triggerProcessor handle AWS trigger events like S3, EventBridge.

I'll proceed to create a PR for official Hono now.

NamesMT added a commit to NamesMT/hono that referenced this issue May 20, 2024
@NamesMT NamesMT linked a pull request May 20, 2024 that will close this issue
4 tasks
@NamesMT
Copy link
Contributor

NamesMT commented May 20, 2024

Hi @mthmcalixto, coming back to this issue, graphql is using HTTP method and should be supported by the official adapter.

For your case, it is actually because you were including a JSON body with a GET method, which is not supported by the Web Standard Request, you should put it as query parameter for GET requests.
And in your test output on AWS Lambda, I guess you were testing with a simple payload like this?:
{ "hello": true },
This is an invalid request and will throw error.

You could check the AWS doc to see the valid payload for testing.

@mthmcalixto
Copy link
Author

mthmcalixto commented May 20, 2024

@NamesMT Yes, it seems that I passed an empty graphql body and this caused an internal error.

image
image

Body empty - Lambda URL:
image

Localhost:
image

What improvements are there in https://github.com/NamesMT/hono-adapter-aws-lambda?

@NamesMT
Copy link
Contributor

NamesMT commented May 21, 2024

That's weird, is "GraphQL query" in the Body a string that you put in or is it a placeholder by the API Client?,
From my testing, when the body is NOT EMPTY for GET requests, Lambda will returns a 502, and the localhost will return a 400 with empty error, that is correct,
But if you are sure your body is empty then do the following debug steps:

  • Check your CloudWatch log to see what Error has been thrown
  • Use another API client

If after that it still happens, could you setup a repo so that I can clone and test?
For my freshly set up repo, the GraphQL works fine with an empty body (400 with message), and will works when I pass it as a query parameter for GET request, example:
local:
image
Lambda:
image

Or you know what? You could just screw, never use GET requests and use only POST requests, GraphQL also recommend POST requests for easier sending of the query payload.


For my adapter fork,
Currently, it allows unknown events to be processed by v1Processor instead of throwing at isALBEvent check and it adds a triggerProcessor to support custom invokations following the format: { "eventSource": "string" } and AWS event sources like S3, EventBridge,...

@mthmcalixto
Copy link
Author

mthmcalixto commented May 21, 2024

@NamesMT try to put the query empty and do a GET.

image

image

This should return a json error, not an internal error.

image

Here it stops you from continuing, because it returned 502:
image

@NamesMT
Copy link
Contributor

NamesMT commented May 21, 2024

Hi @mthmcalixto, this is my response if I put the query empty and do a GET (Lambda):
image

The response is a proper 400 with message, not a 502

@mthmcalixto
Copy link
Author

Hi @mthmcalixto, this is my response if I put the query empty and do a GET (Lambda): image

The response is a proper 400 with message, not a 502

Try https://httpie.io/docs/desktop

@NamesMT
Copy link
Contributor

NamesMT commented May 22, 2024

@mthmcalixto
It will return 400 with message if the body is selected as None,
If the body is selected as GraphQL, even with nothing inputted, httpie will still include the JSON body of: {"query": ""}, so this issue is basically user error / HTTP client error.

@NamesMT
Copy link
Contributor

NamesMT commented May 22, 2024

image
You could have checked it the whole time in the Request tab 🤦🏽

@mthmcalixto
Copy link
Author

mthmcalixto commented May 22, 2024

image You could have checked it the whole time in the tab 🤦🏽Request

but it shouldn't return a 502 error, but a 400 error

img

@NamesMT
Copy link
Contributor

NamesMT commented May 22, 2024

Hi,
The problem here is by web standards, HTTP GET requests cannot contains a Body, and HTTPie will includes a body of {"query": ""} even when you have inputted nothing with GraphQL mode selected, so the request is invalid and therefor will cause error on Lambda while parsing the request.

@mthmcalixto
Copy link
Author

Hi, The problem here is by web standards, HTTP GET requests cannot contains a Body, and HTTPie will includes a body of {"query": ""} even when you have inputted nothing with GraphQL mode selected, so the request is invalid and therefor will cause error on Lambda while parsing the request.

That's exactly the error I'm saying, it should return 400, not a 502.

The error is not handled.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants