-
Notifications
You must be signed in to change notification settings - Fork 69
/
command_query.go
75 lines (59 loc) · 1.99 KB
/
command_query.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package nerdgraph
import (
"bytes"
"encoding/json"
"errors"
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/pkg/nerdgraph"
)
var (
variables string
)
var cmdQuery = &cobra.Command{
Use: "query",
Short: "Execute a raw GraphQL query request to the NerdGraph API",
Long: `Execute a raw GraphQL query request to the NerdGraph API
The query command accepts a single argument in the form of a GraphQL query as a string.
This command accepts an optional flag, --variables, which should be a JSON string where the
keys are the variables to be referenced in the GraphQL query.
`,
Example: `newrelic nerdgraph query 'query($guid: EntityGuid!) { actor { entity(guid: $guid) { guid name domain entityType } } }' --variables '{"guid": "<GUID>"}'`,
Args: func(cmd *cobra.Command, args []string) error {
argsCount := len(args)
if argsCount < 1 {
return errors.New("missing graph query argument")
}
if argsCount > 1 {
return errors.New("command expects only 1 argument")
}
return nil
},
PreRun: client.RequireClient,
Run: func(cmd *cobra.Command, args []string) {
var variablesParsed map[string]interface{}
err := json.Unmarshal([]byte(variables), &variablesParsed)
if err != nil {
log.Fatal(err)
}
query := args[0]
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(ng.QueryResponse{
Actor: result.(ng.QueryResponse).Actor,
})
utils.LogIfFatal(err)
utils.LogIfFatal(output.Print(reqBodyBytes))
},
}
func init() {
Command.AddCommand(cmdQuery)
cmdQuery.Flags().StringVar(&variables, "variables", "{}", "the variables to pass to the GraphQL query, represented as a JSON string")
}