New Relic One SDK provides Query components based on ApolloClient's query components. These components are an abstraction layer making it easier to query NerdGraph without worrying about configuring Apollo Client and, for the most common use cases, without having to write GraphQL queries.
A generic NerdGraph Query component that allows you to query anything from NerdGraph.
Usage
import { NerdGraphQuery } from 'nr1'
Examples
Example 1
1function render() {2 const query = `3 query($id: Int!) {4 actor {5 account(id: $id) {6 name7 }8 }9 }10 `;1112 const variables = {13 id: 1,14 };1516 return (17 <NerdGraphQuery query={query} variables={variables}>18 {({ loading, error, data }) => {19 if (loading) {20 return <Spinner />;21 }2223 if (error) {24 return 'Error!';25 }2627 return <BlockText>{data.actor.account.name}</BlockText>;28 }}29 </NerdGraphQuery>30 );31}
Props
Render prop function as a child.
function (queryResult: {Object // Results of the query.
) => undefined
NerdGraphQuery .FETCH_POLICY_TYPE .CACHE_AND_NETWORK
Fetch policy to be used for the query.
Allows you to specify how you want your query to interact with the cached data.
CACHE_AND_NETWORK
: The query returns your initial data from the cache if available. However, regardless of whether or not the full data is in your cache, the query always makes a request using your network interface and returns the updated data. This option is not available when using the staticquery()
method of the component.CACHE_FIRST
: The query makes a request using your network interface only if the data for your query is not already in the cache.CACHE_ONLY
: The query never makes a request using your network interface. Instead it returns the data available in the cache. If the data for your query does not exist in the cache, then an error is thrown.NETWORK_ONLY
: The query never returns your initial data from the cache. Instead it always makes a request using your network interface.NO_CACHE
: The query never returns your initial data from the cache. Instead it always makes a request using your network interface. Unlike theNETWORK_ONLY
policy, it does not write any data to the cache after the query completes.
<One ofNerdGraphQuery.FETCH_POLICY_TYPE.CACHE_AND_NETWORK,NerdGraphQuery.FETCH_POLICY_TYPE.CACHE_FIRST,NerdGraphQuery.FETCH_POLICY_TYPE.CACHE_ONLY,NerdGraphQuery.FETCH_POLICY_TYPE.NETWORK_ONLY,NerdGraphQuery.FETCH_POLICY_TYPE.NO_CACHE,>
0
Interval in milliseconds to poll for new data.
GraphQL query, either as a string or a GraphQL document parsed into
an AST by the gql
method of nr1
.
Example 1
1import { ngql } from 'nr1';23const query = ngql`4 {5 actor {6 user {7 id8 email9 name10 }11 }12 }13`;
Object containing all the variables your query needs to execute.
Methods
NerdGraphQuery.render
function () => undefined
NerdGraphQuery.query
function () => undefined