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

Send status 200 as soon as query parse is valid #3226

Open
wmertens opened this issue Apr 8, 2024 · 5 comments
Open

Send status 200 as soon as query parse is valid #3226

wmertens opened this issue Apr 8, 2024 · 5 comments
Labels
kind/discussion Discussion, question or feedback

Comments

@wmertens
Copy link

wmertens commented Apr 8, 2024

Is your feature request related to a problem? Please describe.

Our nginx proxy automatically sends 502 if the server takes too long to respond. This is a good feature IMHO.

However, if a mutation takes a long time to process, that means the caller will get 502 instead of the result.

Describe the solution you'd like

As soon as the query is parsed and deemed valid, before running the resolvers, yoga should already respond with 200.

Describe alternatives you've considered

Complicated async setups with subscriptions.

@ardatan
Copy link
Collaborator

ardatan commented Apr 8, 2024

Hmm interesting use case, but if we do that, this won't be valid anymore for status codes;
https://the-guild.dev/graphql/yoga-server/docs/features/error-masking#modifying-http-status-codes-and-headers
Some users want to return different status codes for auth if the query is restricted etc.
However you can use hooks to set your status code earlier than Yoga starts to feed the response stream on your own;
https://the-guild.dev/graphql/yoga-server/docs/features/envelop-plugins

@wmertens
Copy link
Author

wmertens commented Apr 8, 2024

So it would be after onValidate, but how can I tell yoga to send the 200 but leave the connection open, and won't that interfere with the other plugins?

@ardatan
Copy link
Collaborator

ardatan commented Apr 8, 2024

Due to the current design of Yoga, it is not possible with the Fetch based API we currently provide but depending on what you use as a server underneath you can manipulate the original response object available in the context. For example, it is "context.res" in vanilla Node.js

@enisdenjo
Copy link
Collaborator

As per GraphQL over HTTP spec, the response is to be sent when the operation execution is done. Yoga is following the spec and will stay compliant.

There is a few things you can do:

  1. Optimize mutations (recommended)
  2. Detect mutations in nginx and allow only those to take longer
  3. Increase timeout in nginx
  4. Custom Yoga plugin for early responses

If you still decide to go with 4. - you must make sure to handle the following:

  1. What should the response body look like? (It's not a GQL response anymore)
  2. What happens if the mutation execution fails (but is parsable and valid), how do you notify the client?
  3. How do you make sure the mutation does not hang forever

@enisdenjo enisdenjo added the kind/discussion Discussion, question or feedback label Apr 8, 2024
@n1ru4l
Copy link
Collaborator

n1ru4l commented Apr 29, 2024

A possible solution could be using a different transport, e.g. GraphQL over SSE, as it also sends pings. But I also noticed that in Yoga for GraphQL over SSE, the status code is blocked by the initial execution result.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/discussion Discussion, question or feedback
Projects
None yet
Development

No branches or pull requests

4 participants