Skip to content

Commit

Permalink
Merge pull request #263 from movio/add-step-debugging-info
Browse files Browse the repository at this point in the history
Inject execution debugging info into query plan
  • Loading branch information
Lucian Jones authored Apr 23, 2024
2 parents 8d2cb4f + af1331f commit b60dd36
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 16 deletions.
29 changes: 23 additions & 6 deletions execution.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"strings"
"sync"
"sync/atomic"
"time"

"github.com/vektah/gqlparser/v2/ast"
"github.com/vektah/gqlparser/v2/gqlerror"
Expand Down Expand Up @@ -56,10 +57,16 @@ func (q *queryExecution) Execute(queryPlan *QueryPlan) ([]executionResult, gqler

for _, step := range queryPlan.RootSteps {
if step.ServiceURL == internalServiceName {
reqStart := time.Now()
r, err := executeBrambleStep(step)
if err != nil {
return nil, q.createGQLErrors(step, err)
}
step.executionResult = &executionStepResult{
executed: true,
error: err,
timeTaken: time.Since(reqStart),
}
results = append(results, *r)
continue
}
Expand Down Expand Up @@ -92,6 +99,7 @@ func (q *queryExecution) Execute(queryPlan *QueryPlan) ([]executionResult, gqler

func (q *queryExecution) executeRootStep(step *QueryPlanStep) error {
var document string
reqStart := time.Now()

var variables map[string]interface{}
switch step.ParentType {
Expand All @@ -109,13 +117,17 @@ func (q *queryExecution) executeRootStep(step *QueryPlanStep) error {

var data map[string]interface{}
err := q.graphqlClient.Request(q.ctx, step.ServiceURL, req, &data)
q.writeExecutionResult(step, data, err)
step.executionResult = &executionStepResult{
executed: true,
error: err,
timeTaken: time.Since(reqStart),
}

if err != nil {
q.writeExecutionResult(step, data, err)
return nil
}

q.writeExecutionResult(step, data, nil)

for _, childStep := range step.Then {
boundaryIDs, err := extractAndDedupeBoundaryIDs(data, childStep.InsertionPoint, childStep.ParentType)
if err != nil {
Expand Down Expand Up @@ -151,6 +163,7 @@ func (q *queryExecution) executeChildStep(step *QueryPlanStep, boundaryIDs []str
if newRequestCount > q.maxRequest {
return fmt.Errorf("exceeded max requests of %v", q.maxRequest)
}
reqStart := time.Now()

boundaryField, err := q.boundaryFields.Field(step.ServiceURL, step.ParentType)
if err != nil {
Expand All @@ -163,13 +176,17 @@ func (q *queryExecution) executeChildStep(step *QueryPlanStep, boundaryIDs []str
}

data, err := q.executeBoundaryQuery(documents, step.ServiceURL, variables, boundaryField)
q.writeExecutionResult(step, data, err)
step.executionResult = &executionStepResult{
executed: true,
error: err,
timeTaken: time.Since(reqStart),
}

if err != nil {
q.writeExecutionResult(step, data, err)
return nil
}

q.writeExecutionResult(step, data, nil)

nonNilBoundaryResults := extractNonNilBoundaryResults(data)

if len(nonNilBoundaryResults) > 0 {
Expand Down
43 changes: 33 additions & 10 deletions plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/json"
"fmt"
"strings"
"time"

"github.com/99designs/gqlgen/graphql"
"github.com/vektah/gqlparser/v2/ast"
Expand All @@ -19,6 +20,26 @@ type QueryPlanStep struct {
SelectionSet ast.SelectionSet
InsertionPoint []string
Then []*QueryPlanStep

executionResult *executionStepResult
}

type executionStepResult struct {
executed bool
error error
timeTaken time.Duration
}

func (e *executionStepResult) MarshalJSON() ([]byte, error) {
return json.Marshal(&struct {
Executed bool
Error error `json:",omitempty"`
TimeTaken string
}{
Executed: e.executed,
TimeTaken: e.timeTaken.String(),
Error: e.error,
})
}

// MarshalJSON marshals the step the JSON
Expand All @@ -27,17 +48,19 @@ func (s *QueryPlanStep) MarshalJSON() ([]byte, error) {
Variables: map[string]interface{}{},
})
return json.Marshal(&struct {
ServiceURL string
ParentType string
SelectionSet string
InsertionPoint []string
Then []*QueryPlanStep
ServiceURL string
ParentType string
SelectionSet string
InsertionPoint []string
ExecutionStepResult *executionStepResult `json:",omitempty"`
Then []*QueryPlanStep
}{
ServiceURL: s.ServiceURL,
ParentType: s.ParentType,
SelectionSet: formatSelectionSetSingleLine(ctx, nil, s.SelectionSet),
InsertionPoint: s.InsertionPoint,
Then: s.Then,
ServiceURL: s.ServiceURL,
ParentType: s.ParentType,
SelectionSet: formatSelectionSetSingleLine(ctx, nil, s.SelectionSet),
InsertionPoint: s.InsertionPoint,
Then: s.Then,
ExecutionStepResult: s.executionResult,
})
}

Expand Down

0 comments on commit b60dd36

Please sign in to comment.