From 0639e782c33dbdfb7e25b4a602d28346f5bebe29 Mon Sep 17 00:00:00 2001 From: Kofo Okesola Date: Fri, 3 Nov 2023 10:25:58 +0100 Subject: [PATCH] chaged handlers --- gateway/handler_success.go | 24 ++++-------------------- go.mod | 1 + internal/graphql/graphql_request.go | 1 + internal/graphql/graphql_request_test.go | 11 ++++++++--- 4 files changed, 14 insertions(+), 23 deletions(-) diff --git a/gateway/handler_success.go b/gateway/handler_success.go index ae7e4513c58c..123004604b09 100644 --- a/gateway/handler_success.go +++ b/gateway/handler_success.go @@ -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) { diff --git a/go.mod b/go.mod index b3496592f89b..04e4ca7d6130 100644 --- a/go.mod +++ b/go.mod @@ -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 ) diff --git a/internal/graphql/graphql_request.go b/internal/graphql/graphql_request.go index 31cbd7e79ac2..b7187aa37bf9 100644 --- a/internal/graphql/graphql_request.go +++ b/internal/graphql/graphql_request.go @@ -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 } diff --git a/internal/graphql/graphql_request_test.go b/internal/graphql/graphql_request_test.go index 6ea12acb825b..d96673e091eb 100644 --- a/internal/graphql/graphql_request_test.go +++ b/internal/graphql/graphql_request_test.go @@ -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" @@ -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{ @@ -139,6 +141,7 @@ func TestGraphStatsExtractionVisitor_ExtractStats(t *testing.T) { }, }, IsGraphQL: true, + Variables: `{"in":"hello"}`, }, }, { @@ -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) + } }) } }