Skip to content

Commit

Permalink
feat(nerdgraph): Add support to output results for mutations in CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
vinay-newrelic committed Dec 4, 2024
1 parent 69b8299 commit 12eabdc
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions internal/nerdgraph/command_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,15 @@ import (
"bytes"
"encoding/json"
"errors"
"github.com/newrelic/newrelic-cli/internal/output"
"io/ioutil"
"strings"

log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"

"github.com/newrelic/newrelic-cli/internal/client"
"github.com/newrelic/newrelic-cli/internal/output"
"github.com/newrelic/newrelic-cli/internal/utils"
ng "github.com/newrelic/newrelic-client-go/v2/pkg/nerdgraph"
)

var (
Expand Down Expand Up @@ -86,21 +85,32 @@ keys are the variables to be referenced in the GraphQL query.
query = args[0]
}

result, err := client.NRClient.NerdGraph.QueryWithContext(utils.SignalCtx, query, variablesParsed)
trimmedQuery := strings.TrimSpace(query)
var result interface{}

// Check if it starts with "actor {" or "mutation {"
if strings.HasPrefix(trimmedQuery, "mutation {") {
err = client.NRClient.NerdGraph.QueryWithResponseAndContext(utils.SignalCtx, query, variablesParsed, &result)
} else {
result, err = client.NRClient.NerdGraph.QueryWithContext(utils.SignalCtx, query, variablesParsed)
}

if err != nil {
log.Fatal(err)
}

reqBodyBytes := new(bytes.Buffer)

encoder := json.NewEncoder(reqBodyBytes)
err = encoder.Encode(result)

/* The below variable specific encoding is no more needed we need a common and dynamic interface for the query and mutation related response
err = encoder.Encode(ng.QueryResponse{
Actor: result.(ng.QueryResponse).Actor,
Docs: result.(ng.QueryResponse).Docs,
RequestContext: result.(ng.QueryResponse).RequestContext,
})
*/
utils.LogIfFatal(err)

utils.LogIfFatal(output.Print(reqBodyBytes))
},
}
Expand Down

0 comments on commit 12eabdc

Please sign in to comment.