Skip to content

Commit

Permalink
Merge pull request #8 from hgiasac/forward-headers-from-grafana-requests
Browse files Browse the repository at this point in the history
Forward headers from Grafana to GraphQL requests. Closes #7
  • Loading branch information
retrodaredevil authored Oct 18, 2024
2 parents 7d56613 + f004999 commit 6f4adf1
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
10 changes: 5 additions & 5 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ jobs:
- name: Setup Node.js environment
uses: actions/setup-node@v3
with:
node-version: '20'
cache: 'npm'
node-version: "20"
cache: "npm"

- name: Install dependencies
run: npm ci
Expand All @@ -45,7 +45,7 @@ jobs:
if: steps.check-for-backend.outputs.has-backend == 'true'
uses: actions/setup-go@v3
with:
go-version: '1.21'
go-version: "1.21"

- name: Test backend
if: steps.check-for-backend.outputs.has-backend == 'true'
Expand All @@ -71,15 +71,15 @@ jobs:
- name: Start grafana docker
if: steps.check-for-e2e.outputs.has-e2e == 'true'
run: docker-compose up -d
run: docker compose up -d

- name: Run e2e tests
if: steps.check-for-e2e.outputs.has-e2e == 'true'
run: npm run e2e

- name: Stop grafana docker
if: steps.check-for-e2e.outputs.has-e2e == 'true'
run: docker-compose down
run: docker compose down

- name: Archive E2E output
uses: actions/upload-artifact@v3
Expand Down
13 changes: 10 additions & 3 deletions pkg/plugin/datasource.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@ import (
"encoding/json"
"errors"
"fmt"
"net/http"

"github.com/grafana/grafana-plugin-sdk-go/backend"
"github.com/grafana/grafana-plugin-sdk-go/backend/httpclient"
"github.com/grafana/grafana-plugin-sdk-go/backend/instancemgmt"
"github.com/wildmountainfarms/wild-graphql-datasource/pkg/plugin/parsing"
"github.com/wildmountainfarms/wild-graphql-datasource/pkg/plugin/querymodel"
"github.com/wildmountainfarms/wild-graphql-datasource/pkg/plugin/queryvariables"
"github.com/wildmountainfarms/wild-graphql-datasource/pkg/util/graphql"
"net/http"
)

// Make sure Datasource implements required interfaces. This is important to do
Expand Down Expand Up @@ -73,7 +74,7 @@ func (d *Datasource) QueryData(ctx context.Context, req *backend.QueryDataReques
// but attempting to do that is out of scope for us right now, especially with how complicated a GraphQL query can be.

for _, q := range req.Queries {
res, err := d.query(ctx, req.PluginContext, q)
res, err := d.query(ctx, req, q)
if err != nil {
// If an error is returned from the query, we assume that it is not a recoverable error.
// We can consider changing this in the future
Expand All @@ -100,7 +101,7 @@ func statusFromResponse(response http.Response) backend.Status {
// In most error scenarios, the error should be nested within the DataResponse.
// In some cases that are never expected to happen, error is returned and the DataResponse is nil.
// In these cases, you can assume that something is seriously wrong, as we didn't intend to recover from that specific situation.
func (d *Datasource) query(ctx context.Context, pCtx backend.PluginContext, query backend.DataQuery) (*backend.DataResponse, error) {
func (d *Datasource) query(ctx context.Context, req *backend.QueryDataRequest, query backend.DataQuery) (*backend.DataResponse, error) {

//log.DefaultLogger.Info(fmt.Sprintf("JSON is: %s", query.JSON))

Expand Down Expand Up @@ -133,6 +134,9 @@ func (d *Datasource) query(ctx context.Context, pCtx backend.PluginContext, quer
return nil, err
}

for key, value := range req.GetHTTPHeaders() {
request.Header[key] = value
}
resp, err := d.httpClient.Do(request)
if err != nil {
// http.Client.Do returns an error when there's a network connectivity problem or something weird going on,
Expand Down Expand Up @@ -215,6 +219,9 @@ func (d *Datasource) CheckHealth(ctx context.Context, req *backend.CheckHealthRe
return nil, err
}

for key, value := range req.GetHTTPHeaders() {
request.Header[key] = value
}
resp, err := d.httpClient.Do(request)
if err != nil {
return nil, err
Expand Down

0 comments on commit 6f4adf1

Please sign in to comment.