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

Micro CORS sandbox "Cannot set headers after they are sent to the client" #5513

Closed
RamonDonnell opened this issue Jul 20, 2021 · 3 comments · Fixed by #5516
Closed

Micro CORS sandbox "Cannot set headers after they are sent to the client" #5513

RamonDonnell opened this issue Jul 20, 2021 · 3 comments · Fixed by #5516

Comments

@RamonDonnell
Copy link

Trying upgrade AS2 to AS3 using vercel serverless functions and apollo-server-micro. Sandbox doesn't connect to localhost server.

To Reproduce:

  • Following the CORS Example or clone
  • npm i; npm start
  • Browse to locahost:3000
  • click "query your server"
  • Sandox alert (expected sandbox to connect and work)
//sandbox alert
Network error: unable to reach server
We couldn't reach your server.

    The server's response may be missing CORS headers.

    To verify, please open Developer Tools and check the console for any CORS related error messages, like “Cross Origin Request Blocked”.

    If that is the case, contact your system administrator and add https://studio.apollographql.com to your server's allow-list.

    Learn more about enabling CORS 

The server may be not reachable at the provided endpoint. Please double check your configured endpoint URL for typos.Check the endpoint URL
Is the server currently running?


//server log
micro: Accepting connections on port 3000
(node:43266) UnhandledPromiseRejectionWarning: Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
    at ServerResponse.setHeader (_http_outgoing.js:558:11)
    at send (/Users/ramondonnell/devel/source/learn/graphql/apolloserver3/node_modules/micro/lib/index.js:76:6)
    at sendError (/Users/ramondonnell/devel/source/learn/graphql/apolloserver3/node_modules/micro/lib/index.js:83:2)
    at /Users/ramondonnell/devel/source/learn/graphql/apolloserver3/node_modules/micro/lib/index.js:110:17
    at processTicksAndRejections (internal/process/task_queues.js:93:5)
(Use `node --trace-warnings ...` to show where the warning was created)
(node:43266) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:43266) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

Note local curl query works

curl --request POST --header 'content-type: application/json' --url http://localhost:3000/graphql --data '{"query":"query { __typename }"}'
@glasser glasser added this to the MM-2021-07 milestone Jul 20, 2021
@glasser
Copy link
Member

glasser commented Jul 20, 2021

Can reproduce by replacing POST with OPTIONS in your URL line.

@glasser
Copy link
Member

glasser commented Jul 20, 2021

Looks like this is just how micro-cors works; the README should suggest actually sending a response instead of res.end(), I guess. Will fix.

Note that even after this is fixed, there's something a bit confusing in how the default graphql path is /graphql but (unlike all the other web server integrations) apollo-server-micro ignores the path for deciding whether to serve the landing page... but then the landing page lets you go to a sandbox that won't actually work because it's not on the right path. Should clean that up.

glasser added a commit that referenced this issue Jul 20, 2021
ie, by default, serve 404 on root.

This makes it consistent with the other framework integrations. This
also means that if you're using the playground or local default landing
page, you won't manage to load a UI at an URL that doesn't also serve
GraphQL and then get confused when it can't talk to your server.

Also fixes #5513, a doc issue around CORS.
glasser added a commit that referenced this issue Jul 20, 2021
ie, by default, serve 404 on root.

This makes it consistent with the other framework integrations. This
also means that if you're using the playground or local default landing
page, you won't manage to load a UI at an URL that doesn't also serve
GraphQL and then get confused when it can't talk to your server.

Also fixes #5513, a doc issue around CORS.
glasser added a commit that referenced this issue Jul 20, 2021
ie, by default, serve 404 on root.

This makes it consistent with the other framework integrations. This
also means that if you're using the playground or local default landing
page, you won't manage to load a UI at an URL that doesn't also serve
GraphQL and then get confused when it can't talk to your server.

Also fixes #5513, a doc issue around CORS.
@RamonDonnell
Copy link
Author

Can reproduce by replacing POST with OPTIONS in your URL line.

Yes, reproduces the same error.

curl --request OPTIONS --url http://localhost:3000/graphql

@hwillson hwillson removed this from the MM-2021-07 milestone Jul 29, 2021
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 20, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants