Skip to content

Commit

Permalink
feat(agent): adding graphql introspection query (#3989)
Browse files Browse the repository at this point in the history
* feat(agent): adding graphql introspection query

* feat(agent): Adding graphql trigger
  • Loading branch information
xoscar committed Aug 28, 2024
1 parent 0a57188 commit 3af9524
Show file tree
Hide file tree
Showing 15 changed files with 1,424 additions and 957 deletions.
1,947 changes: 1,020 additions & 927 deletions agent/proto/orchestrator.pb.go

Large diffs are not rendered by default.

8 changes: 7 additions & 1 deletion agent/proto/orchestrator.proto
Original file line number Diff line number Diff line change
Expand Up @@ -128,13 +128,19 @@ message HttpRequest {

message GraphqlRequest {
string url = 1;
string body = 2;
GraphqlBody body = 2;
repeated HttpHeader headers = 3;
HttpAuthentication authentication = 4;
bool SSLVerification = 5;
string schema = 6;
}

message GraphqlBody {
string query = 1;
map <string, string> variables = 2;
string operationName = 3;
}

message HttpHeader {
string key = 1;
string value = 2;
Expand Down
31 changes: 16 additions & 15 deletions agent/workers/graphql_introspect.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package workers

import (
"context"
"encoding/json"
"strings"

"github.com/kubeshop/tracetest/agent/client"
Expand Down Expand Up @@ -56,6 +55,9 @@ func NewGraphqlIntrospectWorker(client *client.Client, opts ...GraphqlIntrospect
meter: telemetry.GetNoopMeter(),
}

http := trigger.HTTP()
worker.graphqlTrigger = trigger.GRAPHQL(http)

for _, opt := range opts {
opt(worker)
}
Expand All @@ -77,7 +79,7 @@ func (w *GraphqlIntrospectWorker) Introspect(ctx context.Context, r *proto.Graph
}

w.logger.Debug("Sending graphql introspection result", zap.Any("response", response))
err = w.client.SendGraphqlIntrospectionResult(ctx, mapGraphqlToProtoResponse(response.Result.Graphql))
err = w.client.SendGraphqlIntrospectionResult(ctx, mapGraphqlToProtoResponse(r, response.Result.Graphql))
if err != nil {
w.logger.Error("Could not send graphql introspection result", zap.Error(err))
span.RecordError(err)
Expand All @@ -97,44 +99,43 @@ func mapProtoToGraphqlRequest(r *proto.GraphqlIntrospectRequest) trigger.Trigger
})
}

headers = append(headers, trigger.HTTPHeader{
Value: "application/json",
Key: "Content-Type",
})

request := &trigger.GraphqlRequest{
URL: r.Graphql.Url,
SSLVerification: r.Graphql.SSLVerification,
Headers: headers,
}

body := GraphQLBody{
request.Body = trigger.GraphqlBody{
Query: IntrospectionQuery,
}

json, _ := json.Marshal(body)
request.Body = string(json)

return trigger.Trigger{
Type: trigger.TriggerTypeGraphql,
Graphql: request,
}
}

type GraphQLBody struct {
Query string `json:"query"`
}

func mapGraphqlToProtoResponse(r *trigger.GraphqlResponse) *proto.GraphqlIntrospectResponse {
func mapGraphqlToProtoResponse(r *proto.GraphqlIntrospectRequest, resp *trigger.GraphqlResponse) *proto.GraphqlIntrospectResponse {
headers := make([]*proto.HttpHeader, 0)
for _, header := range r.Headers {
for _, header := range resp.Headers {
headers = append(headers, &proto.HttpHeader{
Key: header.Key,
Value: header.Value,
})
}

return &proto.GraphqlIntrospectResponse{
RequestID: r.RequestID,
Response: &proto.HttpResponse{
StatusCode: int32(r.StatusCode),
Status: r.Status,
StatusCode: int32(resp.StatusCode),
Status: resp.Status,
Headers: headers,
Body: []byte(r.Body),
Body: []byte(resp.Body),
},
}
}
Expand Down
10 changes: 7 additions & 3 deletions agent/workers/trigger.go
Original file line number Diff line number Diff line change
Expand Up @@ -335,9 +335,13 @@ func convertProtoGraphqlTriggerToGraphqlTrigger(graphqlRequest *proto.GraphqlReq
}

return &trigger.GraphqlRequest{
URL: graphqlRequest.Url,
Headers: headers,
Body: graphqlRequest.Body,
URL: graphqlRequest.Url,
Headers: headers,
Body: trigger.GraphqlBody{
Query: graphqlRequest.Body.Query,
Variables: graphqlRequest.Body.Variables,
OperationName: graphqlRequest.Body.OperationName,
},
SSLVerification: graphqlRequest.SSLVerification,
}
}
Expand Down
16 changes: 14 additions & 2 deletions agent/workers/trigger/graphql.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package trigger

import (
"context"
"encoding/json"
"fmt"
)

Expand Down Expand Up @@ -41,12 +42,17 @@ func (t *graphqlTriggerer) Type() TriggerType {
const TriggerTypeGraphql TriggerType = "graphql"

func mapGraphqlToHttp(triggerConfig Trigger) Trigger {
json, err := json.Marshal(triggerConfig.Graphql.Body)
if err != nil {
return Trigger{}
}

return Trigger{
Type: TriggerTypeHTTP,
HTTP: &HTTPRequest{
URL: triggerConfig.Graphql.URL,
Method: HTTPMethodPOST,
Body: triggerConfig.Graphql.Body,
Body: string(json),
Headers: triggerConfig.Graphql.Headers,
SSLVerification: triggerConfig.Graphql.SSLVerification,
},
Expand All @@ -72,7 +78,7 @@ func mapHttpToGraphql(response Response) Response {

type GraphqlRequest struct {
URL string `expr_enabled:"true" json:"url,omitempty"`
Body string `expr_enabled:"true" json:"body,omitempty"`
Body GraphqlBody `expr_enabled:"true" json:"body,omitempty"`
Headers []HTTPHeader `json:"headers,omitempty"`
Auth *HTTPAuthenticator `json:"auth,omitempty"`
SSLVerification bool `json:"sslVerification,omitempty"`
Expand All @@ -85,3 +91,9 @@ type GraphqlResponse struct {
Headers []HTTPHeader `json:"headers,omitempty"`
Body string `json:"body,omitempty"`
}

type GraphqlBody struct {
Query string `json:"query,omitempty"`
Variables map[string]string `json:"variables,omitempty"`
OperationName string `json:"operationName,omitempty"`
}
6 changes: 4 additions & 2 deletions agent/workers/trigger/graphql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func TestGraphqlTrigger(t *testing.T) {

b, err := io.ReadAll(req.Body)
assert.NoError(t, err)
assert.Equal(t, `query { films { name } }`, string(b))
assert.Equal(t, `{"query":"query { films { name } }"}`, string(b))

rw.Header().Add("Content-Type", "application/json")
rw.WriteHeader(200)
Expand All @@ -42,7 +42,9 @@ func TestGraphqlTrigger(t *testing.T) {
Headers: []trigger.HTTPHeader{
{Key: "Key1", Value: "Value1"},
},
Body: `query { films { name } }`,
Body: trigger.GraphqlBody{
Query: `query { films { name } }`,
},
},
}

Expand Down
14 changes: 13 additions & 1 deletion api/graphql.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ components:
auth:
$ref: "./http.yaml#/components/schemas/HTTPAuth"
body:
type: string
$ref: "#/components/schemas/GraphqlBody"
sslVerification:
type: boolean
default: false
Expand All @@ -33,3 +33,15 @@ components:
$ref: "./http.yaml#/components/schemas/HTTPHeader"
body:
type: string

GraphqlBody:
type: object
properties:
query:
type: string
variables:
type: object
additionalProperties:
type: string
operationName:
type: string
Loading

0 comments on commit 3af9524

Please sign in to comment.