Skip to content

Commit 2d250c7

Browse files
fiampvormste
authored andcommitted
fix: crash when requesting trace info without initializing it (wundergraph#704)
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.
1 parent 248ac3d commit 2d250c7

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

v2/pkg/engine/resolve/context.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,5 +138,7 @@ func SetPlannerStats(ctx context.Context, stats PlannerStats) {
138138
}
139139

140140
func GetTraceInfo(ctx context.Context) *TraceInfo {
141-
return ctx.Value(traceStartKey{}).(*TraceInfo)
141+
// The context might not have trace info, in that case we return nil
142+
info, _ := ctx.Value(traceStartKey{}).(*TraceInfo)
143+
return info
142144
}

v2/pkg/engine/resolve/resolvable_test.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -469,6 +469,33 @@ func BenchmarkResolvable_ResolveWithErrorBubbleUp(b *testing.B) {
469469
}
470470
}
471471

472+
func TestResolvable_WithTracingNotStarted(t *testing.T) {
473+
res := NewResolvable()
474+
// Do not start a trace with SetTraceStart(), but request it to be output
475+
ctx := NewContext(context.Background())
476+
ctx.RequestTracingOptions.IncludeTraceOutputInResponseExtensions = true
477+
err := res.Init(ctx, []byte(`{"hello": "world"}`), ast.OperationTypeQuery)
478+
assert.NoError(t, err)
479+
object := &Object{
480+
Fields: []*Field{
481+
{
482+
Name: []byte("hello"),
483+
Value: &String{
484+
Path: []string{"hello"},
485+
},
486+
},
487+
},
488+
}
489+
out := &bytes.Buffer{}
490+
err = res.Resolve(ctx.ctx, object, out)
491+
492+
assert.NoError(t, err)
493+
assert.JSONEq(t, `{
494+
"data": {"hello":"world"},
495+
"extensions":{"trace":{"node_type":"object","nullable":true,"fields":[{"name":"hello","value":{"node_type":"string","path":["hello"]}}]}}
496+
}`, out.String())
497+
}
498+
472499
func TestResolvable_WithTracing(t *testing.T) {
473500
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}]}`
474501
res := NewResolvable()

0 commit comments

Comments
 (0)