nerdgraph

package
v0.19.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 25, 2020 License: Apache-2.0 Imports: 5 Imported by: 2

Documentation

Overview

Package nerdgraph provides a programmatic API for interacting with the New Relic NerdGraph GraphQL API. It can be used to run freeform queries and mutations. The GraphiQL API explorer is a useful resource for learning about what is available via the NerdGraph API:

https://api.newrelic.com/graphiql?#

Authentication

You will need a valid Personal API key to communicate with the backend New Relic API that provides this functionality. See the API key documentation below for more information on how to locate this key:

https://docs.newrelic.com/docs/apis/get-started/intro-apis/types-new-relic-api-keys

Package nerdgraph provides a programmatic API for interacting with NerdGraph, New Relic One's GraphQL API.

Example (Query)
// Initialize the client configuration.  A Personal API key is required to
// communicate with the backend API.
cfg := config.Config{
	PersonalAPIKey: os.Getenv("NEW_RELIC_API_KEY"),
}

// Initialize the client.
client := New(cfg)

// Execute a NRQL query to retrieve the average duration of transactions for
// the "Example application" app.
query := `
	query($accountId: Int!, $nrqlQuery: Nrql!) {
		actor {
			account(id: $accountId) {
				nrql(query: $nrqlQuery, timeout: 5) {
					results
				}
			}
		}
	}`

variables := map[string]interface{}{
	"accountId": 12345678,
	"nrqlQuery": "SELECT average(duration) FROM Transaction TIMESERIES WHERE appName = 'Example application'",
}

resp, err := client.Query(query, variables)
if err != nil {
	log.Fatal("error running NerdGraph query: ", err)
}

queryResp := resp.(QueryResponse)
actor := queryResp.Actor.(map[string]interface{})
account := actor["account"].(map[string]interface{})
nrql := account["nrql"].(map[string]interface{})
results := nrql["results"].([]interface{})

var durations []float64
for _, r := range results {
	data := r.(map[string]interface{})
	durations = append(durations, data["average.duration"].(float64))
}

// Output the raw time series values for transaction duration.
fmt.Printf("durations: %v\n", durations)
Output:

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func ResolveSchemaTypes added in v0.19.0

func ResolveSchemaTypes(schema Schema, typeNames []string) (map[string]string, error)

func TypeGen added in v0.19.0

func TypeGen(schema Schema, typeName string) (map[string]string, error)

TypeGen is the mother type generator.

Types

type AccountReference added in v0.17.0

type AccountReference struct {
	ID   int    `json:"id,omitempty"`
	Name string `json:"name,omitempty"`
}

AccountReference represents the NerdGraph schema for a New Relic account.

type NerdGraph

type NerdGraph struct {
	// contains filtered or unexported fields
}

NerdGraph is used to communicate with the New Relic's GraphQL API, NerdGraph.

func New

func New(config config.Config) NerdGraph

New returns a new GraphQL client for interacting with New Relic's GraphQL API, NerdGraph.

func (*NerdGraph) Query

func (n *NerdGraph) Query(query string, variables map[string]interface{}) (interface{}, error)

Query facilitates making a NerdGraph request with a raw GraphQL query. Variables may be provided in the form of a map. The response's data structure will vary based on the query provided.

func (*NerdGraph) QuerySchema added in v0.19.0

func (n *NerdGraph) QuerySchema() (*Schema, error)

type QueryResponse added in v0.13.0

type QueryResponse struct {
	Actor          interface{} `json:"actor,omitempty"`
	Docs           interface{} `json:"docs,omitempty"`
	RequestContext interface{} `json:"requestContext,omitempty"`
}

QueryResponse represents the top-level GraphQL response object returned from a NerdGraph query request.

type Schema added in v0.19.0

type Schema struct {
	Types []*SchemaType `json:"types"`
}

type SchemaInputValue added in v0.19.0

type SchemaInputValue struct {
	DefaultValue interface{}   `json:"defaultValue"`
	Description  string        `json:"description"`
	Name         string        `json:"name"`
	Type         SchemaTypeRef `json:"type"`
}

type SchemaType added in v0.19.0

type SchemaType struct {
	InputFields []SchemaInputValue `json:"inputFields"`
	Kind        string             `json:"kind"`
	Name        string             `json:"name"`
	// Description string             `json:"description"`
	Fields []struct {
		Name        string             `json:"name"`
		Description string             `json:"description"`
		Args        []SchemaInputValue `json:"args"`
		Type        SchemaTypeRef      `json:"type"`
	} `json:"fields"`
	Interfaces    []SchemaTypeRef `json:"interfaces"`
	PossibleTypes []SchemaTypeRef `json:"possibleTypes"`
	EnumValues    []struct {
		Name              string `json:"name"`
		Description       string `json:"description"`
		IsDeprecated      bool   `json:"isDeprecated"`
		DeprecationReason string `json:"deprecationReason"`
	} `json:"enumValues"`
}

Wheee... :)

type SchemaTypeRef added in v0.19.0

type SchemaTypeRef struct {
	Name   string `json:"name"`
	Kind   string `json:"kind"`
	OfType struct {
		Name   string `json:"name"`
		Kind   string `json:"kind"`
		OfType struct {
			Name   string `json:"name"`
			Kind   string `json:"kind"`
			OfType struct {
				Name   string `json:"name"`
				Kind   string `json:"kind"`
				OfType struct {
					Name   string `json:"name"`
					Kind   string `json:"kind"`
					OfType struct {
						Name   string `json:"name"`
						Kind   string `json:"kind"`
						OfType struct {
							Name   string `json:"name"`
							Kind   string `json:"kind"`
							OfType struct {
								Name string `json:"name"`
								Kind string `json:"kind"`
							} `json:"ofType"`
						} `json:"ofType"`
					} `json:"ofType"`
				} `json:"ofType"`
			} `json:"ofType"`
		} `json:"ofType"`
	} `json:"ofType"`
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL