You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@skywalking.apache.org by ke...@apache.org on 2025/06/04 01:07:13 UTC
(skywalking-cli) branch master updated: Add default values for GraphQL variables (#224)
This is an automated email from the ASF dual-hosted git repository.
kezhenxu94 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/skywalking-cli.git
The following commit(s) were added to refs/heads/master by this push:
new 77b4c49 Add default values for GraphQL variables (#224)
77b4c49 is described below
commit 77b4c49e89c9c000278f44e62729d534f2ec842e
Author: Zixin Zhou <zh...@apache.org>
AuthorDate: Wed Jun 4 09:07:08 2025 +0800
Add default values for GraphQL variables (#224)
---
pkg/graphql/client/client.go | 20 ++++++++++++++++----
1 file changed, 16 insertions(+), 4 deletions(-)
diff --git a/pkg/graphql/client/client.go b/pkg/graphql/client/client.go
index 29e0910..a2ad60a 100644
--- a/pkg/graphql/client/client.go
+++ b/pkg/graphql/client/client.go
@@ -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{}
@@ -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)
}
@@ -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))
}
@@ -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
+}