...
1 package errorlog
2
3 import (
4 "bytes"
5 "context"
6 "time"
7
8 "edge-infra.dev/pkg/edge/api/graphqlhelpers"
9 "edge-infra.dev/pkg/edge/api/middleware"
10 "edge-infra.dev/pkg/edge/api/middleware/request"
11 "edge-infra.dev/pkg/lib/fog"
12
13 "github.com/99designs/gqlgen/graphql"
14 "github.com/vektah/gqlparser/v2/formatter"
15 )
16
17 const (
18 correlationIDKey = "correlation_id"
19 )
20
21 type GraphqlErrorLogger struct{}
22
23 func (g GraphqlErrorLogger) ExtensionName() string { return "GraphqlErrorLogging" }
24 func (g GraphqlErrorLogger) Validate(_ graphql.ExecutableSchema) error { return nil }
25 func (g GraphqlErrorLogger) InterceptResponse(ctx context.Context, next graphql.ResponseHandler) *graphql.Response {
26 startTime := time.Now()
27 resp := next(ctx)
28 rctx := graphql.GetOperationContext(ctx)
29 middleware.HandleGraphQlMetrics(rctx, resp, startTime)
30 switch {
31 case len(resp.Errors) > 0:
32 correlationID := request.FromContext(ctx, request.CorrelationIDCtxKey{})
33 log := fog.FromContext(ctx)
34 rawquery := graphqlhelpers.GetRawQuery(rctx)
35 schema, err := graphqlhelpers.ParseQuery(rawquery)
36 if err != nil {
37 return resp
38 }
39 graphqlhelpers.SanitizeDocument(schema)
40 variables := graphqlhelpers.GetVariables(rctx)
41 graphqlhelpers.UpdateQueryWithVariables(schema, variables)
42 buf := bytes.NewBuffer(nil)
43 formatter.NewFormatter(buf).FormatQueryDocument(schema)
44 opts := []Option{
45 WithCorrelationID(correlationID),
46 WithInput(buf.String()),
47 WithErrors(resp.Errors),
48 }
49 NewProvider().Build(log, opts...)
50 }
51 return resp
52 }
53
View as plain text