aboutsummaryrefslogtreecommitdiffstats
path: root/api/graphql
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
parent5a70e8b3a2e0fe3d1a1dcd4c24bb6bf64633cb7f (diff)
downloadgit-bug-8d11e620a3d663cf21a62910d0f3961a8aff4be1.tar.gz
webui: add a flag to log handling errors
Diffstat (limited to 'api/graphql')
-rw-r--r--api/graphql/graphql_test.go2
-rw-r--r--api/graphql/handler.go6
-rw-r--r--api/graphql/tracer.go67
3 files changed, 73 insertions, 2 deletions
diff --git a/api/graphql/graphql_test.go b/api/graphql/graphql_test.go
index 350e489a..2ddfb314 100644
--- a/api/graphql/graphql_test.go
+++ b/api/graphql/graphql_test.go
@@ -22,7 +22,7 @@ func TestQueries(t *testing.T) {
_, err := mrc.RegisterDefaultRepository(repo)
require.NoError(t, err)
- handler := NewHandler(mrc)
+ handler := NewHandler(mrc, nil)
c := client.New(handler)
diff --git a/api/graphql/handler.go b/api/graphql/handler.go
index 00141f01..1d30bf72 100644
--- a/api/graphql/handler.go
+++ b/api/graphql/handler.go
@@ -20,11 +20,15 @@ type Handler struct {
io.Closer
}
-func NewHandler(mrc *cache.MultiRepoCache) Handler {
+func NewHandler(mrc *cache.MultiRepoCache, errorOut io.Writer) Handler {
rootResolver := resolvers.NewRootResolver(mrc)
config := graph.Config{Resolvers: rootResolver}
h := handler.NewDefaultServer(graph.NewExecutableSchema(config))
+ if errorOut != nil {
+ h.Use(&Tracer{Out: errorOut})
+ }
+
return Handler{
Handler: h,
Closer: rootResolver,
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
+}