This repo is an example of how to add error types (a.k.a error codes) to your gqlgen GraphQL server for Go.
The project demonstrates two features:
- Intentional errors are returned with an error type, used by the client code to identify the error and behave accordingly.
- Unexpected errors in the server are masked and replaced with "Internal Server Error". This prevents leak of sensitive information inside error messages.
For a further explanation, check out my blog post: Who Moved My Error Codes? Adding error types to your GoLang GraphQL Server
Run the project to see how it works, then have a look at the implementation to understand how it uses error types for its functionality.
Then, try disabling the error type and error masking features by editing the server/main.go
file and comment out the srv.SetErrorPresenter(GqlErrorPresenter)
line, and rerun the server. Repeat the demo to see how it is affected.
Run the server:
cd server
go run .
Then run the server by running the client:
cd client
go run .
You can also work against the server using the GraphQL playground at http://localhost:8080/playground
- The GraphQL schema is in the
graphql/
dir. This is the only thing that is shared between the client and the server. - Files that are named
*_gen.go
orgenerated.go
are auto-generated. You can see the command that generated them ingenerate.go