Query NRQL through the New Relic GraphQL API

You can use the New Relic GraphQL API to make New Relic Query Language (NRQL) queries. To learn how to construct these queries and see responses, you can use the New Relic GraphQL Explorer. This document explains some of the available functions. 

Basic NRQL queries with GraphQL

You can make NRQL queries using New Relic's GraphQL API by passing the NRQL query as a string argument to the NRQL object and including the results field in your GraphQL query. For example, you can use the following query to get a count of all transaction events in the last hour:

{
   actor {
      account(id: YOUR_ACCOUNT_ID) {
         nrql(query: "SELECT count(*) FROM Transaction SINCE 1 HOUR AGO") {
            results
         }
      }
   }
}

This query returns the following:

{
   "data": {
      "actor": {
         "account": {
            "nrql": {
               "results": [
                 {
                  "count": 1000
                 }
                ]
            }
         }
      }
   }
}

The actual value of the count varies depending on your transaction data. Use the New Relic GraphQL Explorer to experiment with queries.

Create embeddable charts

In addition to returning raw data, you can fetch embeddable chart links for the data to use in an application. For example, instead of a single count of transaction, you can create a chart that illustrates a timeseries of bucketed counts over time. Add TIMESERIES to your query with embeddedChartUrl:

{
   actor {
      account(id: YOUR_ACCOUNT_ID) {
         nrql(query: "SELECT count(*) from Transaction TIMESERIES") {
            embeddedChartUrl
         }
      }
   }
}

This returns the URL for the chart in the following response:

{
   "data": {
      "actor": {
         "account": {
            "nrql": {
               "embeddedChartUrl": "https://chart-embed.service.newrelic.com/charts/EMBEDDABLE-CHART-ID"
            }
         }
      }
   }
}

If you view the embed chart URL using any standard HTTP client, it returns an image showing a visualization of the response to the query you submitted. These charts follow the same embedded chart rules as embedded charts that are created with New Relic Insights. To change the style of the data visualization, pass a chartType argument to embeddedChartUrl.

Suggested facets

When using the New Relic GraphQL API to explore your data, you can use the suggestedFacets field to return suggested attributes for use in faceted NRQL queries.

Rules governing suggested facets

Here are some of the rules that govern what attributes are suggested:

  • Built-in suggestions. Each event type comes with its own set of recommended attributes. These are attributes chosen by New Relic for their importance and popularity.
  • Usage-based suggestions. Some attribute suggestions are based on the queries that have been frequently used by your account. These suggestions can include custom attributes.
  • Role restriction. Restricted Users do not have access to account-related facet suggestions.

To disable the use of account data for determining suggested queries, contact support.

Example of returning suggested attributes

Here's an example of returning suggested attributes for faceting transaction counts. The response suggests the host attribute. Faceting by host can reveal that one host is servicing more requests than other hosts.

{
   actor {
      account(id: YOUR_ACCOUNT_ID) {
         nrql(query: "SELECT count(*) from Transaction TIMESERIES") {
            suggestedFacets {
               attributes
            }
         }
      }
   }
}

This query returns a response similar to:

{
   "data": {
      "actor": {
         "account": {
            "nrql": {
               "suggestedFacets": [
       	          "attributes": ["host"]
               ]
            }
         }
      }
   }
}

For more help

Recommendations for learning more: