Skip to content

Commit

Permalink
Data path can point to an object rather than only an array of objects
Browse files Browse the repository at this point in the history
  • Loading branch information
retrodaredevil committed Feb 27, 2024
1 parent 91db6b7 commit 79cbf05
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 8 deletions.
1 change: 1 addition & 0 deletions DEVELOPMENT.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 0 additions & 8 deletions pkg/plugin/datasource.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
6 changes: 6 additions & 0 deletions pkg/plugin/parsing/parsing.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
2 changes: 2 additions & 0 deletions pkg/util/graphql/graphql.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}
Expand Down

0 comments on commit 79cbf05

Please sign in to comment.