github.com/newrelic/newrelic-client-go@v1.1.0/pkg/nerdgraph/nerdgraph.go (about) 1 // Package nerdgraph provides a programmatic API for interacting with NerdGraph, New Relic One's GraphQL API. 2 package nerdgraph 3 4 import ( 5 "context" 6 7 "github.com/newrelic/newrelic-client-go/internal/http" 8 "github.com/newrelic/newrelic-client-go/pkg/config" 9 "github.com/newrelic/newrelic-client-go/pkg/logging" 10 ) 11 12 // NerdGraph is used to communicate with the New Relic's GraphQL API, NerdGraph. 13 type NerdGraph struct { 14 client http.Client 15 logger logging.Logger 16 } 17 18 // QueryResponse represents the top-level GraphQL response object returned 19 // from a NerdGraph query request. 20 type QueryResponse struct { 21 Actor interface{} `json:"actor,omitempty" yaml:"actor,omitempty"` 22 Docs interface{} `json:"docs,omitempty" yaml:"docs,omitempty"` 23 RequestContext interface{} `json:"requestContext,omitempty" yaml:"requestContext,omitempty"` 24 } 25 26 // New returns a new GraphQL client for interacting with New Relic's GraphQL API, NerdGraph. 27 func New(config config.Config) NerdGraph { 28 return NerdGraph{ 29 client: http.NewClient(config), 30 logger: config.GetLogger(), 31 } 32 } 33 34 // Query facilitates making a NerdGraph request with a raw GraphQL query. Variables may be provided 35 // in the form of a map. The response's data structure will vary based on the query provided. 36 func (n *NerdGraph) Query(query string, variables map[string]interface{}) (interface{}, error) { 37 return n.QueryWithContext(context.Background(), query, variables) 38 } 39 40 // QueryWithContext facilitates making a NerdGraph request with a raw GraphQL query. Variables may be provided 41 // in the form of a map. The response's data structure will vary based on the query provided. 42 func (n *NerdGraph) QueryWithContext(ctx context.Context, query string, variables map[string]interface{}) (interface{}, error) { 43 respBody := QueryResponse{} 44 45 if err := n.QueryWithResponseAndContext(ctx, query, variables, &respBody); err != nil { 46 return nil, err 47 } 48 49 return respBody, nil 50 } 51 52 // QueryWithResponse functions similarly to Query, but alows for full customization of the returned data payload. 53 // Query should be preferred most of the time. 54 func (n *NerdGraph) QueryWithResponse(query string, variables map[string]interface{}, respBody interface{}) error { 55 return n.QueryWithResponseAndContext(context.Background(), query, variables, respBody) 56 } 57 58 // QueryWithResponseAndContext functions similarly to QueryWithContext, but alows for full customization of the returned data payload. 59 // QueryWithContext should be preferred most of the time. 60 func (n *NerdGraph) QueryWithResponseAndContext(ctx context.Context, query string, variables map[string]interface{}, respBody interface{}) error { 61 return n.client.NerdGraphQueryWithContext(ctx, query, variables, respBody) 62 } 63 64 // AccountReference represents the NerdGraph schema for a New Relic account. 65 type AccountReference struct { 66 ID int `json:"id,omitempty" yaml:"id,omitempty"` 67 Name string `json:"name,omitempty" yaml:"name,omitempty"` 68 }