From 79cbf052d923cebffe4c5fbfa66d4ed84b1e62ba Mon Sep 17 00:00:00 2001 From: Lavender Shannon Date: Tue, 27 Feb 2024 16:41:08 -0600 Subject: [PATCH] Data path can point to an object rather than only an array of objects --- DEVELOPMENT.md | 1 + pkg/plugin/datasource.go | 8 -------- pkg/plugin/parsing/parsing.go | 6 ++++++ pkg/util/graphql/graphql.go | 2 ++ 4 files changed, 9 insertions(+), 8 deletions(-) diff --git a/DEVELOPMENT.md b/DEVELOPMENT.md index a99cc34..c308314 100644 --- a/DEVELOPMENT.md +++ b/DEVELOPMENT.md @@ -123,6 +123,7 @@ This section contains notes about dependencies. ## To-Do +* Add ability to move parsing options up and down * Add support for secure variable data defined in the data source configuration * The variables defined here cannot be overridden for any request - this is for security * Also add support for secure HTTP headers diff --git a/pkg/plugin/datasource.go b/pkg/plugin/datasource.go index eb1b987..ee5e60a 100644 --- a/pkg/plugin/datasource.go +++ b/pkg/plugin/datasource.go @@ -173,14 +173,6 @@ func (d *Datasource) query(ctx context.Context, pCtx backend.PluginContext, quer }, nil } - //dataBytes, serializeError := json.Marshal(graphQLResponse.Data) - //if serializeError != nil { - // return nil, serializeError // this should not happen - //} - //log.DefaultLogger.Info(fmt.Sprintf("result is: %s", dataBytes)) - - log.DefaultLogger.Debug("Successful query!") - var response backend.DataResponse // add the frames to the response. diff --git a/pkg/plugin/parsing/parsing.go b/pkg/plugin/parsing/parsing.go index 2f44cb4..bb4b7c4 100644 --- a/pkg/plugin/parsing/parsing.go +++ b/pkg/plugin/parsing/parsing.go @@ -58,6 +58,12 @@ func ParseData(graphQlResponseData map[string]interface{}, parsingOption querymo return nil, errors.New(fmt.Sprintf("One of the elements inside the data array is not an object! element: %d is of type: %v", i, reflect.TypeOf(element))), FRIENDLY_ERROR } } + case map[string]interface{}: + // It's also valid if the final part of the data path refers to an object. + // The only downside of this is that it makes configuration errors harder to diagnose. + dataArray = []map[string]interface{}{ + value, + } default: return nil, errors.New(fmt.Sprintf("Final part of data path: is not an array! dataPath: %s type of result: %v", parsingOption.DataPath, reflect.TypeOf(value))), FRIENDLY_ERROR } diff --git a/pkg/util/graphql/graphql.go b/pkg/util/graphql/graphql.go index 510cbc3..e41a3be 100644 --- a/pkg/util/graphql/graphql.go +++ b/pkg/util/graphql/graphql.go @@ -37,6 +37,8 @@ func (request *GraphQLRequest) ToRequest(ctx context.Context, url string) (*http } type GraphQLResponse struct { + // TODO we need this data to be able to iterate over it in its actual order. + // Go maps don't guarantee insertion order or any order for that matter Data map[string]interface{} `json:"data"` Errors []GraphQLError `json:"errors"` }