Package com.apollographql.apollo
Class ApolloClient
- java.lang.Object
-
- com.apollographql.apollo.ApolloClient
-
- All Implemented Interfaces:
ApolloMutationCall.Factory,ApolloPrefetch.Factory,ApolloQueryCall.Factory,ApolloSubscriptionCall.Factory
public final class ApolloClient extends java.lang.Object implements ApolloQueryCall.Factory, ApolloMutationCall.Factory, ApolloPrefetch.Factory, ApolloSubscriptionCall.Factory
ApolloClient class represents the abstraction for the graphQL client that will be used to execute queries and read the responses back.ApolloClient should be shared
Since each ApolloClient holds its own connection pool and thread pool, it is recommended to only create a single ApolloClient and use that for execution of all the queries, as this would reduce latency and would also save memory. Conversely, creating a client for each query execution would result in resource wastage on idle pools.
See the
ApolloClient.Builderclass for configuring the ApolloClient.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classApolloClient.Builder
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description intactiveCallsCount()Returns the count ofApolloCall&ApolloPrefetchobjects which are currently in progress.voidaddOnSubscriptionManagerStateChangeListener(OnSubscriptionManagerStateChangeListener onStateChangeListener)Adds new listener for subscription manager state changes.com.apollographql.apollo.cache.normalized.ApolloStoreapolloStore()Deprecated.Use getApolloStore() instead.static ApolloClient.Builderbuilder()voidclearHttpCache()Clear all entries from theHttpCache, if present.booleanclearNormalizedCache()Clear all entries from the normalized cache.voidclearNormalizedCache(com.apollographql.apollo.cache.normalized.ApolloStoreOperation.Callback<java.lang.Boolean> callback)Clear all entries from the normalized cache.com.apollographql.apollo.cache.CacheHeadersdefaultCacheHeaders()Deprecated.Use getDefaultCacheHeaders() insteadvoiddisableSubscriptions()Callstopon the subscriptionManager.voidenableSubscriptions()Callstarton the subscriptionManager.com.apollographql.apollo.cache.normalized.ApolloStoregetApolloStore()java.util.List<com.apollographql.apollo.interceptor.ApolloInterceptorFactory>getApplicationInterceptorFactories()java.util.List<ApolloInterceptor>getApplicationInterceptors()com.apollographql.apollo.interceptor.ApolloInterceptorFactorygetAutoPersistedOperationsInterceptorFactory()com.apollographql.apollo.cache.CacheHeadersgetDefaultCacheHeaders()com.apollographql.apollo.api.cache.http.HttpCachegetHttpCache()com.apollographql.apollo.api.ScalarTypeAdaptersgetScalarTypeAdapters()okhttp3.HttpUrlgetServerUrl()SubscriptionManagergetSubscriptionManager()SubscriptionManagerStategetSubscriptionManagerState()Returns the current state of subscription manager.voididleCallback(IdleResourceCallback idleResourceCallback)Sets the idleResourceCallback which will be called when this ApolloClient is idle.<D extends com.apollographql.apollo.api.Operation.Data,T,V extends com.apollographql.apollo.api.Operation.Variables>
ApolloMutationCall<T>mutate(com.apollographql.apollo.api.Mutation<D,T,V> mutation)Creates and prepares a newApolloMutationCallcall.<D extends com.apollographql.apollo.api.Operation.Data,T,V extends com.apollographql.apollo.api.Operation.Variables>
ApolloMutationCall<T>mutate(com.apollographql.apollo.api.Mutation<D,T,V> mutation, D withOptimisticUpdates)Creates and prepares a newApolloMutationCallcall with optimistic updates.ApolloClient.BuildernewBuilder()<D extends com.apollographql.apollo.api.Operation.Data,T,V extends com.apollographql.apollo.api.Operation.Variables>
ApolloPrefetchprefetch(com.apollographql.apollo.api.Operation<D,T,V> operation)Creates the ApolloPrefetch by wrapping the operation object inside.<D extends com.apollographql.apollo.api.Operation.Data,T,V extends com.apollographql.apollo.api.Operation.Variables>
ApolloQueryCall<T>query(com.apollographql.apollo.api.Query<D,T,V> query)Creates and prepares a newApolloQueryCallcall.voidremoveOnSubscriptionManagerStateChangeListener(OnSubscriptionManagerStateChangeListener onStateChangeListener)Removes listener for subscription manager state changes.<D extends com.apollographql.apollo.api.Operation.Data,T,V extends com.apollographql.apollo.api.Operation.Variables>
ApolloSubscriptionCall<T>subscribe(com.apollographql.apollo.api.Subscription<D,T,V> subscription)Creates and prepares a newApolloSubscriptionCallcall.
-
-
-
Method Detail
-
builder
public static ApolloClient.Builder builder()
-
mutate
public <D extends com.apollographql.apollo.api.Operation.Data,T,V extends com.apollographql.apollo.api.Operation.Variables> ApolloMutationCall<T> mutate(@NotNull com.apollographql.apollo.api.Mutation<D,T,V> mutation)
Description copied from interface:ApolloMutationCall.FactoryCreates and prepares a newApolloMutationCallcall.- Specified by:
mutatein interfaceApolloMutationCall.Factory- Parameters:
mutation- theMutationwhich needs to be performed- Returns:
- prepared
ApolloMutationCallcall to be executed at some point in the future
-
mutate
public <D extends com.apollographql.apollo.api.Operation.Data,T,V extends com.apollographql.apollo.api.Operation.Variables> ApolloMutationCall<T> mutate(@NotNull com.apollographql.apollo.api.Mutation<D,T,V> mutation, @NotNull D withOptimisticUpdates)
Description copied from interface:ApolloMutationCall.FactoryCreates and prepares a new
Provided optimistic updates will be stored inApolloMutationCallcall with optimistic updates.ApolloStoreimmediately before mutation execution. AnyApolloQueryWatcherdependent on the changed cache records will be re-fetched.- Specified by:
mutatein interfaceApolloMutationCall.Factory- Parameters:
mutation- theMutationwhich needs to be performedwithOptimisticUpdates- optimistic updates for this mutation- Returns:
- prepared
ApolloMutationCallcall to be executed at some point in the future
-
query
public <D extends com.apollographql.apollo.api.Operation.Data,T,V extends com.apollographql.apollo.api.Operation.Variables> ApolloQueryCall<T> query(@NotNull com.apollographql.apollo.api.Query<D,T,V> query)
Description copied from interface:ApolloQueryCall.FactoryCreates and prepares a newApolloQueryCallcall.- Specified by:
queryin interfaceApolloQueryCall.Factory- Parameters:
query- the operation which needs to be performed- Returns:
- prepared
ApolloQueryCallcall to be executed at some point in the future
-
prefetch
public <D extends com.apollographql.apollo.api.Operation.Data,T,V extends com.apollographql.apollo.api.Operation.Variables> ApolloPrefetch prefetch(@NotNull com.apollographql.apollo.api.Operation<D,T,V> operation)
Description copied from interface:ApolloPrefetch.FactoryCreates the ApolloPrefetch by wrapping the operation object inside.- Specified by:
prefetchin interfaceApolloPrefetch.Factory- Parameters:
operation- the operation which needs to be performed- Returns:
- The ApolloPrefetch object with the wrapped operation object
-
subscribe
public <D extends com.apollographql.apollo.api.Operation.Data,T,V extends com.apollographql.apollo.api.Operation.Variables> ApolloSubscriptionCall<T> subscribe(@NotNull com.apollographql.apollo.api.Subscription<D,T,V> subscription)
Description copied from interface:ApolloSubscriptionCall.FactoryCreates and prepares a newApolloSubscriptionCallcall.- Specified by:
subscribein interfaceApolloSubscriptionCall.Factory- Parameters:
subscription- to be sent to the subscription server to start listening pushed updates- Returns:
- prepared
ApolloSubscriptionCallcall to be executed
-
addOnSubscriptionManagerStateChangeListener
public void addOnSubscriptionManagerStateChangeListener(@NotNull OnSubscriptionManagerStateChangeListener onStateChangeListener)Adds new listener for subscription manager state changes.- Parameters:
onStateChangeListener- to be called when state changed
-
removeOnSubscriptionManagerStateChangeListener
public void removeOnSubscriptionManagerStateChangeListener(@NotNull OnSubscriptionManagerStateChangeListener onStateChangeListener)Removes listener for subscription manager state changes.- Parameters:
onStateChangeListener- to remove
-
getSubscriptionManagerState
public SubscriptionManagerState getSubscriptionManagerState()
Returns the current state of subscription manager.- Returns:
- current state
-
getSubscriptionManager
public SubscriptionManager getSubscriptionManager()
-
enableSubscriptions
public void enableSubscriptions()
Callstarton the subscriptionManager. Which will put the subscriptionManager in a connectible state if its current state is STOPPED. This is a noop if the current state is anything other than STOPPED.When subscriptions are re-enabled after having been disabled, the underlying transport isn't reconnected immediately, but will be on the first new subscription created.
-
disableSubscriptions
public void disableSubscriptions()
Callstopon the subscriptionManager. Which will unsubscribe from all active subscriptions, disconnect the underlying transport (eg websocket), and put the subscriptionManager in the STOPPED state.New subscriptions will fail until
enableSubscriptions()is called.
-
defaultCacheHeaders
@Deprecated public com.apollographql.apollo.cache.CacheHeaders defaultCacheHeaders()
Deprecated.Use getDefaultCacheHeaders() instead- Returns:
- The default
CacheHeaderswhich this instance ofApolloClientwas configured.
-
getDefaultCacheHeaders
public com.apollographql.apollo.cache.CacheHeaders getDefaultCacheHeaders()
- Returns:
- The default
CacheHeaderswhich this instance ofApolloClientwas configured.
-
clearHttpCache
public void clearHttpCache()
Clear all entries from theHttpCache, if present.
-
clearNormalizedCache
public void clearNormalizedCache(@NotNull com.apollographql.apollo.cache.normalized.ApolloStoreOperation.Callback<java.lang.Boolean> callback)Clear all entries from the normalized cache. This is asynchronous operation and will be scheduled on the dispatcher- Parameters:
callback- to be notified when operation is completed
-
clearNormalizedCache
public boolean clearNormalizedCache()
Clear all entries from the normalized cache. This is synchronous operation and will be executed int the current thread- Returns:
trueif operation succeed,falseotherwise
-
apolloStore
@Deprecated public com.apollographql.apollo.cache.normalized.ApolloStore apolloStore()
Deprecated.Use getApolloStore() instead.- Returns:
- The
ApolloStoremanaging access to the normalized cache created byApolloClient.Builder.normalizedCache(NormalizedCacheFactory, CacheKeyResolver)}
-
getApolloStore
public com.apollographql.apollo.cache.normalized.ApolloStore getApolloStore()
- Returns:
- The
ApolloStoremanaging access to the normalized cache created byApolloClient.Builder.normalizedCache(NormalizedCacheFactory, CacheKeyResolver)}
-
getServerUrl
public okhttp3.HttpUrl getServerUrl()
- Returns:
- The
HttpUrlserverUrl
-
getHttpCache
public com.apollographql.apollo.api.cache.http.HttpCache getHttpCache()
- Returns:
- The
HttpCachehttpCache
-
getScalarTypeAdapters
public com.apollographql.apollo.api.ScalarTypeAdapters getScalarTypeAdapters()
- Returns:
- The
ScalarTypeAdaptersscalarTypeAdapters
-
getApplicationInterceptors
public java.util.List<ApolloInterceptor> getApplicationInterceptors()
- Returns:
- The list of
ApolloInterceptors
-
getApplicationInterceptorFactories
public java.util.List<com.apollographql.apollo.interceptor.ApolloInterceptorFactory> getApplicationInterceptorFactories()
- Returns:
- The list of
ApolloInterceptorFactory
-
getAutoPersistedOperationsInterceptorFactory
public com.apollographql.apollo.interceptor.ApolloInterceptorFactory getAutoPersistedOperationsInterceptorFactory()
- Returns:
- The
ApolloInterceptorused for auto persisted operations
-
idleCallback
public void idleCallback(IdleResourceCallback idleResourceCallback)
Sets the idleResourceCallback which will be called when this ApolloClient is idle.
-
activeCallsCount
public int activeCallsCount()
Returns the count ofApolloCall&ApolloPrefetchobjects which are currently in progress.
-
newBuilder
public ApolloClient.Builder newBuilder()
- Returns:
- a new instance of
ApolloClient.Builderto customize an existingApolloClient
-
-