...
1 package activityhistory
2
3 import (
4 "bytes"
5 "context"
6 "strings"
7 "time"
8
9 "github.com/vektah/gqlparser/v2/formatter"
10
11 "edge-infra.dev/pkg/edge/api/graphqlhelpers"
12 "edge-infra.dev/pkg/edge/api/middleware"
13 "edge-infra.dev/pkg/lib/fog"
14
15 "github.com/99designs/gqlgen/graphql"
16 )
17
18 func (e ActivityHistory) ExtensionName() string { return "APIErrorConverter" }
19 func (e ActivityHistory) Validate(_ graphql.ExecutableSchema) error { return nil }
20
21 func (e ActivityHistory) InterceptResponse(ctx context.Context, next graphql.ResponseHandler) *graphql.Response {
22 log := fog.FromContext(ctx)
23 resp := next(ctx)
24 rctx := graphql.GetOperationContext(ctx)
25 if !graphqlhelpers.IsMutation(rctx) {
26 return resp
27 }
28 rawquery := graphqlhelpers.GetRawQuery(rctx)
29 schema, err := graphqlhelpers.ParseQuery(rawquery)
30 if err != nil {
31 return resp
32 }
33 graphqlhelpers.SanitizeDocument(schema)
34 variables := graphqlhelpers.GetVariables(rctx)
35 graphqlhelpers.UpdateQueryWithVariables(schema, variables)
36 buf := bytes.NewBuffer(nil)
37 formatter.NewFormatter(buf).FormatQueryDocument(schema)
38 opts := []Option{
39 WithUserCtx(middleware.ForContext(ctx)),
40 WithErrors(graphql.GetErrors(ctx)),
41 WithStatus(getResponseStatus(resp)),
42 WithInput(buf.String()),
43 WithTimestamp(time.Now().UTC()),
44 WithAction(strings.Join(graphqlhelpers.GetQueryNames(schema), ",")),
45 }
46 newAction := New(e.RootOrg, e.Resolver.TenantService).Map(opts...)
47 if newAction.TenantEdgeID != "" {
48 if err := e.Resolver.ActivityService.CreateAction(ctx, *newAction); err != nil {
49 log.Error(err, "unable to record activity history action")
50 }
51 }
52 return resp
53 }
54
55 func getResponseStatus(resp *graphql.Response) string {
56 const nullStr = "null"
57 switch {
58 case resp != nil && len(resp.Errors) == 0 && string(resp.Data) != nullStr:
59 return "Success"
60 default:
61 return "Failure"
62 }
63 }
64
View as plain text