Skip to content
Merged
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
20 changes: 16 additions & 4 deletions pkg/graphql/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"github.com/apache/skywalking-cli/pkg/logger"
)

// newClient creates a new GraphQL client with configuration from context.
func newClient(ctx context.Context) *graphql.Client {
options := []graphql.ClientOption{}

Expand All @@ -40,7 +41,8 @@ func newClient(ctx context.Context) *graphql.Client {
options = append(options, graphql.WithHTTPClient(httpClient))
}

client := graphql.NewClient(ctx.Value(contextkey.BaseURL{}).(string), options...)
baseURL := getValue(ctx, contextkey.BaseURL{}, "http://127.0.0.1:12800/graphql")
client := graphql.NewClient(baseURL, options...)
client.Log = func(msg string) {
logger.Log.Debugln(msg)
}
Expand All @@ -49,9 +51,10 @@ func newClient(ctx context.Context) *graphql.Client {

// ExecuteQuery executes the `request` and parse to the `response`, returning `error` if there is any.
func ExecuteQuery(ctx context.Context, request *graphql.Request, response any) error {
username := ctx.Value(contextkey.Username{}).(string)
password := ctx.Value(contextkey.Password{}).(string)
authorization := ctx.Value(contextkey.Authorization{}).(string)
username := getValue(ctx, contextkey.Username{}, "")
password := getValue(ctx, contextkey.Password{}, "")
authorization := getValue(ctx, contextkey.Authorization{}, "")

if authorization == "" && username != "" && password != "" {
authorization = "Basic " + base64.StdEncoding.EncodeToString([]byte(username+":"+password))
}
Expand All @@ -63,3 +66,12 @@ func ExecuteQuery(ctx context.Context, request *graphql.Request, response any) e
err := client.Run(ctx, request, response)
return err
}

// getValue safely extracts a value from the context.
func getValue[T any](ctx context.Context, key any, defaultValue T) T {
val := ctx.Value(key)
if v, ok := val.(T); ok {
return v
}
return defaultValue
}
Loading