aboutsummaryrefslogtreecommitdiffstats
path: root/api/graphql/tracer.go
diff options
context:
space:
mode:
authorMichael Muré <batolettre@gmail.com>2022-08-23 15:02:41 +0200
committerMichael Muré <batolettre@gmail.com>2022-08-23 15:09:31 +0200
commit8d11e620a3d663cf21a62910d0f3961a8aff4be1 (patch)
tree17dc3df82229eea5c3500e2bd8448060dc1ae849 /api/graphql/tracer.go
parent5a70e8b3a2e0fe3d1a1dcd4c24bb6bf64633cb7f (diff)
downloadgit-bug-8d11e620a3d663cf21a62910d0f3961a8aff4be1.tar.gz
webui: add a flag to log handling errors
Diffstat (limited to 'api/graphql/tracer.go')
-rw-r--r--api/graphql/tracer.go67
1 files changed, 67 insertions, 0 deletions
diff --git a/api/graphql/tracer.go b/api/graphql/tracer.go
new file mode 100644
index 00000000..11448a3a
--- /dev/null
+++ b/api/graphql/tracer.go
@@ -0,0 +1,67 @@
+package graphql
+
+import (
+ "context"
+ "encoding/json"
+ "fmt"
+ "io"
+ "strings"
+
+ "github.com/99designs/gqlgen/graphql"
+
+ "github.com/MichaelMure/git-bug/util/colors"
+)
+
+// adapted from https://github.com/99designs/gqlgen/blob/master/graphql/handler/debug/tracer.go
+
+type Tracer struct {
+ Out io.Writer
+}
+
+var _ interface {
+ graphql.HandlerExtension
+ graphql.ResponseInterceptor
+} = &Tracer{}
+
+func (a Tracer) ExtensionName() string {
+ return "error tracer"
+}
+
+func (a *Tracer) Validate(schema graphql.ExecutableSchema) error {
+ return nil
+}
+
+func stringify(value interface{}) string {
+ valueJson, err := json.MarshalIndent(value, " ", " ")
+ if err == nil {
+ return string(valueJson)
+ }
+
+ return fmt.Sprint(value)
+}
+
+func (a Tracer) InterceptResponse(ctx context.Context, next graphql.ResponseHandler) *graphql.Response {
+ resp := next(ctx)
+
+ if len(resp.Errors) == 0 {
+ return resp
+ }
+
+ rctx := graphql.GetOperationContext(ctx)
+
+ _, _ = fmt.Fprintln(a.Out, "GraphQL Request {")
+ for _, line := range strings.Split(rctx.RawQuery, "\n") {
+ _, _ = fmt.Fprintln(a.Out, " ", colors.Cyan(line))
+ }
+ for name, value := range rctx.Variables {
+ _, _ = fmt.Fprintf(a.Out, " var %s = %s\n", name, colors.Yellow(stringify(value)))
+ }
+
+ _, _ = fmt.Fprintln(a.Out, " resp:", colors.Green(stringify(resp)))
+ for _, err := range resp.Errors {
+ _, _ = fmt.Fprintln(a.Out, " error:", colors.Bold(err.Path.String()+":"), colors.Red(err.Message))
+ }
+ _, _ = fmt.Fprintln(a.Out, "}")
+ _, _ = fmt.Fprintln(a.Out)
+ return resp
+}