Interfaces: ApolloSubscription
Subscription components update in real time in reaction to events on your GraphQL server. They are ideal for uses like chat rooms or stock tickers.
Subscription components inherit the ApolloElementInterface.
Exports
import { ApolloSubscriptionInterface } from '@apollo-elements/interfaces/apollo-subscription';
Properties
fetchPolicy
FetchPolicy
Specifies the FetchPolicy to be used for this subscription.
noAutoSubscribe
no-auto-subscribeboolean
If true, the element will not begin querying data until you manually call subscribe
notifyOnNetworkStatusChange
boolean
Whether or not updates to the network status should trigger next on the observer of this subscription.
observable
(optional)Observable<FetchResult<Data<D>>>
Observable watching this element's subscription.
observableSubscription
ZenObservable.Subscription
Subscription to the observable
pollInterval
(optional)number
The time interval (in milliseconds) on which this subscription should be refetched from the server.
shouldResubscribe
boolean | ((options: SubscriptionDataOptions<D, V>) => boolean)
Determines if your subscription should be unsubscribed and subscribed again.
skip
boolean
If skip is true, the query will be skipped entirely
subscription
DocumentNode | TypedDocumentNode
A GraphQL document containing a single subscription. You can set it as a JavaScript property or by appending a GraphQL script to the element (light DOM).
<script type="application/graphql">
subscription { messages { text } }
</script>
client
inherited from ApolloElementInterfaceApolloClient<NormalizedCacheObject> | null
The Apollo Client instance
Defaults to window.__APOLLO_CLIENT__
, which is set by default when creating an Apollo Client. That means that in most cases, it's not necessary to explicity set the client
property. For more information see Getting Started: Apollo Client.
context
(optional) inherited from ApolloElementInterfaceRecord<string, unknown>
Context passed to the link execution chain.
data
inherited from ApolloElementInterfaceData<D> | null
Latest subscription data.
document
inherited from ApolloElementInterfaceDocumentNode | TypedDocumentNode | null
A GraphQL document containing a single query, mutation, or subscription. You can set it as a JavaScript property or by appending a GraphQL script to the element (light DOM).
documentType
inherited from ApolloElementInterface'subscription'
Hint that this is a subscription component
error
(optional) inherited from ApolloElementInterfaceError | ApolloError | null
Latest error
errorPolicy
(optional) inherited from ApolloElementInterfaceErrorPolicy
Specifies the Error Policy to be used for this subscription.
Much like fetchPolicy
, errorPolicy
allows you to control how GraphQL Errors from the server are sent to your UI code. By default, the error policy treats any GraphQL Errors as network errors and ends the request chain. It doesn't save any data in the cache, and renders your UI with the error property set to an ApolloError
. By changing this policy per request, you can adjust how GraphQL Errors are managed by your UI. The possible options for errorPolicy
are:
none
: This is the default policy to match how Apollo Client 1.0 worked. Any GraphQL Errors are treated the same as network errors and any data is ignored from the response.ignore
: Ignore allows you to read any data that is returned alongside GraphQL Errors, but doesn't save the errors or report them to your UI.all
: Using the all policy is the best way to notify your users of potential issues while still showing as much data as possible from your server. It saves both data and errors so your UI can use them.
errors
(optional) inherited from ApolloElementInterfacereadonly GraphQLError[] | null
Latest errors.
loading
inherited from ApolloElementInterfaceboolean
Whether a request is in-flight.
variables
inherited from ApolloElementInterfaceVariables<D, V> | null
An object map from variable name to variable value, where the variables are used within the GraphQL subscription.
If the element has not yet subscribed and noAutoSubscribe
is not set, setting variables causes the element to subscribe, otherwise, setting variables causes the query to refetch.
Methods
cancel
Cancels and clears the subscription
Returns
void
onError
(optional)Callback for when an error occurs.
Parameters
error
Error
Returns
void
onSubscriptionComplete
(optional)Callback for when subscription completes.
Returns
void
onSubscriptionData
(optional)Callback for when subscription data updates.
Parameters
data
Data<D>
Returns
void
shouldSubscribe
Determines whether the element should attempt to subscribe i.e. begin querying Override to prevent subscribing unless your conditions are met
Parameters
options
Partial<SubscriptionOptions<Variables<D, V>, Data<D>>>
Property | Type | Description |
---|---|---|
subscription |
|
See subscription |
variables |
|
See variables |
fetchPolicy |
|
See fetchPolicy |
errorPolicy |
|
See errorPolicy |
context |
|
Context object passed through the link execution chain. |
Returns
boolean
subscribe
Resets the observable and subscribes.
Parameters
params
Partial<SubscriptionDataOptions<D, V>>
Options for controlling how the subscription subscribes.
Option | Type | Description |
---|---|---|
client |
|
Apollo Client to use for the subscription. |
context |
|
Context object passed through the link execution chain. |
errorPolicy |
|
Error policy to use for the subscription. See errorPolicy |
fetchPolicy |
|
See fetchPolicy |
shouldResubscribe |
|
Boolean, or a predicate function of SubscriptionDataOptions that determines if your subscription should be unsubscribed and subscribed again |
skip |
|
If skip is true, the subscription will be skipped entirely. |
subscription |
|
GraphQL document with a single subscription. |
variables |
|
An object containing all of the variables your subscription needs to execute. |
Returns
void
documentChanged
(optional) inherited from ApolloElementInterfaceLifecycle callback that reacts to changes in the GraphQL document.
Parameters
document
DocumentNode | TypedDocumentNode | null
The GraphQL document.
Returns
void
variablesChanged
(optional) inherited from ApolloElementInterfaceLifecycle callback that reacts to changes in the operation variables.
Parameters
variables
Variables<D, V> | null
The variables.
Returns
void
Events
Name | Type | Description |
---|---|---|
apollo-element-connected |
|
when the element connects to the DOM |
apollo-element-disconnected |
|
when the element disconnects from the DOM |
apollo-error |
|
when the mutation rejects |
apollo-subscription-result |
|
when the subscription resolves |