From d764980390a9f7641a388231ccb896f2af88291b Mon Sep 17 00:00:00 2001 From: Lukasz Raczylo Date: Sat, 8 Apr 2023 01:36:56 +0100 Subject: [PATCH] Add mapstring output and update the README. --- README.md | 2 +- base.go | 2 +- query.go | 8 ++++++++ 3 files changed, 10 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 9b23929..5218571 100644 --- a/README.md +++ b/README.md @@ -34,7 +34,7 @@ Therefore, I present you the simple client to which you can copy & paste your gr * `GRAPHQL_ENDPOINT` - Your GraphQL endpoint. Default: `http://127.0.0.1:9090/v1/graphql` * `GRAPHQL_CACHE_ENABLED` - Should the query cache be enabled? Default: `false` * `GRAPHQL_CACHE_TTL` - Cache TTL in seconds for SELECT type of queries. Default: `5` -* `GRAPHQL_OUTPUT` - Output format. Default: `byte`, available: `byte`, `string` +* `GRAPHQL_OUTPUT` - Output format. Default: `mapstring`, available: `byte`, `string`, `mapstring` * `LOG_LEVEL` - Logging level. Default: `info` available: `debug`, `info`, `warn`, `error` * `GRAPHQL_RETRIES_ENABLE` - Should retries be enabled? Default: `true` * `GRAPHQL_RETRIES_NUMBER` - Number of retries: Default: `1` diff --git a/base.go b/base.go index efc493c..7ee8cfa 100644 --- a/base.go +++ b/base.go @@ -33,7 +33,7 @@ func NewConnection() *BaseClient { } b.endpoint = envutil.Getenv("GRAPHQL_ENDPOINT", "https://api.github.com/graphql") - b.responseType = envutil.Getenv("GRAPHQL_OUTPUT", "string") + b.responseType = envutil.Getenv("GRAPHQL_OUTPUT", "mapstring") b.validate = envutil.GetBool("GRAPHQL_VALIDATE", false) b.cache.enabled = envutil.GetBool("GRAPHQL_CACHE_ENABLED", true) b.cache.ttl = envutil.GetInt("GRAPHQL_CACHE_TTL", 60) diff --git a/query.go b/query.go index 12fd34d..b6de6db 100644 --- a/query.go +++ b/query.go @@ -120,6 +120,14 @@ func (c *BaseClient) Query(queryContent string, queryVariables interface{}, quer func (c *BaseClient) decodeResponse(jsonData []byte) any { switch c.responseType { + case "mapstring": + var response map[string]interface{} + err := json.Unmarshal(jsonData, &response) + if err != nil { + c.Logger.Error(c, "Error while converting to map[string]interface{};", "error", err.Error()) + return nil + } + return response case "string": return helpers.BytesToString(jsonData) case "byte":