Components
Deprecated React Apollo render prop component API
Note: Official support for React Apollo render prop components ended in March 2020. This library is still included in the @apollo/client
package, but it no longer receives feature updates or bug fixes.
Installation
The render prop library is included in the core @apollo/client
package:
npm install @apollo/client
You then import the library's symbols from @apollo/client/react/components
.
Query
Props
The Query
component accepts the following props. query
is required.
Name / Type | Description |
---|---|
Operation options | |
| A GraphQL query string parsed into an AST with the Optional for the |
| An object containing all of the GraphQL variables your query requires to execute. Each key in the object corresponds to a variable name, and that key's value corresponds to the variable value. |
| Specifies how the query handles a response that returns both GraphQL errors and partial results. For details, see GraphQL error policies. The default value is |
| A callback function that's called when your query successfully completes with zero errors (or if This function is passed the query's result |
| A callback function that's called when the query encounters one or more errors (unless This function is passed an |
| If This property is part of Apollo Client's React integration, and it is not available in the core The default value is |
Networking options | |
| Specifies the interval (in milliseconds) at which the query polls for updated results. The default value is |
| If The default value is |
| If you're using Apollo Link, this object is the initial value of the |
| Pass |
| The instance of By default, the instance that's passed down via context is used, but you can provide a different instance here. |
Caching options | |
| Specifies how the query interacts with the Apollo Client cache during execution (for example, whether it checks the cache for results before sending a request to the server). For details, see Setting a fetch policy. The default value is |
| Specifies the For example, you can use this to switch back to a |
| If The default value is |
Deprecated options | |
| Deprecated. If The default value is |
Render prop function
The render prop function that you pass to the children
prop of Query
is called with an object (QueryResult
) that has the following properties. This object contains your query result, plus some helpful functions for refetching, dynamic polling, and pagination.
Name / Type | Description |
---|---|
Operation data | |
| An object containing the result of your GraphQL query after it completes. This value might be |
| An object containing the result from the most recent previous execution of this query. This value is |
| If the query produces one or more errors, this object contains either an array of For more information, see Handling operation errors. |
| An object containing the variables that were provided for the query. |
Network info | |
| If |
| A number indicating the current network state of the query's associated request. See possible values. Used in conjunction with the |
| The instance of Apollo Client that executed the query. Can be useful for manually executing followup queries or writing data to the cache. |
| If This field is only present on the result object returned by |
Helper functions | |
| A function that enables you to re-execute the query, optionally passing in new To guarantee that the refetch performs a network request, its See also Refetching. |
| A function that helps you fetch the next set of results for a paginated list field. |
| A function that instructs the query to begin re-executing at a specified interval (in milliseconds). |
| A function that instructs the query to stop polling after a previous call to |
| A function that enables you to execute a subscription, usually to subscribe to specific fields that were included in the query. This function returns another function that you can call to terminate the subscription. |
| A function that enables you to update the query's cached result without executing a followup GraphQL operation. See using updateQuery and updateFragment for additional information. |
Mutation
The Mutation component accepts the following props. Only mutation
is required.
Name / Type | Description |
---|---|
Operation options | |
| A GraphQL query string parsed into an AST with the Optional for the Required for the |
| An object containing all of the GraphQL variables your mutation requires to execute. Each key in the object corresponds to a variable name, and that key's value corresponds to the variable value. |
| Specifies how the mutation handles a response that returns both GraphQL errors and partial results. For details, see GraphQL error policies. The default value is |
| A callback function that's called when your mutation successfully completes with zero errors (or if This function is passed the mutation's result |
| A callback function that's called when the mutation encounters one or more errors (unless This function is passed an |
| Optional callback for intercepting queries whose cache data has been updated by the mutation, as well as any queries specified in the Returning a |
| An array (or a function that returns an array) that specifies which queries you want to refetch after the mutation occurs. Each array value can be either:
|
| If The default value is |
| If The default value is |
Networking options | |
| If The default value is |
| The instance of By default, the instance that's passed down via context is used, but you can provide a different instance here. |
| If you're using Apollo Link, this object is the initial value of the |
Caching options | |
| A function used to update the Apollo Client cache after the mutation completes. For more information, see Updating the cache after a mutation. |
| If provided, Apollo Client caches this temporary (and potentially incorrect) response until the mutation completes, enabling more responsive UI updates. For more information, see Optimistic mutation results. |
| Provide The default value is Unlike queries, mutations do not support fetch policies besides |
Render prop function
The render prop function that you pass to the children
prop of Mutation
is called with the mutate
function and an object with the mutation result. The mutate
function is how you trigger the mutation from your UI. The object contains your mutation result, plus loading and error state.
Mutate function:
Name / Type | Description |
---|---|
| A function to trigger the mutation from your UI. You can optionally pass this function any of the following options:
Any option you pass here overrides any existing value for that option that you passed to The mutate function returns a promise that fulfills with your mutation result. |
Mutation result:
Name / Type | Description |
---|---|
| The data returned from your mutation. Can be |
| If |
| If the mutation produces one or more errors, this object contains either an array of For more information, see Handling operation errors. |
| If |
| The instance of Apollo Client that executed the mutation. Can be useful for manually executing followup operations or writing data to the cache. |
| A function that you can call to reset the mutation's result to its initial, uncalled state. |
Subscription
Props
The Subscription component accepts the following props. Only subscription
is required.
Option | Type | Description |
---|---|---|
subscription | DocumentNode | A GraphQL subscription document parsed into an AST by graphql-tag . Optional for the useSubscription Hook since the subscription can be passed in as the first parameter to the Hook. Required for the Subscription component. |
variables | { [key: string]: any } | An object containing all of the variables your subscription needs to execute |
shouldResubscribe | boolean | Determines if your subscription should be unsubscribed and subscribed again when an input to the hook (such as subscription or variables ) changes. |
skip | boolean | Determines if the current subscription should be skipped. Useful if, for example, variables depend on previous queries and are not ready yet. |
onSubscriptionData | Deprecated. (options: OnSubscriptionDataOptions<TData>) => any | Allows the registration of a callback function that will be triggered each time the useSubscription Hook / Subscription component receives data. The callback options object param consists of the current Apollo Client instance in client , and the received subscription data in subscriptionData . |
onData | (options: OnDataOptions<TData>) => any | Allows the registration of a callback function that will be triggered each time the useSubscription Hook / Subscription component receives data. The callback options object param consists of the current Apollo Client instance in client , and the received subscription data in data . |
onError | (error: ApolloError) => void | Allows the registration of a callback function that will be triggered each time the useSubscription Hook / Subscription component receives an error. |
onSubscriptionComplete | Deprecated. () => void | Allows the registration of a callback function that will be triggered when the useSubscription Hook / Subscription component completes the subscription. |
onComplete | () => void | Allows the registration of a callback function that will be triggered each time the useSubscription Hook / Subscription component completes the subscription. |
fetchPolicy | FetchPolicy | How you want your component to interact with the Apollo cache. For details, see Setting a fetch policy. |
context | Record<string, any> | Shared context between your component and your network interface (Apollo Link). |
client | ApolloClient | An ApolloClient instance. By default useSubscription / Subscription uses the client passed down via context, but a different client can be passed in. |
Render prop function
The render prop function that you pass to the children
prop of Subscription
is called with an object that has the following properties.
Property | Type | Description |
---|---|---|
data | TData | An object containing the result of your GraphQL subscription. Defaults to an empty object. |
loading | boolean | A boolean that indicates whether any initial data has been returned |
error | ApolloError | A runtime error with graphQLErrors and networkError properties |