Skip to content

Commit

Permalink
fix: crash when requesting trace info without initializing it (#704)
Browse files Browse the repository at this point in the history
If the trace info was not initialized (e.g. we want to always print it
if it's available, but it is not always available), we might get into
this scenario.
  • Loading branch information
fiam authored Dec 18, 2023
1 parent a36c292 commit fca783c
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
4 changes: 3 additions & 1 deletion v2/pkg/engine/resolve/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,5 +138,7 @@ func SetPlannerStats(ctx context.Context, stats PlannerStats) {
}

func GetTraceInfo(ctx context.Context) *TraceInfo {
return ctx.Value(traceStartKey{}).(*TraceInfo)
// The context might not have trace info, in that case we return nil
info, _ := ctx.Value(traceStartKey{}).(*TraceInfo)
return info
}
27 changes: 27 additions & 0 deletions v2/pkg/engine/resolve/resolvable_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -469,6 +469,33 @@ func BenchmarkResolvable_ResolveWithErrorBubbleUp(b *testing.B) {
}
}

func TestResolvable_WithTracingNotStarted(t *testing.T) {
res := NewResolvable()
// Do not start a trace with SetTraceStart(), but request it to be output
ctx := NewContext(context.Background())
ctx.RequestTracingOptions.IncludeTraceOutputInResponseExtensions = true
err := res.Init(ctx, []byte(`{"hello": "world"}`), ast.OperationTypeQuery)
assert.NoError(t, err)
object := &Object{
Fields: []*Field{
{
Name: []byte("hello"),
Value: &String{
Path: []string{"hello"},
},
},
},
}
out := &bytes.Buffer{}
err = res.Resolve(ctx.ctx, object, out)

assert.NoError(t, err)
assert.JSONEq(t, `{
"data": {"hello":"world"},
"extensions":{"trace":{"node_type":"object","nullable":true,"fields":[{"name":"hello","value":{"node_type":"string","path":["hello"]}}]}}
}`, out.String())
}

func TestResolvable_WithTracing(t *testing.T) {
topProducts := `{"topProducts":[{"name":"Table","__typename":"Product","upc":"1","reviews":[{"body":"Love Table!","author":{"__typename":"User","id":"1","name":"user-1"}},{"body":"Prefer other Table.","author":{"__typename":"User","id":"2","name":"user-2"}}],"stock":8},{"name":"Couch","__typename":"Product","upc":"2","reviews":[{"body":"Couch Too expensive.","author":{"__typename":"User","id":"1","name":"user-1"}}],"stock":2},{"name":"Chair","__typename":"Product","upc":"3","reviews":[{"body":"Chair Could be better.","author":{"__typename":"User","id":"2","name":"user-2"}}],"stock":5}]}`
res := NewResolvable()
Expand Down

0 comments on commit fca783c

Please sign in to comment.