Skip to content

Commit

Permalink
chaged handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
kofoworola committed Nov 3, 2023
1 parent e095f32 commit 0639e78
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 23 deletions.
24 changes: 4 additions & 20 deletions gateway/handler_success.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,30 +153,14 @@ func recordGraphDetails(rec *analytics.AnalyticsRecord, r *http.Request, resp *h
return
}
}
graphReq, err := graphql.NewRequestFromBodySchema(string(body), spec.GraphQL.Schema)

extractor := graphql.NewGraphStatsExtractor()
stats, err := extractor.ExtractStats(string(body), string(respBody), spec.GraphQL.Schema)
if err != nil {
logger.WithError(err).Error("error recording graph analytics")
return
}

rec.GraphQLStats.IsGraphQL = true
rec.GraphQLStats.Types = graphReq.TypesAndFields()
rec.GraphQLStats.OperationType = graphReq.OperationType()
rec.GraphQLStats.RootFields = graphReq.RootFields()
rec.GraphQLStats.Variables = string(graphReq.OriginalVariables)
graphErr, err := graphReq.GraphErrors(respBody)
if err != nil {
logger.WithError(err).Error("error reading graph errors")
}
if len(graphErr) > 0 {
rec.GraphQLStats.HasErrors = true
rec.GraphQLStats.Errors = make([]analytics.GraphError, len(graphErr))
for i, e := range graphErr {
rec.GraphQLStats.Errors[i] = analytics.GraphError{
Message: e,
}
}
}
rec.GraphQLStats = stats
}

func (s *SuccessHandler) RecordHit(r *http.Request, timing analytics.Latency, code int, responseCopy *http.Response) {
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ require (
require (
github.com/TykTechnologies/kin-openapi v0.90.0
github.com/TykTechnologies/opentelemetry v0.0.20
github.com/google/go-cmp v0.5.9
go.opentelemetry.io/otel v1.19.0
go.opentelemetry.io/otel/trace v1.19.0
)
Expand Down
1 change: 1 addition & 0 deletions internal/graphql/graphql_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ func (g *GraphStatsExtractionVisitor) ExtractStats(rawRequest, response, schema
})
}
stats.HasErrors = len(stats.Errors) > 0
stats.Variables = string(g.gqlRequest.Variables)
return stats, nil
}

Expand Down
11 changes: 8 additions & 3 deletions internal/graphql/graphql_request_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package graphql

import (
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -123,7 +125,7 @@ func TestGraphStatsExtractionVisitor_ExtractStats(t *testing.T) {
{
name: "should successfully parse",
schema: validSchema,
request: `{"query":"query{\n characters(filter: {\n \n }){\n info{\n count\n }\n }\n}"}`,
request: `{"query":"query{\n characters(filter: {\n \n }){\n info{\n count\n }\n }\n}","variables":{"in":"hello"}}`,
response: `{"errors":[{"message":"Name for character with ID 1002 could not be fetched.","locations":[{"line":6,"column":7}],"path":["hero","heroFriends",1,"name"]}]}`,
expected: analytics.GraphQLStats{
Types: map[string][]string{
Expand All @@ -139,6 +141,7 @@ func TestGraphStatsExtractionVisitor_ExtractStats(t *testing.T) {
},
},
IsGraphQL: true,
Variables: `{"in":"hello"}`,
},
},
{
Expand Down Expand Up @@ -211,8 +214,10 @@ func TestGraphStatsExtractionVisitor_ExtractStats(t *testing.T) {
return
}
require.NoError(t, err)
assert.Equal(t, test.expected, stats)

assert.True(t, stats.IsGraphQL)
if diff := cmp.Diff(test.expected, stats, cmpopts.SortSlices(func(a, b string) bool { return a < b })); diff != "" {
t.Fatal(diff)
}
})
}
}
Expand Down

0 comments on commit 0639e78

Please sign in to comment.