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

How to add custom error code like 404, 401, 400 with gqlgen + gin? #2653

Open
karanbangia opened this issue May 29, 2023 · 3 comments
Open

Comments

@karanbangia
Copy link

What happened?

Current resolver do not have gin context that means and method only returns err

What did you expect?

Want to have custom error code like ctx.JSON(http.StatusNotFound, responseBody) but gqlgen only handles it by returning array of error, is there any way to solve this?

@UnAfraid
Copy link
Contributor

In GraphQL there is no concept of status codes, all the requests return either 200, or 422 in case of syntax error

@ianling
Copy link
Contributor

ianling commented Aug 16, 2023

GraphQL is sort of protocol-agnostic, so you are correct that it does not provide guidelines for HTTP status codes, but that doesn't mean this implementation of GraphQL over HTTP should not support status codes other than 200 or 422.

@UnAfraid
Copy link
Contributor

http status codes in GraphQL are usually used when the actual grapql layer is unable to respond due to an error like authentication, internal server errors, bad gateway, etc.., but when are inside a resolver, i don't see a reason to respond with particular http status code, if you want to add additional context to an error you can always extend the errors with extensions as mentioned here https://gqlgen.com/reference/errors/

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

No branches or pull requests

3 participants