Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: crash when requesting trace info without initializing it #704

Merged
merged 1 commit into from
Dec 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading