diff --git a/apollo-normalized-cache/api/apollo-normalized-cache.api b/apollo-normalized-cache/api/apollo-normalized-cache.api index 5a750e2715..6490d4f4c8 100644 --- a/apollo-normalized-cache/api/apollo-normalized-cache.api +++ b/apollo-normalized-cache/api/apollo-normalized-cache.api @@ -20,23 +20,20 @@ public final class com/apollographql/apollo3/cache/normalized/ClientCacheExtensi public static final fun cacheHeaders (Lcom/apollographql/apollo3/api/HasMutableExecutionContext;Lcom/apollographql/apollo3/cache/CacheHeaders;)Lcom/apollographql/apollo3/api/HasMutableExecutionContext; public static final fun clearNormalizedCache (Lcom/apollographql/apollo3/ApolloClient;)Z public static final fun doNotStore (Lcom/apollographql/apollo3/api/HasMutableExecutionContext;Z)Lcom/apollographql/apollo3/api/HasMutableExecutionContext; - public static final fun fetchPolicy (Lcom/apollographql/apollo3/ApolloClient$Builder;Lcom/apollographql/apollo3/cache/normalized/FetchPolicy;)Lcom/apollographql/apollo3/ApolloClient$Builder; - public static final fun fetchPolicy (Lcom/apollographql/apollo3/api/ApolloRequest$Builder;Lcom/apollographql/apollo3/cache/normalized/FetchPolicy;)Lcom/apollographql/apollo3/api/ApolloRequest$Builder; + public static final fun executeCacheAndNetwork (Lcom/apollographql/apollo3/ApolloQueryCall;)Lkotlinx/coroutines/flow/Flow; + public static final fun fetchPolicy (Lcom/apollographql/apollo3/api/HasMutableExecutionContext;Lcom/apollographql/apollo3/cache/normalized/FetchPolicy;)Lcom/apollographql/apollo3/api/HasMutableExecutionContext; public static final fun getApolloStore (Lcom/apollographql/apollo3/ApolloClient;)Lcom/apollographql/apollo3/cache/normalized/ApolloStore; public static final fun getCacheInfo (Lcom/apollographql/apollo3/api/ApolloResponse;)Lcom/apollographql/apollo3/cache/normalized/CacheInfo; public static final fun isFromCache (Lcom/apollographql/apollo3/api/ApolloResponse;)Z public static final fun normalizedCache (Lcom/apollographql/apollo3/ApolloClient$Builder;Lcom/apollographql/apollo3/cache/normalized/NormalizedCacheFactory;Lcom/apollographql/apollo3/cache/normalized/ObjectIdGenerator;Lcom/apollographql/apollo3/cache/normalized/CacheResolver;Z)Lcom/apollographql/apollo3/ApolloClient$Builder; public static synthetic fun normalizedCache$default (Lcom/apollographql/apollo3/ApolloClient$Builder;Lcom/apollographql/apollo3/cache/normalized/NormalizedCacheFactory;Lcom/apollographql/apollo3/cache/normalized/ObjectIdGenerator;Lcom/apollographql/apollo3/cache/normalized/CacheResolver;ZILjava/lang/Object;)Lcom/apollographql/apollo3/ApolloClient$Builder; + public static final fun optimisticUpdates (Lcom/apollographql/apollo3/ApolloMutationCall;Lcom/apollographql/apollo3/api/Mutation$Data;)Lcom/apollographql/apollo3/ApolloMutationCall; public static final fun optimisticUpdates (Lcom/apollographql/apollo3/api/ApolloRequest$Builder;Lcom/apollographql/apollo3/api/Mutation$Data;)Lcom/apollographql/apollo3/api/ApolloRequest$Builder; - public static final fun queryCacheAndNetwork (Lcom/apollographql/apollo3/ApolloClient;Lcom/apollographql/apollo3/api/ApolloRequest;)Lkotlinx/coroutines/flow/Flow; - public static final fun queryCacheAndNetwork (Lcom/apollographql/apollo3/ApolloClient;Lcom/apollographql/apollo3/api/Query;)Lkotlinx/coroutines/flow/Flow; - public static final fun refetchPolicy (Lcom/apollographql/apollo3/ApolloClient$Builder;Lcom/apollographql/apollo3/cache/normalized/FetchPolicy;)Lcom/apollographql/apollo3/ApolloClient$Builder; - public static final fun refetchPolicy (Lcom/apollographql/apollo3/api/ApolloRequest$Builder;Lcom/apollographql/apollo3/cache/normalized/FetchPolicy;)Lcom/apollographql/apollo3/api/ApolloRequest$Builder; + public static final fun refetchPolicy (Lcom/apollographql/apollo3/api/HasMutableExecutionContext;Lcom/apollographql/apollo3/cache/normalized/FetchPolicy;)Lcom/apollographql/apollo3/api/HasMutableExecutionContext; public static final fun store (Lcom/apollographql/apollo3/ApolloClient$Builder;Lcom/apollographql/apollo3/cache/normalized/ApolloStore;Z)Lcom/apollographql/apollo3/ApolloClient$Builder; public static synthetic fun store$default (Lcom/apollographql/apollo3/ApolloClient$Builder;Lcom/apollographql/apollo3/cache/normalized/ApolloStore;ZILjava/lang/Object;)Lcom/apollographql/apollo3/ApolloClient$Builder; public static final fun storePartialResponses (Lcom/apollographql/apollo3/api/HasMutableExecutionContext;Z)Lcom/apollographql/apollo3/api/HasMutableExecutionContext; - public static final fun watch (Lcom/apollographql/apollo3/ApolloClient;Lcom/apollographql/apollo3/api/ApolloRequest;)Lkotlinx/coroutines/flow/Flow; - public static final fun watch (Lcom/apollographql/apollo3/ApolloClient;Lcom/apollographql/apollo3/api/Query;)Lkotlinx/coroutines/flow/Flow; + public static final fun watch (Lcom/apollographql/apollo3/ApolloQueryCall;)Lkotlinx/coroutines/flow/Flow; public static final fun withCacheHeaders (Lcom/apollographql/apollo3/ApolloClient;Lcom/apollographql/apollo3/cache/CacheHeaders;)Lcom/apollographql/apollo3/ApolloClient; public static final fun withCacheHeaders (Lcom/apollographql/apollo3/api/ApolloRequest;Lcom/apollographql/apollo3/cache/CacheHeaders;)Lcom/apollographql/apollo3/api/ApolloRequest; public static final fun withDoNotStore (Lcom/apollographql/apollo3/ApolloClient;Z)Lcom/apollographql/apollo3/ApolloClient; diff --git a/apollo-normalized-cache/src/commonMain/kotlin/com/apollographql/apollo3/cache/normalized/ClientCacheExtensions.kt b/apollo-normalized-cache/src/commonMain/kotlin/com/apollographql/apollo3/cache/normalized/ClientCacheExtensions.kt index 80f496a905..6f19413365 100644 --- a/apollo-normalized-cache/src/commonMain/kotlin/com/apollographql/apollo3/cache/normalized/ClientCacheExtensions.kt +++ b/apollo-normalized-cache/src/commonMain/kotlin/com/apollographql/apollo3/cache/normalized/ClientCacheExtensions.kt @@ -1,6 +1,8 @@ package com.apollographql.apollo3.cache.normalized import com.apollographql.apollo3.ApolloClient +import com.apollographql.apollo3.ApolloMutationCall +import com.apollographql.apollo3.ApolloQueryCall import com.apollographql.apollo3.api.ApolloRequest import com.apollographql.apollo3.api.ApolloResponse import com.apollographql.apollo3.api.ExecutionContext @@ -64,16 +66,9 @@ fun ApolloClient.Builder.store(store: ApolloStore, writeToCacheAsynchronously: B return addInterceptor(ApolloCacheInterceptor(store)).writeToCacheAsynchronously(writeToCacheAsynchronously) } -fun ApolloClient.watch(query: Query): Flow> { - return watch(ApolloRequest.Builder(query).build()) -} - -fun ApolloClient.watch(queryRequest: ApolloRequest): Flow> { - return queryAsFlow(queryRequest.newBuilder().addExecutionContext(WatchContext(true)).build()) -} -fun ApolloClient.queryCacheAndNetwork(query: Query): Flow> { - return queryCacheAndNetwork(ApolloRequest.Builder(query).build()) +fun ApolloQueryCall.watch(): Flow> { + return copy().addExecutionContext(WatchContext(true)).executeAsFlow() } @@ -83,18 +78,18 @@ fun ApolloClient.queryCacheAndNetwork(query: Query): Flow ApolloClient.queryCacheAndNetwork(queryRequest: ApolloRequest): Flow> { +fun ApolloQueryCall.executeCacheAndNetwork(): Flow> { return flow { var cacheException: ApolloException? = null var networkException: ApolloException? = null try { - emit(query(queryRequest.newBuilder().fetchPolicy(FetchPolicy.CacheOnly).build())) + emit(copy().fetchPolicy(FetchPolicy.CacheOnly).execute()) } catch (e: ApolloException) { cacheException = e } try { - emit(query(queryRequest.newBuilder().fetchPolicy(FetchPolicy.NetworkOnly).build())) + emit(copy().fetchPolicy(FetchPolicy.NetworkOnly).execute()) } catch (e: ApolloException) { networkException = e } @@ -122,48 +117,33 @@ val ApolloClient.apolloStore: ApolloStore fun ApolloClient.clearNormalizedCache() = apolloStore.clearAll() /** - * Sets the [FetchPolicy] on this request. D has a bound on [Query.Data] because subscriptions and mutation shouldn't - * read the cache - */ -fun ApolloRequest.Builder.fetchPolicy(fetchPolicy: FetchPolicy) = addExecutionContext( - FetchPolicyContext(fetchPolicy) -) - -/** - * Sets the default [FetchPolicy] for the [ApolloClient]. This only affects queries. Mutations and subscriptions will - * always use [FetchPolicy.NetworkFirst] + * Sets the initial [FetchPolicy] + * This only has effects for queries. Mutations and subscriptions always use [FetchPolicy.NetworkOnly] */ -fun ApolloClient.Builder.fetchPolicy(fetchPolicy: FetchPolicy) = addExecutionContext( +fun > HasMutableExecutionContext.fetchPolicy(fetchPolicy: FetchPolicy) = addExecutionContext( FetchPolicyContext(fetchPolicy) ) /** - * Sets the [FetchPolicy] used when refetching at the request level. This is only used in combination with [watch]. + * Sets the [FetchPolicy] used when watching queries and a cache change has been published */ -fun ApolloRequest.Builder.refetchPolicy(refetchPolicy: FetchPolicy) = addExecutionContext( - RefetchPolicyContext(refetchPolicy) +fun > HasMutableExecutionContext.refetchPolicy(fetchPolicy: FetchPolicy) = addExecutionContext( + RefetchPolicyContext(fetchPolicy) ) -/** - * Sets the [FetchPolicy] used when refetching at the client level. This is only used in combination with [watch]. - */ -fun ApolloClient.Builder.refetchPolicy(refetchPolicy: FetchPolicy) = addExecutionContext( - RefetchPolicyContext(refetchPolicy) -) - -fun HasMutableExecutionContext.doNotStore(doNotStore: Boolean) where T : HasMutableExecutionContext = addExecutionContext( +fun > HasMutableExecutionContext.doNotStore(doNotStore: Boolean) = addExecutionContext( DoNotStoreContext(doNotStore) ) -fun HasMutableExecutionContext.storePartialResponses(storePartialResponses: Boolean) where T : HasMutableExecutionContext = addExecutionContext( +fun > HasMutableExecutionContext.storePartialResponses(storePartialResponses: Boolean) = addExecutionContext( StorePartialResponsesContext(storePartialResponses) ) -fun HasMutableExecutionContext.cacheHeaders(cacheHeaders: CacheHeaders) where T : HasMutableExecutionContext = addExecutionContext( +fun > HasMutableExecutionContext.cacheHeaders(cacheHeaders: CacheHeaders) = addExecutionContext( CacheHeadersContext(cacheHeaders) ) -fun HasMutableExecutionContext.writeToCacheAsynchronously(writeToCacheAsynchronously: Boolean) where T : HasMutableExecutionContext = addExecutionContext( +fun > HasMutableExecutionContext.writeToCacheAsynchronously(writeToCacheAsynchronously: Boolean) = addExecutionContext( WriteToCacheAsynchronouslyContext(writeToCacheAsynchronously) ) @@ -173,6 +153,9 @@ fun HasMutableExecutionContext.writeToCacheAsynchronously(writeToCacheAsy fun ApolloRequest.Builder.optimisticUpdates(data: D) = addExecutionContext( OptimisticUpdatesContext(data) ) +fun ApolloMutationCall.optimisticUpdates(data: D) = addExecutionContext( + OptimisticUpdatesContext(data) +) internal val ApolloRequest.fetchPolicy get() = executionContext[FetchPolicyContext]?.value ?: FetchPolicy.CacheFirst diff --git a/apollo-runtime/api/apollo-runtime.api b/apollo-runtime/api/apollo-runtime.api index 828a5ad7b0..1b9a03874b 100644 --- a/apollo-runtime/api/apollo-runtime.api +++ b/apollo-runtime/api/apollo-runtime.api @@ -1,3 +1,13 @@ +public abstract class com/apollographql/apollo3/ApolloCall : com/apollographql/apollo3/api/HasMutableExecutionContext { + public fun (Lcom/apollographql/apollo3/ApolloClient;Lcom/apollographql/apollo3/api/Operation;)V + public fun addExecutionContext (Lcom/apollographql/apollo3/api/ExecutionContext;)Lcom/apollographql/apollo3/api/HasMutableExecutionContext; + public final fun executeAsFlow ()Lkotlinx/coroutines/flow/Flow; + public final fun getApolloClient ()Lcom/apollographql/apollo3/ApolloClient; + public fun getExecutionContext ()Lcom/apollographql/apollo3/api/ExecutionContext; + public final fun getOperation ()Lcom/apollographql/apollo3/api/Operation; + public fun setExecutionContext (Lcom/apollographql/apollo3/api/ExecutionContext;)V +} + public final class com/apollographql/apollo3/ApolloClient : com/apollographql/apollo3/api/HasExecutionContext { public static final field Companion Lcom/apollographql/apollo3/ApolloClient$Companion; public fun (Lcom/apollographql/apollo3/network/NetworkTransport;)V @@ -10,17 +20,13 @@ public final class com/apollographql/apollo3/ApolloClient : com/apollographql/ap public synthetic fun (Lcom/apollographql/apollo3/network/NetworkTransport;Lcom/apollographql/apollo3/api/CustomScalarAdapters;Lcom/apollographql/apollo3/network/NetworkTransport;Ljava/util/List;Lcom/apollographql/apollo3/api/ExecutionContext;Lkotlinx/coroutines/CoroutineDispatcher;Ljava/util/List;ILkotlin/jvm/internal/DefaultConstructorMarker;)V public fun (Ljava/lang/String;)V public final fun dispose ()V + public final fun executeAsFlow (Lcom/apollographql/apollo3/api/ApolloRequest;)Lkotlinx/coroutines/flow/Flow; public fun getExecutionContext ()Lcom/apollographql/apollo3/api/ExecutionContext; public final fun getInterceptors ()Ljava/util/List; - public final fun mutate (Lcom/apollographql/apollo3/api/ApolloRequest;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; - public final fun mutate (Lcom/apollographql/apollo3/api/Mutation;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; - public final fun mutateAsFlow (Lcom/apollographql/apollo3/api/ApolloRequest;)Lkotlinx/coroutines/flow/Flow; + public final fun mutate (Lcom/apollographql/apollo3/api/Mutation;)Lcom/apollographql/apollo3/ApolloMutationCall; public final fun newBuilder ()Lcom/apollographql/apollo3/ApolloClient$Builder; - public final fun query (Lcom/apollographql/apollo3/api/ApolloRequest;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; - public final fun query (Lcom/apollographql/apollo3/api/Query;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; - public final fun queryAsFlow (Lcom/apollographql/apollo3/api/ApolloRequest;)Lkotlinx/coroutines/flow/Flow; - public final fun subscribe (Lcom/apollographql/apollo3/api/ApolloRequest;)Lkotlinx/coroutines/flow/Flow; - public final fun subscribe (Lcom/apollographql/apollo3/api/Subscription;)Lkotlinx/coroutines/flow/Flow; + public final fun query (Lcom/apollographql/apollo3/api/Query;)Lcom/apollographql/apollo3/ApolloQueryCall; + public final fun subscribe (Lcom/apollographql/apollo3/api/Subscription;)Lcom/apollographql/apollo3/ApolloSubscriptionCall; public final fun withCustomScalarAdapter (Lcom/apollographql/apollo3/api/CustomScalarType;Lcom/apollographql/apollo3/api/Adapter;)Lcom/apollographql/apollo3/ApolloClient; public final fun withExecutionContext (Lcom/apollographql/apollo3/api/ExecutionContext;)Lcom/apollographql/apollo3/ApolloClient; public final fun withFlowDecorator (Lkotlin/jvm/functions/Function1;)Lcom/apollographql/apollo3/ApolloClient; @@ -61,6 +67,24 @@ public final class com/apollographql/apollo3/ApolloClientKt { public static final fun withSendDocument (Lcom/apollographql/apollo3/ApolloClient;Z)Lcom/apollographql/apollo3/ApolloClient; } +public final class com/apollographql/apollo3/ApolloMutationCall : com/apollographql/apollo3/ApolloCall { + public fun (Lcom/apollographql/apollo3/ApolloClient;Lcom/apollographql/apollo3/api/Mutation;)V + public final fun copy ()Lcom/apollographql/apollo3/ApolloMutationCall; + public final fun execute (Lkotlin/coroutines/Continuation;)Ljava/lang/Object; +} + +public final class com/apollographql/apollo3/ApolloQueryCall : com/apollographql/apollo3/ApolloCall { + public fun (Lcom/apollographql/apollo3/ApolloClient;Lcom/apollographql/apollo3/api/Query;)V + public final fun copy ()Lcom/apollographql/apollo3/ApolloQueryCall; + public final fun execute (Lkotlin/coroutines/Continuation;)Ljava/lang/Object; +} + +public final class com/apollographql/apollo3/ApolloSubscriptionCall : com/apollographql/apollo3/ApolloCall { + public fun (Lcom/apollographql/apollo3/ApolloClient;Lcom/apollographql/apollo3/api/Subscription;)V + public final fun copy ()Lcom/apollographql/apollo3/ApolloSubscriptionCall; + public final fun execute ()Lkotlinx/coroutines/flow/Flow; +} + public final class com/apollographql/apollo3/AutoPersistedQueryInfo : com/apollographql/apollo3/api/ExecutionContext$Element { public static final field Key Lcom/apollographql/apollo3/AutoPersistedQueryInfo$Key; public fun (Z)V diff --git a/apollo-runtime/src/commonMain/kotlin/com/apollographql/apollo3/ApolloCall.kt b/apollo-runtime/src/commonMain/kotlin/com/apollographql/apollo3/ApolloCall.kt new file mode 100644 index 0000000000..04981a3228 --- /dev/null +++ b/apollo-runtime/src/commonMain/kotlin/com/apollographql/apollo3/ApolloCall.kt @@ -0,0 +1,115 @@ +package com.apollographql.apollo3 + +import com.apollographql.apollo3.api.ApolloRequest +import com.apollographql.apollo3.api.ApolloResponse +import com.apollographql.apollo3.api.ExecutionContext +import com.apollographql.apollo3.api.HasMutableExecutionContext +import com.apollographql.apollo3.api.Mutation +import com.apollographql.apollo3.api.Operation +import com.apollographql.apollo3.api.Query +import com.apollographql.apollo3.api.Subscription +import kotlinx.coroutines.flow.Flow +import kotlinx.coroutines.flow.single + + +abstract class ApolloCall> +(val apolloClient: ApolloClient, val operation: Operation): HasMutableExecutionContext { + override var executionContext: ExecutionContext = ExecutionContext.Empty + + override fun addExecutionContext(executionContext: ExecutionContext): E { + this.executionContext += executionContext + return this as E + } + + fun executeAsFlow(): Flow> { + val request = ApolloRequest.Builder(operation) + .addExecutionContext(executionContext) + .build() + return apolloClient.executeAsFlow(request) + } +} + +/** + * [ApolloQueryCall] contains everything needed to execute a [Query] with the given [ApolloClient] + * + * [ApolloQueryCall] is mutable. You can customize it before calling [execute] + */ +class ApolloQueryCall(apolloClient: ApolloClient, query: Query) + : ApolloCall>(apolloClient, query) { + /** + * Executes the [ApolloQueryCall] + * [ApolloQueryCall] can be executed several times + * + * Example: + * ``` + * val response = apolloClient.query(HeroQuery()) + * .httpHeader("Authorization", myToken) + * .fetchPolicy(FetchPolicy.NetworkOnly) + * .execute() + * ``` + */ + suspend fun execute(): ApolloResponse { + return executeAsFlow().single() + } + + fun copy(): ApolloQueryCall { + return ApolloQueryCall(apolloClient, operation as Query).addExecutionContext(executionContext) + } +} + +/** + * [ApolloMutationCall] contains everything needed to execute a [Mutation] with the given [ApolloClient] + * + * [ApolloMutationCall] is mutable. You can customize it before calling [execute] + */ +class ApolloMutationCall(apolloClient: ApolloClient, mutation: Mutation) + : ApolloCall>(apolloClient, mutation) { + /** + * Executes the [ApolloMutationCall] + * [ApolloMutationCall] can be executed several times + * + * Example: + * ``` + * val response = apolloClient.mutate(SetHeroName("Luke")) + * .httpHeader("Authorization", myToken) + * .optimisticData(data) + * .execute() + * ``` + */ + suspend fun execute(): ApolloResponse { + return executeAsFlow().single() + } + + fun copy(): ApolloMutationCall { + return ApolloMutationCall(apolloClient, operation as Mutation).addExecutionContext(executionContext) + } +} + +/** + * [ApolloSubscriptionCall] contains everything needed to execute a [Subscription] with the given [ApolloClient] + * + * [ApolloSubscriptionCall] is mutable. You can customize it before calling [execute] + */ +class ApolloSubscriptionCall(apolloClient: ApolloClient, subscription: Subscription) + : ApolloCall>(apolloClient, subscription) { + /** + * Executes the [ApolloSubscriptionCall] + * [ApolloSubscriptionCall] can be executed several times + * + * Example: + * ``` + * apolloClient.subscribe(NewOrders()) + * .execute() + * .collect { + * println("order received: ${it.data?.order?.id"}) + * } + * ``` + */ + fun execute(): Flow> { + return executeAsFlow() + } + + fun copy(): ApolloSubscriptionCall { + return ApolloSubscriptionCall(apolloClient, operation as Subscription).addExecutionContext(executionContext) + } +} \ No newline at end of file diff --git a/apollo-runtime/src/commonMain/kotlin/com/apollographql/apollo3/ApolloClient.kt b/apollo-runtime/src/commonMain/kotlin/com/apollographql/apollo3/ApolloClient.kt index cacdcd7a7e..a539576d03 100644 --- a/apollo-runtime/src/commonMain/kotlin/com/apollographql/apollo3/ApolloClient.kt +++ b/apollo-runtime/src/commonMain/kotlin/com/apollographql/apollo3/ApolloClient.kt @@ -34,7 +34,6 @@ import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.cancel import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.flowOn -import kotlinx.coroutines.flow.single import kotlin.jvm.JvmOverloads typealias FlowDecorator = (Flow>) -> Flow> @@ -67,57 +66,24 @@ class ApolloClient @JvmOverloads @Deprecated("Please use ApolloClient.Builder in ) /** - * Executes the given query and returns a response or throws on transport errors - * use [query] ([ApolloRequest]) to customize the request + * Creates a new [ApolloQueryCall] that you can customize and/or execute. */ - suspend fun query(query: Query): ApolloResponse = query(ApolloRequest.Builder(query).build()) - - /** - * Executes the given mutation and returns a response or throws on transport errors - * use [mutation] ([ApolloRequest]) to customize the request - */ - suspend fun mutate(mutation: Mutation): ApolloResponse = mutate(ApolloRequest.Builder(mutation).build()) - - /** - * Subscribes to the given subscription. The subscription is cancelled when the coroutine collecting the flow is canceled - */ - fun subscribe(subscription: Subscription): Flow> = subscribe(ApolloRequest.Builder(subscription).build()) - - /** - * Executes the given queryRequest and returns a response or throws on transport errors - */ - suspend fun query(queryRequest: ApolloRequest): ApolloResponse { - return queryRequest.execute().single() - } - - /** - * Executes the given queryRequest and returns a Flow of responses. - * - * It is used by [watch] when multiple responses are expected in response to a single query - */ - fun queryAsFlow(queryRequest: ApolloRequest): Flow> { - return queryRequest.execute() - } - - /** - * Executes the given mutationRequest and returns a response or throws on transport errors - */ - suspend fun mutate(mutationRequest: ApolloRequest): ApolloResponse { - return mutationRequest.execute().single() + fun query(query: Query): ApolloQueryCall { + return ApolloQueryCall(this, query) } /** - * Executes the given mutationRequest and returns a Flow of response or throws on transport errors + * Creates a new [ApolloMutationCall] that you can customize and/or execute. */ - fun mutateAsFlow(mutationRequest: ApolloRequest): Flow> { - return mutationRequest.execute() + fun mutate(mutation: Mutation): ApolloMutationCall { + return ApolloMutationCall(this, mutation) } /** - * Executes the given subscriptionRequest and returns a response or throws on transport errors + * Creates a new [ApolloSubscriptionCall] that you can customize and/or execute. */ - fun subscribe(subscriptionRequest: ApolloRequest): Flow> { - return subscriptionRequest.execute() + fun subscribe(subscription: Subscription): ApolloSubscriptionCall { + return ApolloSubscriptionCall(this, subscription) } fun dispose() { @@ -176,12 +142,23 @@ class ApolloClient @JvmOverloads @Deprecated("Please use ApolloClient.Builder in ) } + /** + * Low level API to execute the given [apolloRequest] and return a [Flow]. + * + * Prefer [query], [mutate] or [subscribe] when possible. + * + * For simple queries, the returned [Flow] will contain only one element. + * For more advanced use cases like watchers or subscriptions, it may contain any number of elements and never + * finish. You can cancel the corresponding coroutine to terminate the [Flow] in this case. + * + * + */ @OptIn(ExperimentalCoroutinesApi::class, kotlinx.coroutines.FlowPreview::class) - private fun ApolloRequest.execute(): Flow> { + fun executeAsFlow(apolloRequest: ApolloRequest): Flow> { assertMainThreadOnNative() - val executionContext = clientScope + customScalarAdapters + this@ApolloClient.executionContext + this.executionContext + val executionContext = clientScope + customScalarAdapters + executionContext + apolloRequest.executionContext - val request = newBuilder().addExecutionContext(executionContext).build() + val request = apolloRequest.newBuilder().addExecutionContext(executionContext).build() // ensureNeverFrozen(request) val interceptors = interceptors + NetworkInterceptor( networkTransport = networkTransport, diff --git a/apollo-rx2-support/api/apollo-rx2-support.api b/apollo-rx2-support/api/apollo-rx2-support.api index e9cb0eec58..71baa75654 100644 --- a/apollo-rx2-support/api/apollo-rx2-support.api +++ b/apollo-rx2-support/api/apollo-rx2-support.api @@ -1,14 +1,25 @@ -public final class com/apollographql/apollo3/rx2/Rx2ApolloClient { - public fun (Lcom/apollographql/apollo3/ApolloClient;Lio/reactivex/Scheduler;)V - public final fun dispose ()V - public final fun mutate (Lcom/apollographql/apollo3/api/ApolloRequest;)Lio/reactivex/Single; - public final fun mutate (Lcom/apollographql/apollo3/api/Mutation;)Lio/reactivex/Single; - public final fun mutateAsFlow (Lcom/apollographql/apollo3/api/ApolloRequest;)Lio/reactivex/Flowable; - public final fun query (Lcom/apollographql/apollo3/api/ApolloRequest;)Lio/reactivex/Single; - public final fun query (Lcom/apollographql/apollo3/api/Query;)Lio/reactivex/Single; - public final fun queryAsFlow (Lcom/apollographql/apollo3/api/ApolloRequest;)Lio/reactivex/Flowable; - public final fun subscribe (Lcom/apollographql/apollo3/api/ApolloRequest;)Lio/reactivex/Flowable; - public final fun subscribe (Lcom/apollographql/apollo3/api/Subscription;)Lio/reactivex/Flowable; +public final class com/apollographql/apollo3/rx2/Rx2Apollo { + public static final fun rxFlowable (Lcom/apollographql/apollo3/ApolloSubscriptionCall;)Lio/reactivex/Flowable; + public static final fun rxFlowable (Lcom/apollographql/apollo3/ApolloSubscriptionCall;Lio/reactivex/Scheduler;)Lio/reactivex/Flowable; + public static synthetic fun rxFlowable$default (Lcom/apollographql/apollo3/ApolloSubscriptionCall;Lio/reactivex/Scheduler;ILjava/lang/Object;)Lio/reactivex/Flowable; + public static final fun rxSingle (Lcom/apollographql/apollo3/ApolloMutationCall;)Lio/reactivex/Single; + public static final fun rxSingle (Lcom/apollographql/apollo3/ApolloMutationCall;Lio/reactivex/Scheduler;)Lio/reactivex/Single; + public static final fun rxSingle (Lcom/apollographql/apollo3/ApolloQueryCall;)Lio/reactivex/Single; + public static final fun rxSingle (Lcom/apollographql/apollo3/ApolloQueryCall;Lio/reactivex/Scheduler;)Lio/reactivex/Single; + public static synthetic fun rxSingle$default (Lcom/apollographql/apollo3/ApolloMutationCall;Lio/reactivex/Scheduler;ILjava/lang/Object;)Lio/reactivex/Single; + public static synthetic fun rxSingle$default (Lcom/apollographql/apollo3/ApolloQueryCall;Lio/reactivex/Scheduler;ILjava/lang/Object;)Lio/reactivex/Single; + public static final fun toRx2ApolloInterceptor (Lcom/apollographql/apollo3/interceptor/ApolloInterceptor;Lio/reactivex/Scheduler;)Lcom/apollographql/apollo3/rx2/Rx2ApolloInterceptor; + public static synthetic fun toRx2ApolloInterceptor$default (Lcom/apollographql/apollo3/interceptor/ApolloInterceptor;Lio/reactivex/Scheduler;ILjava/lang/Object;)Lcom/apollographql/apollo3/rx2/Rx2ApolloInterceptor; + public static final fun toRx2ApolloInterceptorChain (Lcom/apollographql/apollo3/interceptor/ApolloInterceptorChain;Lio/reactivex/Scheduler;)Lcom/apollographql/apollo3/rx2/Rx2ApolloInterceptorChain; + public static synthetic fun toRx2ApolloInterceptorChain$default (Lcom/apollographql/apollo3/interceptor/ApolloInterceptorChain;Lio/reactivex/Scheduler;ILjava/lang/Object;)Lcom/apollographql/apollo3/rx2/Rx2ApolloInterceptorChain; + public static final fun toRx2ApolloStore (Lcom/apollographql/apollo3/cache/normalized/ApolloStore;Lio/reactivex/Scheduler;)Lcom/apollographql/apollo3/rx2/Rx2ApolloStore; + public static synthetic fun toRx2ApolloStore$default (Lcom/apollographql/apollo3/cache/normalized/ApolloStore;Lio/reactivex/Scheduler;ILjava/lang/Object;)Lcom/apollographql/apollo3/rx2/Rx2ApolloStore; + public static final fun toRx2HttpInterceptor (Lcom/apollographql/apollo3/network/http/HttpInterceptor;Lio/reactivex/Scheduler;)Lcom/apollographql/apollo3/rx2/Rx2HttpInterceptor; + public static synthetic fun toRx2HttpInterceptor$default (Lcom/apollographql/apollo3/network/http/HttpInterceptor;Lio/reactivex/Scheduler;ILjava/lang/Object;)Lcom/apollographql/apollo3/rx2/Rx2HttpInterceptor; + public static final fun toRx2HttpInterceptorChain (Lcom/apollographql/apollo3/network/http/HttpInterceptorChain;Lio/reactivex/Scheduler;)Lcom/apollographql/apollo3/rx2/Rx2HttpInterceptorChain; + public static synthetic fun toRx2HttpInterceptorChain$default (Lcom/apollographql/apollo3/network/http/HttpInterceptorChain;Lio/reactivex/Scheduler;ILjava/lang/Object;)Lcom/apollographql/apollo3/rx2/Rx2HttpInterceptorChain; + public static final fun toRx2NetworkTransport (Lcom/apollographql/apollo3/network/NetworkTransport;Lio/reactivex/Scheduler;)Lcom/apollographql/apollo3/rx2/Rx2NetworkTransport; + public static synthetic fun toRx2NetworkTransport$default (Lcom/apollographql/apollo3/network/NetworkTransport;Lio/reactivex/Scheduler;ILjava/lang/Object;)Lcom/apollographql/apollo3/rx2/Rx2NetworkTransport; } public abstract interface class com/apollographql/apollo3/rx2/Rx2ApolloInterceptor { @@ -63,20 +74,3 @@ public final class com/apollographql/apollo3/rx2/ToCoroutinesKt { public static final fun toNetworkTransport (Lcom/apollographql/apollo3/rx2/Rx2NetworkTransport;)Lcom/apollographql/apollo3/network/NetworkTransport; } -public final class com/apollographql/apollo3/rx2/ToRx2Kt { - public static final fun toRx2ApolloClient (Lcom/apollographql/apollo3/ApolloClient;Lio/reactivex/Scheduler;)Lcom/apollographql/apollo3/rx2/Rx2ApolloClient; - public static synthetic fun toRx2ApolloClient$default (Lcom/apollographql/apollo3/ApolloClient;Lio/reactivex/Scheduler;ILjava/lang/Object;)Lcom/apollographql/apollo3/rx2/Rx2ApolloClient; - public static final fun toRx2ApolloInterceptor (Lcom/apollographql/apollo3/interceptor/ApolloInterceptor;Lio/reactivex/Scheduler;)Lcom/apollographql/apollo3/rx2/Rx2ApolloInterceptor; - public static synthetic fun toRx2ApolloInterceptor$default (Lcom/apollographql/apollo3/interceptor/ApolloInterceptor;Lio/reactivex/Scheduler;ILjava/lang/Object;)Lcom/apollographql/apollo3/rx2/Rx2ApolloInterceptor; - public static final fun toRx2ApolloInterceptorChain (Lcom/apollographql/apollo3/interceptor/ApolloInterceptorChain;Lio/reactivex/Scheduler;)Lcom/apollographql/apollo3/rx2/Rx2ApolloInterceptorChain; - public static synthetic fun toRx2ApolloInterceptorChain$default (Lcom/apollographql/apollo3/interceptor/ApolloInterceptorChain;Lio/reactivex/Scheduler;ILjava/lang/Object;)Lcom/apollographql/apollo3/rx2/Rx2ApolloInterceptorChain; - public static final fun toRx2ApolloStore (Lcom/apollographql/apollo3/cache/normalized/ApolloStore;Lio/reactivex/Scheduler;)Lcom/apollographql/apollo3/rx2/Rx2ApolloStore; - public static synthetic fun toRx2ApolloStore$default (Lcom/apollographql/apollo3/cache/normalized/ApolloStore;Lio/reactivex/Scheduler;ILjava/lang/Object;)Lcom/apollographql/apollo3/rx2/Rx2ApolloStore; - public static final fun toRx2HttpInterceptor (Lcom/apollographql/apollo3/network/http/HttpInterceptor;Lio/reactivex/Scheduler;)Lcom/apollographql/apollo3/rx2/Rx2HttpInterceptor; - public static synthetic fun toRx2HttpInterceptor$default (Lcom/apollographql/apollo3/network/http/HttpInterceptor;Lio/reactivex/Scheduler;ILjava/lang/Object;)Lcom/apollographql/apollo3/rx2/Rx2HttpInterceptor; - public static final fun toRx2HttpInterceptorChain (Lcom/apollographql/apollo3/network/http/HttpInterceptorChain;Lio/reactivex/Scheduler;)Lcom/apollographql/apollo3/rx2/Rx2HttpInterceptorChain; - public static synthetic fun toRx2HttpInterceptorChain$default (Lcom/apollographql/apollo3/network/http/HttpInterceptorChain;Lio/reactivex/Scheduler;ILjava/lang/Object;)Lcom/apollographql/apollo3/rx2/Rx2HttpInterceptorChain; - public static final fun toRx2NetworkTransport (Lcom/apollographql/apollo3/network/NetworkTransport;Lio/reactivex/Scheduler;)Lcom/apollographql/apollo3/rx2/Rx2NetworkTransport; - public static synthetic fun toRx2NetworkTransport$default (Lcom/apollographql/apollo3/network/NetworkTransport;Lio/reactivex/Scheduler;ILjava/lang/Object;)Lcom/apollographql/apollo3/rx2/Rx2NetworkTransport; -} - diff --git a/apollo-rx2-support/src/main/java/com/apollographql/apollo3/rx2/toRx2.kt b/apollo-rx2-support/src/main/java/com/apollographql/apollo3/rx2/toRx2.kt index e3cd3a32eb..93e345f9bc 100644 --- a/apollo-rx2-support/src/main/java/com/apollographql/apollo3/rx2/toRx2.kt +++ b/apollo-rx2-support/src/main/java/com/apollographql/apollo3/rx2/toRx2.kt @@ -1,6 +1,10 @@ +@file:JvmName("Rx2Apollo") + package com.apollographql.apollo3.rx2 -import com.apollographql.apollo3.ApolloClient +import com.apollographql.apollo3.ApolloMutationCall +import com.apollographql.apollo3.ApolloQueryCall +import com.apollographql.apollo3.ApolloSubscriptionCall import com.apollographql.apollo3.api.ApolloRequest import com.apollographql.apollo3.api.ApolloResponse import com.apollographql.apollo3.api.CustomScalarAdapters @@ -15,7 +19,6 @@ import com.apollographql.apollo3.cache.CacheHeaders import com.apollographql.apollo3.cache.normalized.ApolloStore import com.apollographql.apollo3.cache.normalized.CacheKey import com.apollographql.apollo3.cache.normalized.NormalizedCache -import com.apollographql.apollo3.exception.CacheMissException import com.apollographql.apollo3.interceptor.ApolloInterceptor import com.apollographql.apollo3.interceptor.ApolloInterceptorChain import com.apollographql.apollo3.network.NetworkTransport @@ -77,7 +80,7 @@ class Rx2ApolloStore( customScalarAdapters: CustomScalarAdapters, cacheHeaders: CacheHeaders = CacheHeaders.NONE, ): Single = rxSingle(dispatcher) { - delegate.readOperation(operation, customScalarAdapters, cacheHeaders) ?: throw CacheMissException("Cache miss excpetion for $operation") + delegate.readOperation(operation, customScalarAdapters, cacheHeaders) } fun rxReadFragment( @@ -86,7 +89,7 @@ class Rx2ApolloStore( customScalarAdapters: CustomScalarAdapters = CustomScalarAdapters.Empty, cacheHeaders: CacheHeaders = CacheHeaders.NONE, ): Single = rxSingle(dispatcher) { - delegate.readFragment(fragment, cacheKey, customScalarAdapters, cacheHeaders) ?: throw CacheMissException("Cache miss excpetion for $fragment") + delegate.readFragment(fragment, cacheKey, customScalarAdapters, cacheHeaders) } fun rxWriteOperation( @@ -148,38 +151,15 @@ class Rx2ApolloStore( } } -fun ApolloClient.toRx2ApolloClient(scheduler: Scheduler = Schedulers.io()) = Rx2ApolloClient(this, scheduler) - -class Rx2ApolloClient(private val delegate: ApolloClient, private val scheduler: Scheduler) { - private val dispatcher = scheduler.asCoroutineDispatcher() - - fun query(query: Query): Single> = query(ApolloRequest.Builder(query).build()) - - fun mutate(mutation: Mutation): Single> = mutate(ApolloRequest.Builder(mutation).build()) - - fun subscribe(subscription: Subscription): Flowable> = subscribe(ApolloRequest.Builder(subscription).build()) - - fun query(queryRequest: ApolloRequest): Single> = rxSingle(dispatcher) { - delegate.query(queryRequest) - } - - fun queryAsFlow(queryRequest: ApolloRequest): Flowable> { - return delegate.queryAsFlow(queryRequest).asFlowable(dispatcher) - } - - fun mutate(mutationRequest: ApolloRequest): Single> = rxSingle(dispatcher) { - delegate.mutate(mutationRequest) - } - - fun mutateAsFlow(mutationRequest: ApolloRequest): Flowable> { - return delegate.mutateAsFlow(mutationRequest).asFlowable(dispatcher) - } - - fun subscribe(subscriptionRequest: ApolloRequest): Flowable> { - return delegate.subscribe(subscriptionRequest).asFlowable(dispatcher) - } +@JvmOverloads +fun ApolloQueryCall.rxSingle(scheduler: Scheduler = Schedulers.io()) = rxSingle(scheduler.asCoroutineDispatcher()) { + execute() +} - fun dispose() { - delegate.dispose() - } +@JvmOverloads +fun ApolloMutationCall.rxSingle(scheduler: Scheduler = Schedulers.io()) = rxSingle(scheduler.asCoroutineDispatcher()) { + execute() } + +@JvmOverloads +fun ApolloSubscriptionCall.rxFlowable(scheduler: Scheduler = Schedulers.io()) = execute().asFlowable(scheduler.asCoroutineDispatcher()) diff --git a/apollo-rx3-support/api/apollo-rx3-support.api b/apollo-rx3-support/api/apollo-rx3-support.api index 94b2e806aa..54df277d4e 100644 --- a/apollo-rx3-support/api/apollo-rx3-support.api +++ b/apollo-rx3-support/api/apollo-rx3-support.api @@ -1,16 +1,3 @@ -public final class com/apollographql/apollo3/rx3/Rx3ApolloClient { - public fun (Lcom/apollographql/apollo3/ApolloClient;Lio/reactivex/rxjava3/core/Scheduler;)V - public final fun dispose ()V - public final fun mutate (Lcom/apollographql/apollo3/api/ApolloRequest;)Lio/reactivex/rxjava3/core/Single; - public final fun mutate (Lcom/apollographql/apollo3/api/Mutation;)Lio/reactivex/rxjava3/core/Single; - public final fun mutateAsFlow (Lcom/apollographql/apollo3/api/ApolloRequest;)Lio/reactivex/rxjava3/core/Flowable; - public final fun query (Lcom/apollographql/apollo3/api/ApolloRequest;)Lio/reactivex/rxjava3/core/Single; - public final fun query (Lcom/apollographql/apollo3/api/Query;)Lio/reactivex/rxjava3/core/Single; - public final fun queryAsFlow (Lcom/apollographql/apollo3/api/ApolloRequest;)Lio/reactivex/rxjava3/core/Flowable; - public final fun subscribe (Lcom/apollographql/apollo3/api/ApolloRequest;)Lio/reactivex/rxjava3/core/Flowable; - public final fun subscribe (Lcom/apollographql/apollo3/api/Subscription;)Lio/reactivex/rxjava3/core/Flowable; -} - public abstract interface class com/apollographql/apollo3/rx3/Rx3ApolloInterceptor { public abstract fun intercept (Lcom/apollographql/apollo3/api/ApolloRequest;Lcom/apollographql/apollo3/rx3/Rx3ApolloInterceptorChain;)Lio/reactivex/rxjava3/core/Flowable; } @@ -64,8 +51,9 @@ public final class com/apollographql/apollo3/rx3/ToCoroutinesKt { } public final class com/apollographql/apollo3/rx3/ToRx3Kt { - public static final fun toRx3ApolloClient (Lcom/apollographql/apollo3/ApolloClient;Lio/reactivex/rxjava3/core/Scheduler;)Lcom/apollographql/apollo3/rx3/Rx3ApolloClient; - public static synthetic fun toRx3ApolloClient$default (Lcom/apollographql/apollo3/ApolloClient;Lio/reactivex/rxjava3/core/Scheduler;ILjava/lang/Object;)Lcom/apollographql/apollo3/rx3/Rx3ApolloClient; + public static final fun rxFlowable (Lcom/apollographql/apollo3/ApolloSubscriptionCall;Lio/reactivex/rxjava3/core/Scheduler;)Lio/reactivex/rxjava3/core/Flowable; + public static final fun rxSingle (Lcom/apollographql/apollo3/ApolloMutationCall;Lio/reactivex/rxjava3/core/Scheduler;)Lio/reactivex/rxjava3/core/Single; + public static final fun rxSingle (Lcom/apollographql/apollo3/ApolloQueryCall;Lio/reactivex/rxjava3/core/Scheduler;)Lio/reactivex/rxjava3/core/Single; public static final fun toRx3ApolloInterceptor (Lcom/apollographql/apollo3/interceptor/ApolloInterceptor;Lio/reactivex/rxjava3/core/Scheduler;)Lcom/apollographql/apollo3/rx3/Rx3ApolloInterceptor; public static synthetic fun toRx3ApolloInterceptor$default (Lcom/apollographql/apollo3/interceptor/ApolloInterceptor;Lio/reactivex/rxjava3/core/Scheduler;ILjava/lang/Object;)Lcom/apollographql/apollo3/rx3/Rx3ApolloInterceptor; public static final fun toRx3ApolloInterceptorChain (Lcom/apollographql/apollo3/interceptor/ApolloInterceptorChain;Lio/reactivex/rxjava3/core/Scheduler;)Lcom/apollographql/apollo3/rx3/Rx3ApolloInterceptorChain; diff --git a/apollo-rx3-support/src/main/java/com/apollographql/apollo3/rx3/toRx3.kt b/apollo-rx3-support/src/main/java/com/apollographql/apollo3/rx3/toRx3.kt index a6d92d69bc..d7bb3cc2fe 100644 --- a/apollo-rx3-support/src/main/java/com/apollographql/apollo3/rx3/toRx3.kt +++ b/apollo-rx3-support/src/main/java/com/apollographql/apollo3/rx3/toRx3.kt @@ -3,7 +3,9 @@ */ package com.apollographql.apollo3.rx3 -import com.apollographql.apollo3.ApolloClient +import com.apollographql.apollo3.ApolloMutationCall +import com.apollographql.apollo3.ApolloQueryCall +import com.apollographql.apollo3.ApolloSubscriptionCall import com.apollographql.apollo3.api.ApolloRequest import com.apollographql.apollo3.api.ApolloResponse import com.apollographql.apollo3.api.CustomScalarAdapters @@ -18,7 +20,6 @@ import com.apollographql.apollo3.cache.CacheHeaders import com.apollographql.apollo3.cache.normalized.ApolloStore import com.apollographql.apollo3.cache.normalized.CacheKey import com.apollographql.apollo3.cache.normalized.NormalizedCache -import com.apollographql.apollo3.exception.CacheMissException import com.apollographql.apollo3.interceptor.ApolloInterceptor import com.apollographql.apollo3.interceptor.ApolloInterceptorChain import com.apollographql.apollo3.network.NetworkTransport @@ -80,7 +81,7 @@ class Rx3ApolloStore( customScalarAdapters: CustomScalarAdapters, cacheHeaders: CacheHeaders = CacheHeaders.NONE, ): Single = rxSingle(dispatcher) { - delegate.readOperation(operation, customScalarAdapters, cacheHeaders) ?: throw CacheMissException("Cache miss excpetion for $operation") + delegate.readOperation(operation, customScalarAdapters, cacheHeaders) } fun rxReadFragment( @@ -89,7 +90,7 @@ class Rx3ApolloStore( customScalarAdapters: CustomScalarAdapters = CustomScalarAdapters.Empty, cacheHeaders: CacheHeaders = CacheHeaders.NONE, ): Single = rxSingle(dispatcher) { - delegate.readFragment(fragment, cacheKey, customScalarAdapters, cacheHeaders) ?: throw CacheMissException("Cache miss excpetion for $fragment") + delegate.readFragment(fragment, cacheKey, customScalarAdapters, cacheHeaders) } fun rxWriteOperation( @@ -151,38 +152,10 @@ class Rx3ApolloStore( } } -fun ApolloClient.toRx3ApolloClient(scheduler: Scheduler = Schedulers.io()) = Rx3ApolloClient(this, scheduler) - -class Rx3ApolloClient(private val delegate: ApolloClient, private val scheduler: Scheduler) { - private val dispatcher = scheduler.asCoroutineDispatcher() - - fun query(query: Query): Single> = query(ApolloRequest.Builder(query).build()) - - fun mutate(mutation: Mutation): Single> = mutate(ApolloRequest.Builder(mutation).build()) - - fun subscribe(subscription: Subscription): Flowable> = subscribe(ApolloRequest.Builder(subscription).build()) - - fun query(queryRequest: ApolloRequest): Single> = rxSingle(dispatcher) { - delegate.query(queryRequest) - } - - fun queryAsFlow(queryRequest: ApolloRequest): Flowable> { - return delegate.queryAsFlow(queryRequest).asFlowable(dispatcher) - } - - fun mutate(mutationRequest: ApolloRequest): Single> = rxSingle(dispatcher) { - delegate.mutate(mutationRequest) - } - - fun mutateAsFlow(mutationRequest: ApolloRequest): Flowable> { - return delegate.mutateAsFlow(mutationRequest).asFlowable(dispatcher) - } - - fun subscribe(subscriptionRequest: ApolloRequest): Flowable> { - return delegate.subscribe(subscriptionRequest).asFlowable(dispatcher) - } - - fun dispose() { - delegate.dispose() - } +fun ApolloQueryCall.rxSingle(scheduler: Scheduler) = rxSingle(scheduler.asCoroutineDispatcher()) { + execute() +} +fun ApolloMutationCall.rxSingle(scheduler: Scheduler) = rxSingle(scheduler.asCoroutineDispatcher()) { + execute() } +fun ApolloSubscriptionCall.rxFlowable(scheduler: Scheduler) = execute().asFlowable(scheduler.asCoroutineDispatcher()) diff --git a/tests/coroutines-mt/src/macosX64Test/kotlin/macos/app/MainTest.kt b/tests/coroutines-mt/src/macosX64Test/kotlin/macos/app/MainTest.kt index 35c34f098f..13f52df99b 100644 --- a/tests/coroutines-mt/src/macosX64Test/kotlin/macos/app/MainTest.kt +++ b/tests/coroutines-mt/src/macosX64Test/kotlin/macos/app/MainTest.kt @@ -30,7 +30,11 @@ class MainTest { println("Dispatchers.Main: ${currentThreadId()}") val server = MockServer() server.enqueue(json) - val response = ApolloClient.Builder().serverUrl(server.url()).build().query(GetRandomQuery()) + val response = ApolloClient.Builder() + .serverUrl(server.url()) + .build() + .query(GetRandomQuery()) + .execute() check(response.dataOrThrow.random == 42) } } @@ -43,7 +47,7 @@ class MainTest { val client = ApolloClient.Builder().serverUrl(server.url()).build().freeze() withContext(Dispatchers.Default) { assertFailsWith(IllegalStateException::class) { - client.query(GetRandomQuery()) + client.query(GetRandomQuery()).execute() } } } @@ -55,7 +59,7 @@ class MainTest { val client = ApolloClient.Builder().serverUrl(server.url()).normalizedCache(MemoryCacheFactory()).build() withContext(Dispatchers.Default) { withContext(Dispatchers.Main) { - val response = client.query(GetRandomQuery()) + val response = client.query(GetRandomQuery()).execute() check(response.dataOrThrow.random == 42) } } diff --git a/tests/http-cache/src/test/kotlin/HttpCacheTest.kt b/tests/http-cache/src/test/kotlin/HttpCacheTest.kt index eb3081e8f6..5575537239 100644 --- a/tests/http-cache/src/test/kotlin/HttpCacheTest.kt +++ b/tests/http-cache/src/test/kotlin/HttpCacheTest.kt @@ -1,5 +1,5 @@ + import com.apollographql.apollo3.ApolloClient -import com.apollographql.apollo3.api.ApolloRequest import com.apollographql.apollo3.api.http.DefaultHttpRequestComposer import com.apollographql.apollo3.cache.http.CachingHttpEngine import com.apollographql.apollo3.cache.http.HttpFetchPolicy @@ -12,11 +12,10 @@ import com.apollographql.apollo3.mockserver.MockServer import com.apollographql.apollo3.mockserver.enqueue import com.apollographql.apollo3.network.http.HttpNetworkTransport import com.apollographql.apollo3.testing.runTest -import kotlinx.coroutines.delay -import kotlinx.coroutines.runBlocking -import org.junit.Before import httpcache.GetRandom2Query import httpcache.GetRandomQuery +import kotlinx.coroutines.delay +import kotlinx.coroutines.runBlocking import java.io.File import kotlin.test.Test import kotlin.test.assertEquals @@ -67,11 +66,11 @@ class HttpCacheTest { mockServer.enqueue(response) runBlocking { - var response = apolloClient.query(GetRandomQuery()) + var response = apolloClient.query(GetRandomQuery()).execute() assertEquals(42, response.data?.random) assertEquals(false, response.isFromHttpCache) - response = apolloClient.query(GetRandomQuery()) + response = apolloClient.query(GetRandomQuery()).execute() assertEquals(42, response.data?.random) assertEquals(true, response.isFromHttpCache) } @@ -83,13 +82,14 @@ class HttpCacheTest { mockServer.enqueue(MockResponse(statusCode = 500)) runBlocking { - val response = apolloClient.query(GetRandomQuery()) + val response = apolloClient.query(GetRandomQuery()).execute() assertEquals(42, response.data?.random) assertEquals(false, response.isFromHttpCache) - val request = ApolloRequest.Builder(GetRandomQuery()).httpFetchPolicy(HttpFetchPolicy.NetworkOnly).build() assertFails { - apolloClient.query(request) + apolloClient.query(GetRandomQuery()) + .httpFetchPolicy(HttpFetchPolicy.NetworkOnly) + .execute() } } } @@ -101,11 +101,13 @@ class HttpCacheTest { runBlocking { var response = apolloClient.query(GetRandomQuery()) + .execute() assertEquals(42, response.data?.random) assertEquals(false, response.isFromHttpCache) - val request = ApolloRequest.Builder(GetRandomQuery()).httpFetchPolicy(HttpFetchPolicy.NetworkFirst).build() - response = apolloClient.query(request) + response = apolloClient.query(GetRandomQuery()) + .httpFetchPolicy(HttpFetchPolicy.NetworkFirst) + .execute() assertEquals(42, response.data?.random) assertEquals(true, response.isFromHttpCache) } @@ -116,21 +118,20 @@ class HttpCacheTest { mockServer.enqueue(response) runBlocking { - var response = apolloClient.query(GetRandomQuery()) + var response = apolloClient.query(GetRandomQuery()).execute() assertEquals(42, response.data?.random) assertEquals(false, response.isFromHttpCache) - response = apolloClient.query(ApolloRequest.Builder(GetRandomQuery()).httpExpireTimeout(500).build()) + response = apolloClient.query(GetRandomQuery()).httpExpireTimeout(500).execute() assertEquals(42, response.data?.random) assertEquals(true, response.isFromHttpCache) delay(1000) assertFailsWith(HttpCacheMissException::class) { - apolloClient.query(ApolloRequest.Builder(GetRandomQuery()) + apolloClient.query(GetRandomQuery()) .httpExpireTimeout(500) .httpFetchPolicy(HttpFetchPolicy.CacheOnly) - .build() - ) + .execute() } } } @@ -141,11 +142,11 @@ class HttpCacheTest { mockServer.enqueue(response2) runBlocking { - val response = apolloClient.query(GetRandomQuery()) + val response = apolloClient.query(GetRandomQuery()).execute() assertEquals(42, response.data?.random) assertEquals(false, response.isFromHttpCache) - val response2 = apolloClient.query(GetRandom2Query()) + val response2 = apolloClient.query(GetRandom2Query()).execute() assertEquals(42, response2.data?.random2) assertEquals(false, response2.isFromHttpCache) } diff --git a/tests/http-cache/src/test/kotlin/WithHttpCacheTest.kt b/tests/http-cache/src/test/kotlin/WithHttpCacheTest.kt index 999d5ded58..dc8ec4bb3d 100644 --- a/tests/http-cache/src/test/kotlin/WithHttpCacheTest.kt +++ b/tests/http-cache/src/test/kotlin/WithHttpCacheTest.kt @@ -1,12 +1,13 @@ + import com.apollographql.apollo3.ApolloClient import com.apollographql.apollo3.cache.http.httpCache import com.apollographql.apollo3.cache.http.isFromHttpCache import com.apollographql.apollo3.mockserver.MockServer import com.apollographql.apollo3.mockserver.enqueue import com.apollographql.apollo3.testing.runTest +import httpcache.GetRandomQuery import kotlinx.coroutines.runBlocking import org.junit.Test -import httpcache.GetRandomQuery import java.io.File import kotlin.test.assertEquals @@ -34,11 +35,11 @@ class WithHttpCacheTest { mockServer.enqueue(mockResponse) runBlocking { - var response = apolloClient.query(GetRandomQuery()) + var response = apolloClient.query(GetRandomQuery()).execute() assertEquals(42, response.data?.random) assertEquals(false, response.isFromHttpCache) - response = apolloClient.query(GetRandomQuery()) + response = apolloClient.query(GetRandomQuery()).execute() assertEquals(42, response.data?.random) assertEquals(true, response.isFromHttpCache) } diff --git a/tests/idling-resource/src/test/java/IdlingResourceTest.kt b/tests/idling-resource/src/test/java/IdlingResourceTest.kt index 41ea57e49c..0c6624127c 100644 --- a/tests/idling-resource/src/test/java/IdlingResourceTest.kt +++ b/tests/idling-resource/src/test/java/IdlingResourceTest.kt @@ -29,7 +29,7 @@ class IdlingResourceTest { launch(start = CoroutineStart.UNDISPATCHED) { kotlin.runCatching { - apolloClient.query(IdlingResourceQuery()) + apolloClient.query(IdlingResourceQuery()).execute() } } assert(!idlingResource.isIdleNow) diff --git a/tests/integration-tests/src/appleTest/kotlin/test/HttpEngineTest.kt b/tests/integration-tests/src/appleTest/kotlin/test/HttpEngineTest.kt index 1f42b44a0c..6d6dc4ab84 100644 --- a/tests/integration-tests/src/appleTest/kotlin/test/HttpEngineTest.kt +++ b/tests/integration-tests/src/appleTest/kotlin/test/HttpEngineTest.kt @@ -21,7 +21,7 @@ class HttpEngineTest { val apolloClient = ApolloClient.Builder().serverUrl("https://inexistent.host/graphql").build() val result = kotlin.runCatching { - apolloClient.query(HeroNameQuery()) + apolloClient.query(HeroNameQuery()).execute() } val apolloNetworkException = result.exceptionOrNull() diff --git a/tests/integration-tests/src/commonTest/kotlin/test/AutoPersistedQueriesTest.kt b/tests/integration-tests/src/commonTest/kotlin/test/AutoPersistedQueriesTest.kt index 58ef1d4e8a..0bf3a09361 100644 --- a/tests/integration-tests/src/commonTest/kotlin/test/AutoPersistedQueriesTest.kt +++ b/tests/integration-tests/src/commonTest/kotlin/test/AutoPersistedQueriesTest.kt @@ -11,7 +11,6 @@ import readResource import kotlin.test.Test import kotlin.test.assertFalse import kotlin.test.assertTrue -import kotlinx.coroutines.delay class AutoPersistedQueriesTest { private lateinit var mockServer: MockServer @@ -30,7 +29,7 @@ class AutoPersistedQueriesTest { val apolloClient = ApolloClient.Builder().serverUrl(mockServer.url()).autoPersistedQueries(httpMethodForHashedQueries = HttpMethod.Post).build() - apolloClient.query(HeroNameQuery()) + apolloClient.query(HeroNameQuery()).execute() val request = mockServer.takeRequest() @@ -54,7 +53,7 @@ class AutoPersistedQueriesTest { val apolloClient = ApolloClient.Builder().serverUrl(mockServer.url()).autoPersistedQueries(httpMethodForHashedQueries = HttpMethod.Post).build() - apolloClient.query(HeroNameQuery()) + apolloClient.query(HeroNameQuery()).execute() var request = mockServer.takeRequest() assertFalse(request.body.utf8().contains("query")) diff --git a/tests/integration-tests/src/commonTest/kotlin/test/BasicTest.kt b/tests/integration-tests/src/commonTest/kotlin/test/BasicTest.kt index 740f1ba67d..04da97d024 100644 --- a/tests/integration-tests/src/commonTest/kotlin/test/BasicTest.kt +++ b/tests/integration-tests/src/commonTest/kotlin/test/BasicTest.kt @@ -2,7 +2,6 @@ package test import IdObjectIdGenerator import com.apollographql.apollo3.ApolloClient -import com.apollographql.apollo3.api.ApolloRequest import com.apollographql.apollo3.api.ApolloResponse import com.apollographql.apollo3.api.Query import com.apollographql.apollo3.cache.normalized.ApolloStore @@ -59,9 +58,13 @@ class BasicTest { block: ApolloResponse.() -> Unit, ) = runTest(before = { setUp() }, after = { tearDown() }) { mockServer.enqueue(readResource(resourceName)) - var response = apolloClient.query(ApolloRequest.Builder(query).fetchPolicy(FetchPolicy.NetworkOnly).build()) + var response = apolloClient.query(query) + .fetchPolicy(FetchPolicy.NetworkOnly) + .execute() response.block() - response = apolloClient.query(ApolloRequest.Builder(query).fetchPolicy(FetchPolicy.CacheOnly).build()) + response = apolloClient.query(query) + .fetchPolicy(FetchPolicy.CacheOnly) + .execute() response.block() } diff --git a/tests/integration-tests/src/commonTest/kotlin/test/BearerTokenInterceptorTest.kt b/tests/integration-tests/src/commonTest/kotlin/test/BearerTokenInterceptorTest.kt index 41113e481d..cd7fcc1536 100644 --- a/tests/integration-tests/src/commonTest/kotlin/test/BearerTokenInterceptorTest.kt +++ b/tests/integration-tests/src/commonTest/kotlin/test/BearerTokenInterceptorTest.kt @@ -44,7 +44,7 @@ class BearerTokenInterceptorTest { ) .build() - val response = apolloClient.query(HeroNameQuery()) + val response = apolloClient.query(HeroNameQuery()).execute() assertEquals("R2-D2", response.data?.hero?.name) assertEquals("Bearer $token1", mockServer.takeRequest().headers["Authorization"]) @@ -62,7 +62,7 @@ class BearerTokenInterceptorTest { .build() try { - apolloClient.query(HeroNameQuery()) + apolloClient.query(HeroNameQuery()).execute() } catch (e: ApolloHttpException) { assertEquals(401, e.statusCode) } diff --git a/tests/integration-tests/src/commonTest/kotlin/test/CacheFlagsTest.kt b/tests/integration-tests/src/commonTest/kotlin/test/CacheFlagsTest.kt index 94b05a2839..7791728fe3 100644 --- a/tests/integration-tests/src/commonTest/kotlin/test/CacheFlagsTest.kt +++ b/tests/integration-tests/src/commonTest/kotlin/test/CacheFlagsTest.kt @@ -2,7 +2,6 @@ package test import com.apollographql.apollo3.ApolloClient import com.apollographql.apollo3.api.AnyAdapter -import com.apollographql.apollo3.api.ApolloRequest import com.apollographql.apollo3.api.toJson import com.apollographql.apollo3.cache.ApolloCacheHeaders import com.apollographql.apollo3.cache.CacheHeaders @@ -46,11 +45,11 @@ class CacheFlagsTest { val data = HeroNameQuery.Data(HeroNameQuery.Hero("R2-D2")) mockServer.enqueue(query, data) - apolloClient.query(ApolloRequest.Builder(query).doNotStore(true).build()) + apolloClient.query(query).doNotStore(true).execute() // Since the previous request was not stored, this should fail assertFailsWith(CacheMissException::class) { - apolloClient.query(ApolloRequest.Builder(query).fetchPolicy(FetchPolicy.CacheOnly).build()) + apolloClient.query(query).fetchPolicy(FetchPolicy.CacheOnly).execute() } } @@ -61,22 +60,19 @@ class CacheFlagsTest { mockServer.enqueue(query, data) // Store the data - apolloClient.query(ApolloRequest.Builder(query).fetchPolicy(FetchPolicy.NetworkOnly).build()) + apolloClient.query(query).fetchPolicy(FetchPolicy.NetworkOnly).execute() // This should work and evict the entries - val response = apolloClient.query( - ApolloRequest.Builder(query) + val response = apolloClient.query(query) .fetchPolicy(FetchPolicy.CacheOnly) - .cacheHeaders( - CacheHeaders.builder().addHeader(ApolloCacheHeaders.EVICT_AFTER_READ, "true").build() - ) - .build() - ) + .cacheHeaders(CacheHeaders.builder().addHeader(ApolloCacheHeaders.EVICT_AFTER_READ, "true").build()) + .execute() + assertEquals("R2-D2", response.data?.hero?.name) // Second time should fail assertFailsWith(CacheMissException::class) { - apolloClient.query(ApolloRequest.Builder(query).fetchPolicy(FetchPolicy.CacheOnly).build()) + apolloClient.query(query).fetchPolicy(FetchPolicy.CacheOnly).execute() } } @@ -103,10 +99,10 @@ class CacheFlagsTest { mockServer.enqueue(AnyAdapter.toJson(partialResponse)) // this should not store the response - apolloClient.query(ApolloRequest.Builder(query).build()) + apolloClient.query(query).execute() assertFailsWith(CacheMissException::class) { - apolloClient.query(ApolloRequest.Builder(query).fetchPolicy(FetchPolicy.CacheOnly).build()) + apolloClient.query(query).fetchPolicy(FetchPolicy.CacheOnly).execute() } } @@ -116,9 +112,9 @@ class CacheFlagsTest { mockServer.enqueue(AnyAdapter.toJson(partialResponse)) // this should not store the response - apolloClient.query(ApolloRequest.Builder(query).storePartialResponses(true).build()) + apolloClient.query(query).storePartialResponses(true).execute() - val response = apolloClient.query(ApolloRequest.Builder(query).fetchPolicy(FetchPolicy.CacheOnly).build()) + val response = apolloClient.query(query).fetchPolicy(FetchPolicy.CacheOnly).execute() assertNotNull(response.data) } } \ No newline at end of file diff --git a/tests/integration-tests/src/commonTest/kotlin/test/CacheResolverTest.kt b/tests/integration-tests/src/commonTest/kotlin/test/CacheResolverTest.kt index ff915defda..380efdf247 100644 --- a/tests/integration-tests/src/commonTest/kotlin/test/CacheResolverTest.kt +++ b/tests/integration-tests/src/commonTest/kotlin/test/CacheResolverTest.kt @@ -33,7 +33,7 @@ class CacheResolverTest { ) .build() - val response = apolloClient.query(HeroNameQuery()) + val response = apolloClient.query(HeroNameQuery()).execute() assertEquals("Luke", response.data?.hero?.name) } diff --git a/tests/integration-tests/src/commonTest/kotlin/test/CancelTest.kt b/tests/integration-tests/src/commonTest/kotlin/test/CancelTest.kt index 8a08037ddc..50d55c2e20 100644 --- a/tests/integration-tests/src/commonTest/kotlin/test/CancelTest.kt +++ b/tests/integration-tests/src/commonTest/kotlin/test/CancelTest.kt @@ -2,15 +2,14 @@ package test import com.apollographql.apollo3.ApolloClient import com.apollographql.apollo3.cache.normalized.MemoryCacheFactory +import com.apollographql.apollo3.cache.normalized.executeCacheAndNetwork import com.apollographql.apollo3.cache.normalized.normalizedCache -import com.apollographql.apollo3.cache.normalized.queryCacheAndNetwork import com.apollographql.apollo3.integration.normalizer.EpisodeHeroNameQuery import com.apollographql.apollo3.integration.normalizer.type.Episode import com.apollographql.apollo3.mockserver.MockServer import com.apollographql.apollo3.mockserver.enqueue import com.apollographql.apollo3.testing.runTest import kotlinx.coroutines.delay -import kotlinx.coroutines.flow.MutableSharedFlow import kotlinx.coroutines.flow.toList import kotlinx.coroutines.launch import readTestFixture @@ -34,7 +33,7 @@ class CancelTest { val job = launch { delay(100) - apolloClient.query(EpisodeHeroNameQuery(Episode.EMPIRE)) + apolloClient.query(EpisodeHeroNameQuery(Episode.EMPIRE)).execute() error("The Flow should have been canceled before reaching that state") } job.cancel() @@ -47,7 +46,7 @@ class CancelTest { val apolloClient = ApolloClient.Builder().serverUrl(mockServer.url()).normalizedCache(MemoryCacheFactory()).build() val job = launch { - apolloClient.queryCacheAndNetwork(EpisodeHeroNameQuery(Episode.EMPIRE)).toList() + apolloClient.query(EpisodeHeroNameQuery(Episode.EMPIRE)).executeCacheAndNetwork().toList() } delay(100) job.cancel() diff --git a/tests/integration-tests/src/commonTest/kotlin/test/ExceptionsTest.kt b/tests/integration-tests/src/commonTest/kotlin/test/ExceptionsTest.kt index 59927c993b..9380db0b1f 100644 --- a/tests/integration-tests/src/commonTest/kotlin/test/ExceptionsTest.kt +++ b/tests/integration-tests/src/commonTest/kotlin/test/ExceptionsTest.kt @@ -1,7 +1,6 @@ package test import com.apollographql.apollo3.ApolloClient -import com.apollographql.apollo3.api.ApolloRequest import com.apollographql.apollo3.exception.ApolloHttpException import com.apollographql.apollo3.exception.ApolloNetworkException import com.apollographql.apollo3.integration.normalizer.HeroNameQuery @@ -34,7 +33,7 @@ class ExceptionsTest { mockServer.enqueue("malformed") val result = kotlin.runCatching { - apolloClient.query(HeroNameQuery()) + apolloClient.query(HeroNameQuery()).execute() } assertTrue(result.exceptionOrNull() != null) @@ -45,7 +44,7 @@ class ExceptionsTest { mockServer.enqueue(MockResponse(statusCode = 404)) val result = kotlin.runCatching { - apolloClient.query(HeroNameQuery()) + apolloClient.query(HeroNameQuery()).execute() } val exception = result.exceptionOrNull() @@ -58,7 +57,7 @@ class ExceptionsTest { mockServer.stop() val result = kotlin.runCatching { - apolloClient.query(HeroNameQuery()) + apolloClient.query(HeroNameQuery()).execute() } val exception = result.exceptionOrNull() @@ -73,7 +72,8 @@ class ExceptionsTest { mockServer.enqueue(query, data) val response = apolloClient - .queryAsFlow(ApolloRequest.Builder(query).build()) + .query(query) + .executeAsFlow() .retryWhen { _, attempt -> attempt == 0L } .single() diff --git a/tests/integration-tests/src/commonTest/kotlin/test/FetchPolicyTest.kt b/tests/integration-tests/src/commonTest/kotlin/test/FetchPolicyTest.kt index 04a0ba549d..1b3b536efc 100644 --- a/tests/integration-tests/src/commonTest/kotlin/test/FetchPolicyTest.kt +++ b/tests/integration-tests/src/commonTest/kotlin/test/FetchPolicyTest.kt @@ -1,13 +1,12 @@ package test import com.apollographql.apollo3.ApolloClient -import com.apollographql.apollo3.api.ApolloRequest import com.apollographql.apollo3.cache.normalized.ApolloStore import com.apollographql.apollo3.cache.normalized.FetchPolicy import com.apollographql.apollo3.cache.normalized.MemoryCacheFactory +import com.apollographql.apollo3.cache.normalized.executeCacheAndNetwork import com.apollographql.apollo3.cache.normalized.fetchPolicy import com.apollographql.apollo3.cache.normalized.isFromCache -import com.apollographql.apollo3.cache.normalized.queryCacheAndNetwork import com.apollographql.apollo3.cache.normalized.store import com.apollographql.apollo3.exception.ApolloCompositeException import com.apollographql.apollo3.integration.normalizer.HeroNameQuery @@ -46,16 +45,16 @@ class FetchPolicyTest { val data = HeroNameQuery.Data(HeroNameQuery.Hero("R2-D2")) mockServer.enqueue(query, data) - // Cache first is also the default, no need to set the fetchPolicy // First query should hit the network and save in cache - val request = ApolloRequest.Builder(query).fetchPolicy(FetchPolicy.NetworkFirst).build() - var response = apolloClient.query(request) + var response = apolloClient.query(query) + .fetchPolicy(FetchPolicy.NetworkFirst) + .execute() assertNotNull(response.data) assertFalse(response.isFromCache) // Second query should only hit the cache - response = apolloClient.query(query) + response = apolloClient.query(query).execute() assertNotNull(response.data) assertTrue(response.isFromCache) @@ -64,7 +63,7 @@ class FetchPolicyTest { store.clearAll() mockServer.enqueue("malformed") try { - apolloClient.query(query) + apolloClient.query(query).execute() fail("we expected the query to fail") } catch (e: Exception) { assertTrue(e is ApolloCompositeException) @@ -76,25 +75,25 @@ class FetchPolicyTest { val query = HeroNameQuery() val data = HeroNameQuery.Data(HeroNameQuery.Hero("R2-D2")) - val request = ApolloRequest.Builder(query).fetchPolicy(FetchPolicy.NetworkFirst).build() + val call = apolloClient.query(query).fetchPolicy(FetchPolicy.NetworkFirst) // First query should hit the network and save in cache mockServer.enqueue(query, data) - var response = apolloClient.query(request) + var response = call.execute() assertNotNull(response.data) assertFalse(response.isFromCache) // Now data is cached but it shouldn't be used since network will go through mockServer.enqueue(query, data) - response = apolloClient.query(request) + response = call.execute() assertNotNull(response.data) assertFalse(response.isFromCache) // Network error -> we should hit now the cache mockServer.enqueue("malformed") - response = apolloClient.query(request) + response = call.execute() assertNotNull(response.data) assertTrue(response.isFromCache) @@ -103,7 +102,7 @@ class FetchPolicyTest { mockServer.enqueue("malformed") store.clearAll() try { - apolloClient.query(request) + call.execute() fail("NETWORK_FIRST should throw the network exception if nothing is in the cache") } catch (e: Exception) { @@ -115,19 +114,16 @@ class FetchPolicyTest { val query = HeroNameQuery() val data = HeroNameQuery.Data(HeroNameQuery.Hero("R2-D2")) - var request = ApolloRequest.Builder(query).build() // First query should hit the network and save in cache mockServer.enqueue(query, data) - var response = apolloClient.query(request) + var response = apolloClient.query(query).execute() assertNotNull(response.data) assertFalse(response.isFromCache) - request = request.newBuilder().fetchPolicy(FetchPolicy.CacheOnly).build() - // Second query should only hit the cache - response = apolloClient.query(request) + response = apolloClient.query(query).fetchPolicy(FetchPolicy.CacheOnly).execute() // And make sure we don't read the network assertNotNull(response.data) @@ -139,11 +135,11 @@ class FetchPolicyTest { val query = HeroNameQuery() val data = HeroNameQuery.Data(HeroNameQuery.Hero("R2-D2")) - val request = ApolloRequest.Builder(query).fetchPolicy(FetchPolicy.NetworkOnly).build() + val call = apolloClient.query(query).fetchPolicy(FetchPolicy.NetworkOnly) // First query should hit the network and save in cache mockServer.enqueue(query, data) - val response = apolloClient.query(request) + val response = call.execute() assertNotNull(response.data) assertFalse(response.isFromCache) @@ -151,7 +147,7 @@ class FetchPolicyTest { // Offer a malformed response, it should fail mockServer.enqueue("malformed") try { - apolloClient.query(request) + call.execute() fail("we expected a failure") } catch (e: Exception) { @@ -167,13 +163,13 @@ class FetchPolicyTest { // Cache Error + Network Error => Error mockServer.enqueue(MockResponse(statusCode = 500)) assertFailsWith(ApolloCompositeException::class) { - apolloClient.queryCacheAndNetwork(query).toList() + apolloClient.query(query).executeCacheAndNetwork().toList() } // Make the network return something // Cache Error + Nework Success => 1 response mockServer.enqueue(query, data) - var responses = apolloClient.queryCacheAndNetwork(query).toList() + var responses = apolloClient.query(query).executeCacheAndNetwork().toList() assertEquals(1, responses.size) assertNotNull(responses[0].data) @@ -183,7 +179,7 @@ class FetchPolicyTest { // Now cache is populated but make the network fail again // Cache Success + Network Error => 1 response mockServer.enqueue(MockResponse(statusCode = 500)) - responses = apolloClient.queryCacheAndNetwork(query).toList() + responses = apolloClient.query(query).executeCacheAndNetwork().toList() assertEquals(1, responses.size) assertNotNull(responses[0].data) @@ -192,7 +188,7 @@ class FetchPolicyTest { // Cache Success + Network Success => 1 response mockServer.enqueue(query, data) - responses = apolloClient.queryCacheAndNetwork(query).toList() + responses = apolloClient.query(query).executeCacheAndNetwork().toList() assertEquals(2, responses.size) assertNotNull(responses[0].data) diff --git a/tests/integration-tests/src/commonTest/kotlin/test/FileUploadTest.kt b/tests/integration-tests/src/commonTest/kotlin/test/FileUploadTest.kt index 081eb3a114..72766999bb 100644 --- a/tests/integration-tests/src/commonTest/kotlin/test/FileUploadTest.kt +++ b/tests/integration-tests/src/commonTest/kotlin/test/FileUploadTest.kt @@ -77,7 +77,7 @@ class FileUploadTest { @Test @Throws(Exception::class) fun single() = runTest(before = { setUp() }, after = { tearDown() }) { - apolloClient.mutate(mutationSingle) + apolloClient.mutate(mutationSingle).execute() val request = mockServer.takeRequest() val parts = request.parts() @@ -92,7 +92,7 @@ class FileUploadTest { @Test @Throws(Exception::class) fun twice() = runTest(before = { setUp() }, after = { tearDown() }) { - apolloClient.mutate(mutationTwice) + apolloClient.mutate(mutationTwice).execute() val request = mockServer.takeRequest() val parts = request.parts() @@ -107,7 +107,7 @@ class FileUploadTest { @Test @Throws(Exception::class) fun multiple() = runTest(before = { setUp() }, after = { tearDown() }) { - apolloClient.mutate(mutationMultiple) + apolloClient.mutate(mutationMultiple).execute() val request = mockServer.takeRequest() val parts = request.parts() @@ -122,7 +122,7 @@ class FileUploadTest { @Test @Throws(Exception::class) fun nested() = runTest(before = { setUp() }, after = { tearDown() }) { - apolloClient.mutate(mutationNested) + apolloClient.mutate(mutationNested).execute() val request = mockServer.takeRequest() val parts = request.parts() diff --git a/tests/integration-tests/src/commonTest/kotlin/test/HTTPHeadersTest.kt b/tests/integration-tests/src/commonTest/kotlin/test/HTTPHeadersTest.kt index dd10d84c1a..f50dcd4bd5 100644 --- a/tests/integration-tests/src/commonTest/kotlin/test/HTTPHeadersTest.kt +++ b/tests/integration-tests/src/commonTest/kotlin/test/HTTPHeadersTest.kt @@ -34,7 +34,7 @@ class HTTPHeadersTest { mockServer.enqueue(query, data) - val response = apolloClient.query(query) + val response = apolloClient.query(query).execute() assertNotNull(response.data) @@ -62,7 +62,7 @@ class HTTPHeadersTest { ) ) - val response = apolloClient.query(query) + val response = apolloClient.query(query).execute() assertEquals(response.executionContext[HttpInfo]?.headers?.valueOf("Header1"), "Value1") assertEquals(response.executionContext[HttpInfo]?.headers?.valueOf("Header2"), "Value2") diff --git a/tests/integration-tests/src/commonTest/kotlin/test/HttpInterceptorTest.kt b/tests/integration-tests/src/commonTest/kotlin/test/HttpInterceptorTest.kt index 826730d5a9..d24f88b4f0 100644 --- a/tests/integration-tests/src/commonTest/kotlin/test/HttpInterceptorTest.kt +++ b/tests/integration-tests/src/commonTest/kotlin/test/HttpInterceptorTest.kt @@ -34,6 +34,6 @@ class HttpInterceptorTest { mockServer.enqueue(readResource("HeroNameResponse.json")) - client.query(HeroNameQuery()) + client.query(HeroNameQuery()).execute() } } \ No newline at end of file diff --git a/tests/integration-tests/src/commonTest/kotlin/test/HttpRequestComposerTest.kt b/tests/integration-tests/src/commonTest/kotlin/test/HttpRequestComposerTest.kt index b1fbebf6af..0f782e10b4 100644 --- a/tests/integration-tests/src/commonTest/kotlin/test/HttpRequestComposerTest.kt +++ b/tests/integration-tests/src/commonTest/kotlin/test/HttpRequestComposerTest.kt @@ -42,7 +42,7 @@ class HttpRequestComposerTest { kotlin.runCatching { // No need to enqueue a successful response, we just want to make sure our headers reached the server mockServer.enqueue("error") - apolloClient.query(ApolloRequest.Builder(AllPlanetsQuery()).httpHeader("test", "is passing").build()) + apolloClient.query(AllPlanetsQuery()).httpHeader("test", "is passing").execute() } val response = mockServer.takeRequest() diff --git a/tests/integration-tests/src/commonTest/kotlin/test/JsonScalarTest.kt b/tests/integration-tests/src/commonTest/kotlin/test/JsonScalarTest.kt index 2cbcdf4631..1d6b43dfc1 100644 --- a/tests/integration-tests/src/commonTest/kotlin/test/JsonScalarTest.kt +++ b/tests/integration-tests/src/commonTest/kotlin/test/JsonScalarTest.kt @@ -2,7 +2,6 @@ package test import com.apollographql.apollo3.ApolloClient import com.apollographql.apollo3.api.AnyAdapter -import com.apollographql.apollo3.api.ApolloRequest import com.apollographql.apollo3.cache.normalized.ApolloStore import com.apollographql.apollo3.cache.normalized.FetchPolicy import com.apollographql.apollo3.cache.normalized.MemoryCacheFactory @@ -40,7 +39,7 @@ class JsonScalarTest { @Test fun jsonScalar() = runTest(before = { setUp() }, after = { tearDown() }) { mockServer.enqueue(readResource("JsonScalar.json")) - var response = apolloClient.query(GetJsonScalarQuery()) + var response = apolloClient.query(GetJsonScalarQuery()).execute() assertFalse(response.hasErrors()) var expectedMap = mapOf( @@ -53,8 +52,8 @@ class JsonScalarTest { * Update the json value, it should be replaced, not merged */ mockServer.enqueue(readResource("JsonScalarModified.json")) - apolloClient.query(ApolloRequest.Builder(GetJsonScalarQuery()).fetchPolicy(FetchPolicy.NetworkFirst).build()) - response = apolloClient.query(ApolloRequest.Builder(GetJsonScalarQuery()).fetchPolicy(FetchPolicy.CacheOnly).build()) + apolloClient.query(GetJsonScalarQuery()).fetchPolicy(FetchPolicy.NetworkFirst).execute() + response = apolloClient.query(GetJsonScalarQuery()).fetchPolicy(FetchPolicy.CacheOnly).execute() assertFalse(response.hasErrors()) diff --git a/tests/integration-tests/src/commonTest/kotlin/test/OptimisticCacheTest.kt b/tests/integration-tests/src/commonTest/kotlin/test/OptimisticCacheTest.kt index 4852dc2b6b..201de46e4b 100644 --- a/tests/integration-tests/src/commonTest/kotlin/test/OptimisticCacheTest.kt +++ b/tests/integration-tests/src/commonTest/kotlin/test/OptimisticCacheTest.kt @@ -2,7 +2,6 @@ package test import IdObjectIdGenerator import com.apollographql.apollo3.ApolloClient -import com.apollographql.apollo3.api.ApolloRequest import com.apollographql.apollo3.api.Optional import com.apollographql.apollo3.cache.normalized.ApolloStore import com.apollographql.apollo3.cache.normalized.FetchPolicy @@ -56,7 +55,7 @@ class OptimisticCacheTest { val query = HeroAndFriendsNamesQuery(Episode.JEDI) mockServer.enqueue(readResource("HeroAndFriendsNameResponse.json")) - apolloClient.query(ApolloRequest.Builder(query).fetchPolicy(FetchPolicy.NetworkOnly).build()) + apolloClient.query(query).fetchPolicy(FetchPolicy.NetworkOnly).execute() val mutationId = uuid4() val data = HeroAndFriendsNamesQuery.Data(HeroAndFriendsNamesQuery.Hero( @@ -76,7 +75,7 @@ class OptimisticCacheTest { mutationId = mutationId, publish = true) - var response = apolloClient.query(ApolloRequest.Builder(query).fetchPolicy(FetchPolicy.CacheOnly).build()) + var response = apolloClient.query(query).fetchPolicy(FetchPolicy.CacheOnly).execute() assertEquals(response.data?.hero?.name, "R222-D222") assertEquals(response.data?.hero?.friends?.size, 2) @@ -84,7 +83,7 @@ class OptimisticCacheTest { assertEquals(response.data?.hero?.friends?.get(1)?.name, "Batman") store.rollbackOptimisticUpdates(mutationId, false) - response = apolloClient.query(ApolloRequest.Builder(query).fetchPolicy(FetchPolicy.CacheOnly).build()) + response = apolloClient.query(query).fetchPolicy(FetchPolicy.CacheOnly).execute() assertEquals(response.data?.hero?.name, "R2-D2") assertEquals(response.data?.hero?.friends?.size, 3) @@ -103,7 +102,7 @@ class OptimisticCacheTest { // execute query1 from the network mockServer.enqueue(readResource("HeroAndFriendsNameWithIdsResponse.json")) - apolloClient.query(ApolloRequest.Builder(query1).fetchPolicy(FetchPolicy.NetworkOnly).build()) + apolloClient.query(query1).fetchPolicy(FetchPolicy.NetworkOnly).execute() // now write some optimistic updates for query1 val data1 = HeroAndFriendsNamesWithIDsQuery.Data( @@ -129,7 +128,7 @@ class OptimisticCacheTest { publish = true) // check if query1 see optimistic updates - var response1 = apolloClient.query(ApolloRequest.Builder(query1).fetchPolicy(FetchPolicy.CacheOnly).build()) + var response1 = apolloClient.query(query1).fetchPolicy(FetchPolicy.CacheOnly).execute() assertEquals(response1.data?.hero?.id, "2001") assertEquals(response1.data?.hero?.name, "R222-D222") assertEquals(response1.data?.hero?.friends?.size, 2) @@ -143,7 +142,7 @@ class OptimisticCacheTest { val mutationId2 = uuid4() mockServer.enqueue(readResource("HeroNameWithIdResponse.json")) - apolloClient.query(query2) + apolloClient.query(query2).execute() // write optimistic data2 val data2 = HeroNameWithIdQuery.Data(HeroNameWithIdQuery.Hero( @@ -157,7 +156,7 @@ class OptimisticCacheTest { publish = true) // check if query1 sees data2 - response1 = apolloClient.query(ApolloRequest.Builder(query1).fetchPolicy(FetchPolicy.CacheOnly).build()) + response1 = apolloClient.query(query1).fetchPolicy(FetchPolicy.CacheOnly).execute() assertEquals(response1.data?.hero?.id, "2001") assertEquals(response1.data?.hero?.name, "R222-D222") assertEquals(response1.data?.hero?.friends?.size, 2) @@ -167,7 +166,7 @@ class OptimisticCacheTest { assertEquals(response1.data?.hero?.friends?.get(1)?.name, "Batman") // check if query2 sees data2 - var response2 = apolloClient.query(ApolloRequest.Builder(query2).fetchPolicy(FetchPolicy.CacheOnly).build()) + var response2 = apolloClient.query(query2).fetchPolicy(FetchPolicy.CacheOnly).execute() assertEquals(response2.data?.hero?.id, "1000") assertEquals(response2.data?.hero?.name, "Beast") @@ -175,7 +174,7 @@ class OptimisticCacheTest { store.rollbackOptimisticUpdates(mutationId1, false) // check if query2 sees the rollback - response1 = apolloClient.query(ApolloRequest.Builder(query1).fetchPolicy(FetchPolicy.CacheOnly).build()) + response1 = apolloClient.query(query1).fetchPolicy(FetchPolicy.CacheOnly).execute() assertEquals(response1.data?.hero?.id, "2001") assertEquals(response1.data?.hero?.name, "R2-D2") assertEquals(response1.data?.hero?.friends?.size, 3) @@ -187,7 +186,7 @@ class OptimisticCacheTest { assertEquals(response1.data?.hero?.friends?.get(2)?.name, "Leia Organa") // check if query2 see the latest optimistic updates - response2 = apolloClient.query(ApolloRequest.Builder(query2).fetchPolicy(FetchPolicy.CacheOnly).build()) + response2 = apolloClient.query(query2).fetchPolicy(FetchPolicy.CacheOnly).execute() assertEquals(response2.data?.hero?.id, "1000") assertEquals(response2.data?.hero?.name, "Beast") @@ -195,7 +194,7 @@ class OptimisticCacheTest { store.rollbackOptimisticUpdates(mutationId2, false) // check if query2 see the latest optimistic updates - response2 = apolloClient.query(ApolloRequest.Builder(query2).fetchPolicy(FetchPolicy.CacheOnly).build()) + response2 = apolloClient.query(query2).fetchPolicy(FetchPolicy.CacheOnly).execute() assertEquals(response2.data?.hero?.id, "1000") assertEquals(response2.data?.hero?.name, "SuperMan") } @@ -205,14 +204,13 @@ class OptimisticCacheTest { mockServer.enqueue(readResource("ReviewsEmpireEpisodeResponse.json")) val channel = Channel() val job = launch { - apolloClient.watch( - ApolloRequest.Builder(ReviewsByEpisodeQuery(Episode.EMPIRE)) - .fetchPolicy(FetchPolicy.NetworkOnly) - .refetchPolicy(FetchPolicy.CacheOnly) - .build() - ).collect { - channel.send(it.data) - } + apolloClient.query(ReviewsByEpisodeQuery(Episode.EMPIRE)) + .fetchPolicy(FetchPolicy.NetworkOnly) + .refetchPolicy(FetchPolicy.CacheOnly) + .watch() + .collect { + channel.send(it.data) + } } var watcherData = channel.receive() @@ -248,18 +246,15 @@ class OptimisticCacheTest { ) ) ) - apolloClient.mutate( - ApolloRequest.Builder(updateReviewMutation).optimisticUpdates( - UpdateReviewMutation.Data( - UpdateReviewMutation.UpdateReview( - "empireReview2", - 5, - "Great" - ) + apolloClient.mutate(updateReviewMutation).optimisticUpdates( + UpdateReviewMutation.Data( + UpdateReviewMutation.UpdateReview( + "empireReview2", + 5, + "Great" ) ) - .build() - ) + ).execute() /** * optimistic updates @@ -301,10 +296,10 @@ class OptimisticCacheTest { val mutationId2 = uuid4() mockServer.enqueue(readResource("HeroAndFriendsNameWithIdsResponse.json")) - apolloClient.query(query1) + apolloClient.query(query1).execute() mockServer.enqueue(readResource("HeroNameWithIdResponse.json")) - apolloClient.query(query2) + apolloClient.query(query2).execute() val data1 = HeroAndFriendsNamesWithIDsQuery.Data( HeroAndFriendsNamesWithIDsQuery.Hero( @@ -338,7 +333,7 @@ class OptimisticCacheTest { publish = true) // check if query1 see optimistic updates - var response1 = apolloClient.query(ApolloRequest.Builder(query1).fetchPolicy(FetchPolicy.CacheOnly).build()) + var response1 = apolloClient.query(query1).fetchPolicy(FetchPolicy.CacheOnly).execute() assertEquals(response1.data?.hero?.id, "2001") assertEquals(response1.data?.hero?.name, "R222-D222") assertEquals(response1.data?.hero?.friends?.size, 2) @@ -349,7 +344,7 @@ class OptimisticCacheTest { // check if query2 see the latest optimistic updates - var response2 = apolloClient.query(ApolloRequest.Builder(query2).fetchPolicy(FetchPolicy.CacheOnly).build()) + var response2 = apolloClient.query(query2).fetchPolicy(FetchPolicy.CacheOnly).execute() assertEquals(response2.data?.hero?.id, "1000") assertEquals(response2.data?.hero?.name, "Spiderman") @@ -357,7 +352,7 @@ class OptimisticCacheTest { store.rollbackOptimisticUpdates(mutationId2, false) // check if query1 see the latest optimistic updates - response1 = apolloClient.query(ApolloRequest.Builder(query1).fetchPolicy(FetchPolicy.CacheOnly).build()) + response1 = apolloClient.query(query1).fetchPolicy(FetchPolicy.CacheOnly).execute() assertEquals(response1.data?.hero?.id, "2001") assertEquals(response1.data?.hero?.name, "R222-D222") assertEquals(response1.data?.hero?.friends?.size, 2) @@ -368,7 +363,7 @@ class OptimisticCacheTest { // check if query2 see the latest optimistic updates - response2 = apolloClient.query(ApolloRequest.Builder(query2).fetchPolicy(FetchPolicy.CacheOnly).build()) + response2 = apolloClient.query(query2).fetchPolicy(FetchPolicy.CacheOnly).execute() assertEquals(response2.data?.hero?.id, "1000") assertEquals(response2.data?.hero?.name, "Robocop") @@ -376,7 +371,7 @@ class OptimisticCacheTest { store.rollbackOptimisticUpdates(mutationId1, false) // check if query1 see the latest non-optimistic updates - response1 = apolloClient.query(ApolloRequest.Builder(query1).fetchPolicy(FetchPolicy.CacheOnly).build()) + response1 = apolloClient.query(query1).fetchPolicy(FetchPolicy.CacheOnly).execute() assertEquals(response1.data?.hero?.id, "2001") assertEquals(response1.data?.hero?.name, "R2-D2") assertEquals(response1.data?.hero?.friends?.size, 3) @@ -389,7 +384,7 @@ class OptimisticCacheTest { // check if query2 see the latest non-optimistic updates - response2 = apolloClient.query(ApolloRequest.Builder(query2).fetchPolicy(FetchPolicy.CacheOnly).build()) + response2 = apolloClient.query(query2).fetchPolicy(FetchPolicy.CacheOnly).execute() assertEquals(response2.data?.hero?.id, "1000") assertEquals(response2.data?.hero?.name, "SuperMan") } diff --git a/tests/integration-tests/src/commonTest/kotlin/test/OtherCacheTest.kt b/tests/integration-tests/src/commonTest/kotlin/test/OtherCacheTest.kt index 152a0b27e0..1f738e365d 100644 --- a/tests/integration-tests/src/commonTest/kotlin/test/OtherCacheTest.kt +++ b/tests/integration-tests/src/commonTest/kotlin/test/OtherCacheTest.kt @@ -4,7 +4,6 @@ import IdCacheResolver import IdObjectIdGenerator import assertEquals2 import com.apollographql.apollo3.ApolloClient -import com.apollographql.apollo3.api.ApolloRequest import com.apollographql.apollo3.cache.normalized.ApolloStore import com.apollographql.apollo3.cache.normalized.FetchPolicy import com.apollographql.apollo3.cache.normalized.MemoryCacheFactory @@ -49,14 +48,15 @@ class OtherCacheTest { fun masterDetailSuccess() = runTest(before = { setUp() }, after = { tearDown() }) { // Store a query that contains all data mockServer.enqueue(readResource("HeroAndFriendsNameWithIdsResponse.json")) - apolloClient.query( - ApolloRequest.Builder(HeroAndFriendsNamesWithIDsQuery(Episode.NEWHOPE)).fetchPolicy(FetchPolicy.NetworkOnly).build() - ) + apolloClient.query(HeroAndFriendsNamesWithIDsQuery(Episode.NEWHOPE)) + .fetchPolicy(FetchPolicy.NetworkOnly) + .execute() // Getting a subtree of that data should work - val detailsResponse = apolloClient.query( - ApolloRequest.Builder(CharacterNameByIdQuery("1002")).fetchPolicy(FetchPolicy.CacheOnly).build() - ) + val detailsResponse = apolloClient.query(CharacterNameByIdQuery("1002")) + .fetchPolicy(FetchPolicy.CacheOnly) + .execute() + assertEquals(detailsResponse.data?.character!!.name, "Han Solo") } @@ -66,15 +66,13 @@ class OtherCacheTest { fun masterDetailFailIncomplete() = runTest(before = { setUp() }, after = { tearDown() }) { // Store a query that contains all data mockServer.enqueue(readResource("HeroAndFriendsNameWithIdsResponse.json")) - apolloClient.query( - ApolloRequest.Builder(HeroAndFriendsNamesWithIDsQuery(Episode.NEWHOPE)).fetchPolicy(FetchPolicy.NetworkOnly).build() - ) + apolloClient.query(HeroAndFriendsNamesWithIDsQuery(Episode.NEWHOPE)) + .fetchPolicy(FetchPolicy.NetworkOnly) + .execute() // Some details are not present in the master query, we should get a cache miss try { - apolloClient.query( - ApolloRequest.Builder(CharacterDetailsQuery("1002")).fetchPolicy(FetchPolicy.CacheOnly).build() - ) + apolloClient.query(CharacterDetailsQuery("1002")).fetchPolicy(FetchPolicy.CacheOnly).execute() fail("we expected a cache miss") } catch (e: CacheMissException) { assertTrue(e.message!!.contains("Object '1002' has no field named '__typename'")) @@ -85,9 +83,9 @@ class OtherCacheTest { @Test fun cacheMissThrows() = runTest(before = { setUp() }, after = { tearDown() }) { try { - apolloClient.query( - ApolloRequest.Builder(EpisodeHeroNameQuery(Episode.EMPIRE)).fetchPolicy(FetchPolicy.CacheOnly).build() - ) + apolloClient.query(EpisodeHeroNameQuery(Episode.EMPIRE)) + .fetchPolicy(FetchPolicy.CacheOnly) + .execute() fail("we expected a cache miss") } catch (e: CacheMissException) { assertTrue(e.message!!.contains("Object 'QUERY_ROOT' has no field named 'hero")) @@ -98,17 +96,15 @@ class OtherCacheTest { @Throws(Exception::class) fun skipIncludeDirective() = runTest(before = { setUp() }, after = { tearDown() }) { mockServer.enqueue(readResource("HeroAndFriendsNameResponse.json")) - apolloClient.query( - ApolloRequest.Builder(HeroAndFriendsDirectivesQuery(Episode.JEDI, true, false)) - .fetchPolicy(FetchPolicy.NetworkOnly) - .build() - ) + apolloClient.query(HeroAndFriendsDirectivesQuery(Episode.JEDI, true, false)) + .fetchPolicy(FetchPolicy.NetworkOnly) + .execute() var response = apolloClient.query( - ApolloRequest.Builder(HeroAndFriendsDirectivesQuery(Episode.JEDI, true, false)) - .fetchPolicy(FetchPolicy.CacheOnly) - .build() - ) + HeroAndFriendsDirectivesQuery(Episode.JEDI, true, false)) + .fetchPolicy(FetchPolicy.CacheOnly) + .execute() + assertEquals2(response.data?.hero?.name, "R2-D2") assertEquals2(response.data?.hero?.friends?.size, 3) assertEquals2(response.data?.hero?.friends?.get(0)?.name, "Luke Skywalker") @@ -116,10 +112,10 @@ class OtherCacheTest { assertEquals2(response.data?.hero?.friends?.get(2)?.name, "Leia Organa") response = apolloClient.query( - ApolloRequest.Builder(HeroAndFriendsDirectivesQuery(Episode.JEDI, false, false)) - .fetchPolicy(FetchPolicy.CacheOnly) - .build() - ) + HeroAndFriendsDirectivesQuery(Episode.JEDI, false, false)) + .fetchPolicy(FetchPolicy.CacheOnly) + .execute() + assertNull(response.data?.hero?.name) assertEquals2(response.data?.hero?.friends?.size, 3) assertEquals2(response.data?.hero?.friends?.get(0)?.name, "Luke Skywalker") @@ -127,10 +123,10 @@ class OtherCacheTest { assertEquals2(response.data?.hero?.friends?.get(2)?.name, "Leia Organa") response = apolloClient.query( - ApolloRequest.Builder(HeroAndFriendsDirectivesQuery(Episode.JEDI, true, true)) - .fetchPolicy(FetchPolicy.CacheOnly) - .build() - ) + HeroAndFriendsDirectivesQuery(Episode.JEDI, true, true)) + .fetchPolicy(FetchPolicy.CacheOnly) + .execute() + assertEquals2(response.data?.hero?.name, "R2-D2") assertNull(response.data?.hero?.friends) } @@ -140,32 +136,25 @@ class OtherCacheTest { fun skipIncludeDirectiveUnsatisfiedCache() = runTest(before = { setUp() }, after = { tearDown() }) { // Store a response that doesn't contain friends mockServer.enqueue(readResource("HeroNameResponse.json")) - apolloClient.query( - ApolloRequest.Builder( - HeroAndFriendsDirectivesQuery(Episode.JEDI, true, true) - ).fetchPolicy(FetchPolicy.NetworkOnly) - .build() - ) + apolloClient.query(HeroAndFriendsDirectivesQuery(Episode.JEDI, true, true)) + .fetchPolicy(FetchPolicy.NetworkOnly) + .execute() + // Get it from the cache, we should get the name but no friends - val response = apolloClient.query( - ApolloRequest.Builder( - HeroAndFriendsDirectivesQuery(Episode.JEDI, true, true) - ).fetchPolicy(FetchPolicy.CacheOnly) - .build() - ) + val response = apolloClient.query(HeroAndFriendsDirectivesQuery(Episode.JEDI, true, true)) + .fetchPolicy(FetchPolicy.CacheOnly) + .execute() assertEquals(response.data?.hero?.name, "R2-D2") assertEquals(response.data?.hero?.friends, null) // Now try to get the friends from the cache, it should fail try { - apolloClient.query( - ApolloRequest.Builder( - HeroAndFriendsDirectivesQuery(Episode.JEDI, true, false) - ).fetchPolicy(FetchPolicy.CacheOnly) - .build() - ) + apolloClient.query(HeroAndFriendsDirectivesQuery(Episode.JEDI, true, false)) + .fetchPolicy(FetchPolicy.CacheOnly) + .execute() + fail("A CacheMissException was expected") } catch (e: CacheMissException) { assertTrue(e.message!!.contains("has no field named 'friends'")) diff --git a/tests/integration-tests/src/commonTest/kotlin/test/StoreTest.kt b/tests/integration-tests/src/commonTest/kotlin/test/StoreTest.kt index b25f14d135..55b0598693 100644 --- a/tests/integration-tests/src/commonTest/kotlin/test/StoreTest.kt +++ b/tests/integration-tests/src/commonTest/kotlin/test/StoreTest.kt @@ -4,7 +4,6 @@ import IdCacheResolver import IdObjectIdGenerator import assertEquals2 import com.apollographql.apollo3.ApolloClient -import com.apollographql.apollo3.api.ApolloRequest import com.apollographql.apollo3.cache.normalized.ApolloStore import com.apollographql.apollo3.cache.normalized.CacheKey import com.apollographql.apollo3.cache.normalized.FetchPolicy @@ -130,27 +129,28 @@ class StoreTest { private suspend fun storeAllFriends() { mockServer.enqueue(readResource("HeroAndFriendsNameWithIdsResponse.json")) - val response = apolloClient.query( - ApolloRequest.Builder(HeroAndFriendsNamesWithIDsQuery(Episode.NEWHOPE)).fetchPolicy(FetchPolicy.NetworkOnly).build() - ) + val response = apolloClient.query(HeroAndFriendsNamesWithIDsQuery(Episode.NEWHOPE)) + .fetchPolicy(FetchPolicy.NetworkOnly).execute() assertEquals(response.data?.hero?.name, "R2-D2") assertEquals(response.data?.hero?.friends?.size, 3) } private suspend fun assertFriendIsCached(id: String, name: String) { - val characterResponse = apolloClient.query( - ApolloRequest.Builder(CharacterNameByIdQuery(id)).fetchPolicy(FetchPolicy.CacheOnly).build() - ) + val characterResponse = apolloClient.query(CharacterNameByIdQuery(id)) + .fetchPolicy(FetchPolicy.CacheOnly) + .execute() + assertEquals2(characterResponse.isFromCache, true) assertEquals2(characterResponse.data?.character?.name, name) } private suspend fun assertFriendIsNotCached(id: String) { try { - apolloClient.query( - ApolloRequest.Builder(CharacterNameByIdQuery(id)).fetchPolicy(FetchPolicy.CacheOnly).build() - ) + apolloClient.query(CharacterNameByIdQuery(id)) + .fetchPolicy(FetchPolicy.CacheOnly) + .execute() + fail("A CacheMissException was expected") } catch (e: CacheMissException) { } @@ -158,9 +158,10 @@ class StoreTest { private suspend fun assertRootNotCached() { try { - apolloClient.query( - ApolloRequest.Builder(HeroAndFriendsNamesWithIDsQuery(Episode.NEWHOPE)).fetchPolicy(FetchPolicy.CacheOnly).build() - ) + apolloClient.query(HeroAndFriendsNamesWithIDsQuery(Episode.NEWHOPE)) + .fetchPolicy(FetchPolicy.CacheOnly) + .execute() + fail("A CacheMissException was expected") } catch (e: CacheMissException) { } diff --git a/tests/integration-tests/src/commonTest/kotlin/test/WatcherTest.kt b/tests/integration-tests/src/commonTest/kotlin/test/WatcherTest.kt index 7dd261473a..51813639c1 100644 --- a/tests/integration-tests/src/commonTest/kotlin/test/WatcherTest.kt +++ b/tests/integration-tests/src/commonTest/kotlin/test/WatcherTest.kt @@ -2,7 +2,6 @@ package test import IdObjectIdGenerator import com.apollographql.apollo3.ApolloClient -import com.apollographql.apollo3.api.ApolloRequest import com.apollographql.apollo3.api.CustomScalarAdapters import com.apollographql.apollo3.cache.CacheHeaders import com.apollographql.apollo3.cache.normalized.ApolloStore @@ -57,7 +56,7 @@ class WatcherTest { // The first query should get a "R2-D2" name mockServer.enqueue(readResource("EpisodeHeroNameResponseWithId.json")) val job = launch { - apolloClient.watch(query).collect { + apolloClient.query(query).watch().collect { channel.send(it.data) } } @@ -66,7 +65,7 @@ class WatcherTest { // Another newer call gets updated information with "Artoo" mockServer.enqueue(readResource("EpisodeHeroNameResponseNameChange.json")) - apolloClient.query(ApolloRequest.Builder(query).fetchPolicy(FetchPolicy.NetworkOnly).build()) + apolloClient.query(query).fetchPolicy(FetchPolicy.NetworkOnly).execute() assertEquals(channel.receiveOrTimeout()?.hero?.name, "Artoo") @@ -83,7 +82,7 @@ class WatcherTest { val operation = EpisodeHeroNameWithIdQuery(Episode.EMPIRE) mockServer.enqueue(readResource("EpisodeHeroNameResponseWithId.json")) val job = launch { - apolloClient.watch(operation).collect { + apolloClient.query(operation).watch().collect { channel.send(it.data) } } @@ -116,7 +115,7 @@ class WatcherTest { // The first query should get a "R2-D2" name mockServer.enqueue(readResource("EpisodeHeroNameResponseWithId.json")) val job = launch { - apolloClient.watch(query).collect { + apolloClient.query(query).watch().collect { channel.send(it.data) } } @@ -125,7 +124,7 @@ class WatcherTest { // Another newer call gets the same name (R2-D2) mockServer.enqueue(readResource("EpisodeHeroNameResponseWithId.json")) - apolloClient.query(ApolloRequest.Builder(query).fetchPolicy(FetchPolicy.NetworkOnly).build()) + apolloClient.query(query).fetchPolicy(FetchPolicy.NetworkOnly).execute() channel.assertEmpty() @@ -142,7 +141,7 @@ class WatcherTest { // The first query should get a "R2-D2" name mockServer.enqueue(readResource("EpisodeHeroNameResponseWithId.json")) val job = launch { - apolloClient.watch(EpisodeHeroNameWithIdQuery(Episode.EMPIRE)).collect { + apolloClient.query(EpisodeHeroNameWithIdQuery(Episode.EMPIRE)).watch().collect { channel.send(it.data) } } @@ -151,12 +150,10 @@ class WatcherTest { // Another newer call gets updated information with "Artoo" mockServer.enqueue(readResource("HeroAndFriendsNameWithIdsNameChange.json")) - apolloClient.query( - ApolloRequest.Builder( - HeroAndFriendsNamesWithIDsQuery(Episode.NEWHOPE) - ).fetchPolicy(FetchPolicy.NetworkOnly) - .build() - ) + apolloClient.query(HeroAndFriendsNamesWithIDsQuery(Episode.NEWHOPE)) + .fetchPolicy(FetchPolicy.NetworkOnly) + .execute() + assertEquals(channel.receiveOrTimeout()?.hero?.name, "Artoo") @@ -173,7 +170,7 @@ class WatcherTest { // The first query should get a "R2-D2" name mockServer.enqueue(readResource("EpisodeHeroNameResponseWithId.json")) val job = launch { - apolloClient.watch(EpisodeHeroNameQuery(Episode.EMPIRE)).collect { + apolloClient.query(EpisodeHeroNameQuery(Episode.EMPIRE)).watch().collect { channel.send(it.data) } } @@ -182,12 +179,9 @@ class WatcherTest { // Another newer call gets the same information mockServer.enqueue(readResource("HeroAndFriendsNameWithIdsResponse.json")) - apolloClient.query( - ApolloRequest.Builder( - HeroAndFriendsNamesWithIDsQuery(Episode.NEWHOPE) - ).fetchPolicy(FetchPolicy.NetworkOnly) - .build() - ) + apolloClient.query(HeroAndFriendsNamesWithIDsQuery(Episode.NEWHOPE)) + .fetchPolicy(FetchPolicy.NetworkOnly) + .execute() channel.assertEmpty() @@ -205,13 +199,12 @@ class WatcherTest { // The first query should get a "R2-D2" name mockServer.enqueue(readResource("EpisodeHeroNameResponseWithId.json")) val job = launch { - val request = ApolloRequest.Builder(EpisodeHeroNameQuery(Episode.EMPIRE)) + apolloClient.query(EpisodeHeroNameQuery(Episode.EMPIRE)) .fetchPolicy(FetchPolicy.NetworkOnly) .refetchPolicy(FetchPolicy.NetworkOnly) - .build() - apolloClient.watch(request).collect { - channel.send(it.data) - } + .watch().collect { + channel.send(it.data) + } } assertEquals(channel.receiveOrTimeout()?.hero?.name, "R2-D2") @@ -224,12 +217,10 @@ class WatcherTest { // - Because the network only watcher will also store in the cache a different name value, it will trigger itself again // Enqueue a stable response to avoid errors during tests mockServer.enqueue(readResource("EpisodeHeroNameResponseNameChangeTwo.json")) - val response = apolloClient.query( - ApolloRequest.Builder( - EpisodeHeroNameQuery(Episode.EMPIRE) - ).fetchPolicy(FetchPolicy.NetworkOnly) - .build() - ) + val response = apolloClient.query(EpisodeHeroNameQuery(Episode.EMPIRE)) + .fetchPolicy(FetchPolicy.NetworkOnly) + .execute() + assertEquals(response.data?.hero?.name, "Artoo") // The watcher should see "ArTwo" @@ -246,13 +237,13 @@ class WatcherTest { // The first query should get a "R2-D2" name mockServer.enqueue(readResource("EpisodeHeroNameResponseWithId.json")) val job = launch { - val request = ApolloRequest.Builder(EpisodeHeroNameQuery(Episode.EMPIRE)) + apolloClient.query(EpisodeHeroNameQuery(Episode.EMPIRE)) .fetchPolicy(FetchPolicy.NetworkOnly) .refetchPolicy(FetchPolicy.NetworkOnly) - .build() - apolloClient.watch(request).collect { - channel.send(it.data) - } + .watch() + .collect { + channel.send(it.data) + } } job.cancelAndJoin() @@ -275,21 +266,23 @@ class WatcherTest { * Doing the initial query as cache only will detect when the query becomes available */ @Test - fun cacheOnlyFetchPolicy() = runTest(before = { setUp() }, after = { tearDown() }) { + fun cacheOnlyFetchPolicy() = runTest(before = { setUp() }, after = { tearDown() }) { val query = EpisodeHeroNameQuery(Episode.EMPIRE) val channel = Channel() // This will initially miss as the cache should be empty val job = launch { - val request = ApolloRequest.Builder(query).fetchPolicy(FetchPolicy.CacheOnly).build() - apolloClient.watch(request).collect { - channel.send(it.data) - } + apolloClient.query(query) + .fetchPolicy(FetchPolicy.CacheOnly) + .watch() + .collect { + channel.send(it.data) + } } // Another newer call gets updated information with "R2-D2" mockServer.enqueue(readResource("EpisodeHeroNameResponseWithId.json")) - apolloClient.query(ApolloRequest.Builder(query).fetchPolicy(FetchPolicy.NetworkOnly).build()) + apolloClient.query(query).fetchPolicy(FetchPolicy.NetworkOnly).execute() assertEquals(channel.receiveOrTimeout()?.hero?.name, "R2-D2") @@ -300,14 +293,13 @@ class WatcherTest { fun queryWatcherWithCacheOnlyNeverGoesToTheNetwork() = runTest(before = { setUp() }, after = { tearDown() }) { val channel = Channel(capacity = Channel.UNLIMITED) val job = launch { - val request = ApolloRequest.Builder(EpisodeHeroNameQuery(Episode.EMPIRE)) + + apolloClient.query(EpisodeHeroNameQuery(Episode.EMPIRE)) .fetchPolicy(FetchPolicy.CacheOnly) .refetchPolicy(FetchPolicy.CacheOnly) - .build() - - apolloClient.watch(request).collect { - channel.send(it.data) - } + .watch().collect { + channel.send(it.data) + } } mockServer.enqueue(readResource("StarshipByIdResponse.json")) diff --git a/tests/integration-tests/src/commonTest/kotlin/test/batching/QueryBatchingTest.kt b/tests/integration-tests/src/commonTest/kotlin/test/batching/QueryBatchingTest.kt index 6d9e9a7def..df16f75fb7 100644 --- a/tests/integration-tests/src/commonTest/kotlin/test/batching/QueryBatchingTest.kt +++ b/tests/integration-tests/src/commonTest/kotlin/test/batching/QueryBatchingTest.kt @@ -4,7 +4,6 @@ import batching.GetLaunch2Query import batching.GetLaunchQuery import com.apollographql.apollo3.ApolloClient import com.apollographql.apollo3.api.AnyAdapter -import com.apollographql.apollo3.api.ApolloRequest import com.apollographql.apollo3.api.http.DefaultHttpRequestComposer import com.apollographql.apollo3.api.internal.json.BufferedSourceJsonReader import com.apollographql.apollo3.mockserver.MockServer @@ -49,10 +48,10 @@ class QueryBatchingTest { .build() val result1 = async { - apolloClient.query(GetLaunchQuery()) + apolloClient.query(GetLaunchQuery()).execute() } val result2 = async { - apolloClient.query(GetLaunch2Query()) + apolloClient.query(GetLaunch2Query()).execute() } assertEquals("83", result1.await().data?.launch?.id) assertEquals("84", result2.await().data?.launch?.id) @@ -77,13 +76,13 @@ class QueryBatchingTest { .build() val result1 = async { - apolloClient.query(GetLaunchQuery()) + apolloClient.query(GetLaunchQuery()).execute() } val result2 = async { // Make sure GetLaunch2Query gets executed after GetLaunchQuery as there is no guarantee otherwise // 300ms batchIntervalMillis and 50ms delay here should be enough. Increase values if some tests become flaky delay(50) - apolloClient.query(GetLaunch2Query()) + apolloClient.query(GetLaunch2Query()).execute() } assertEquals("83", result1.await().data?.launch?.id) @@ -119,12 +118,12 @@ class QueryBatchingTest { .build() val result1 = async { - apolloClient.query(GetLaunchQuery()) + apolloClient.query(GetLaunchQuery()).execute() } val result2 = async { // Wait for the first query to be executed delay(200) - apolloClient.query(GetLaunch2Query()) + apolloClient.query(GetLaunch2Query()).execute() } assertEquals("83", result1.await().data?.launch?.id) @@ -150,12 +149,12 @@ class QueryBatchingTest { .build() val result1 = async { - apolloClient.query(ApolloRequest.Builder(GetLaunchQuery()).canBeBatched(false).build()) + apolloClient.query(GetLaunchQuery()).canBeBatched(false).execute() } val result2 = async { // Make sure GetLaunch2Query gets executed after GetLaunchQuery as there is no guarantee otherwise delay(50) - apolloClient.query(GetLaunch2Query()) + apolloClient.query(GetLaunch2Query()).execute() } assertEquals("83", result1.await().data?.launch?.id) diff --git a/tests/integration-tests/src/jvmTest/kotlin/test/CookiesTest.kt b/tests/integration-tests/src/jvmTest/kotlin/test/CookiesTest.kt index ac45629bdd..ac4e3914a6 100644 --- a/tests/integration-tests/src/jvmTest/kotlin/test/CookiesTest.kt +++ b/tests/integration-tests/src/jvmTest/kotlin/test/CookiesTest.kt @@ -59,13 +59,13 @@ class CookiesTest { )) // first query should set the cookie - apolloClient.query(HeroNameQuery()) + apolloClient.query(HeroNameQuery()).execute() // consume the first request mockServer.takeRequest() mockServer.enqueue(json) // first query should send the cookie - apolloClient.query(HeroNameQuery()) + apolloClient.query(HeroNameQuery()).execute() val cookie = mockServer.takeRequest().headers["Cookie"] assertEquals("yummy_cookie=choco", cookie) diff --git a/tests/integration-tests/src/jvmTest/kotlin/test/WriteToCacheAsynchronouslyTest.kt b/tests/integration-tests/src/jvmTest/kotlin/test/WriteToCacheAsynchronouslyTest.kt index a01088de78..365d19ac7a 100644 --- a/tests/integration-tests/src/jvmTest/kotlin/test/WriteToCacheAsynchronouslyTest.kt +++ b/tests/integration-tests/src/jvmTest/kotlin/test/WriteToCacheAsynchronouslyTest.kt @@ -1,7 +1,6 @@ package test import com.apollographql.apollo3.ApolloClient -import com.apollographql.apollo3.api.ApolloRequest import com.apollographql.apollo3.cache.CacheHeaders import com.apollographql.apollo3.cache.normalized.ApolloStore import com.apollographql.apollo3.cache.normalized.MemoryCacheFactory @@ -12,7 +11,6 @@ import com.apollographql.apollo3.integration.normalizer.type.Episode import com.apollographql.apollo3.mockserver.MockServer import com.apollographql.apollo3.mockserver.enqueue import com.apollographql.apollo3.testing.runTest -import kotlinx.coroutines.CoroutineDispatcher import kotlinx.coroutines.asCoroutineDispatcher import readResource import java.util.concurrent.Executors @@ -53,11 +51,10 @@ class WriteToCacheAsynchronouslyTest { val query = HeroAndFriendsNamesQuery(Episode.JEDI) mockServer.enqueue(readResource("HeroAndFriendsNameResponse.json")) - apolloClient.query( - ApolloRequest.Builder(query) - .writeToCacheAsynchronously(true) - .build() - ) + apolloClient.query(query) + .writeToCacheAsynchronously(true) + .execute() + val record = store.accessCache { it.loadRecord(QUERY_ROOT_KEY, CacheHeaders.NONE) } assertNull(record) @@ -71,11 +68,9 @@ class WriteToCacheAsynchronouslyTest { val query = HeroAndFriendsNamesQuery(Episode.JEDI) mockServer.enqueue(readResource("HeroAndFriendsNameResponse.json")) - apolloClient.query( - ApolloRequest.Builder(query) - .writeToCacheAsynchronously(false) - .build() - ) + apolloClient.query(query) + .writeToCacheAsynchronously(false) + .execute() val record = store.accessCache { it.loadRecord(QUERY_ROOT_KEY, CacheHeaders.NONE) } assertNotNull(record) diff --git a/tests/java-tests/src/test/java/test/ClientTest.java b/tests/java-tests/src/test/java/test/ClientTest.java index ae468fe343..25a84aa7b9 100644 --- a/tests/java-tests/src/test/java/test/ClientTest.java +++ b/tests/java-tests/src/test/java/test/ClientTest.java @@ -4,9 +4,8 @@ import com.apollographql.apollo3.api.ApolloResponse; import com.apollographql.apollo3.mockserver.MockResponse; import com.apollographql.apollo3.mockserver.MockServer; -import com.apollographql.apollo3.rx2.Rx2ApolloClient; +import com.apollographql.apollo3.rx2.Rx2Apollo; import com.google.common.truth.Truth; -import io.reactivex.schedulers.Schedulers; import javatest.GetRandomQuery; import kotlin.coroutines.Continuation; import kotlin.coroutines.CoroutineContext; @@ -41,8 +40,8 @@ public void before() { @Test public void simple() { mockServer.enqueue(new MockResponse("{\"data\": {\"random\": 42}}")); - Rx2ApolloClient rx2ApolloClient = new Rx2ApolloClient(apolloClient, Schedulers.io()); - ApolloResponse response = rx2ApolloClient.query(new GetRandomQuery()).blockingGet(); + + ApolloResponse response = Rx2Apollo.rxSingle(apolloClient.query(new GetRandomQuery())).blockingGet(); Truth.assertThat(response.dataOrThrow().random).isEqualTo(42); } } diff --git a/tests/models-compat/src/commonTest/kotlin/test/BasicTest.kt b/tests/models-compat/src/commonTest/kotlin/test/BasicTest.kt index 4bdd5d16f1..b83f56f449 100644 --- a/tests/models-compat/src/commonTest/kotlin/test/BasicTest.kt +++ b/tests/models-compat/src/commonTest/kotlin/test/BasicTest.kt @@ -5,7 +5,6 @@ import codegen.models.HeroParentTypeDependentFieldQuery import codegen.models.MergedFieldWithSameShapeQuery import codegen.models.type.Episode import com.apollographql.apollo3.ApolloClient -import com.apollographql.apollo3.api.ApolloRequest import com.apollographql.apollo3.api.ApolloResponse import com.apollographql.apollo3.api.Query import com.apollographql.apollo3.cache.normalized.ApolloStore @@ -42,9 +41,9 @@ class BasicTest { private fun basicTest(resourceName: String, query: Query, block: ApolloResponse.() -> Unit) = runTest(before = { setUp() }, after = { tearDown() }) { mockServer.enqueue(readJson(resourceName)) - var response = apolloClient.query(ApolloRequest.Builder(query).fetchPolicy(FetchPolicy.NetworkOnly).build()) + var response = apolloClient.query(query).fetchPolicy(FetchPolicy.NetworkOnly).execute() response.block() - response = apolloClient.query(ApolloRequest.Builder(query).fetchPolicy(FetchPolicy.CacheOnly).build()) + response = apolloClient.query(query).fetchPolicy(FetchPolicy.CacheOnly).execute() response.block() } diff --git a/tests/models-compat/src/commonTest/kotlin/test/StoreTest.kt b/tests/models-compat/src/commonTest/kotlin/test/StoreTest.kt index ba7292ff91..c80e359b48 100644 --- a/tests/models-compat/src/commonTest/kotlin/test/StoreTest.kt +++ b/tests/models-compat/src/commonTest/kotlin/test/StoreTest.kt @@ -40,7 +40,7 @@ class StoreTest { @Test fun readFragmentFromStore() = runTest(before = { setUp() }, after = { tearDown() }) { mockServer.enqueue(readJson("HeroAndFriendsWithTypename.json")) - apolloClient.query(HeroAndFriendsWithTypenameQuery()) + apolloClient.query(HeroAndFriendsWithTypenameQuery()).execute() val heroWithFriendsFragment = store.readFragment( HeroWithFriendsFragmentImpl(), @@ -86,7 +86,7 @@ class StoreTest { fun fragments() = runTest(before = { setUp() }, after = { tearDown() }) { mockServer.enqueue(readJson("HeroAndFriendsNamesWithIDs.json")) val query = HeroAndFriendsWithFragmentsQuery() - var response = apolloClient.query(query) + var response = apolloClient.query(query).execute() assertEquals(response.data?.hero?.__typename, "Droid") assertEquals(response.data?.hero?.fragments?.heroWithFriendsFragment?.id, "2001") assertEquals(response.data?.hero?.fragments?.heroWithFriendsFragment?.name, "R2-D2") @@ -137,7 +137,7 @@ class StoreTest { ) // Values should have changed - response = apolloClient.query(query) + response = apolloClient.query(query).execute() assertEquals(response.data?.hero?.__typename, "Droid") assertEquals(response.data?.hero?.fragments?.heroWithFriendsFragment?.id, "2001") assertEquals(response.data?.hero?.fragments?.heroWithFriendsFragment?.name, "R222-D222") diff --git a/tests/models-operation-based/src/commonTest/kotlin/test/BasicTest.kt b/tests/models-operation-based/src/commonTest/kotlin/test/BasicTest.kt index ce6ed67f95..797496af11 100644 --- a/tests/models-operation-based/src/commonTest/kotlin/test/BasicTest.kt +++ b/tests/models-operation-based/src/commonTest/kotlin/test/BasicTest.kt @@ -5,7 +5,6 @@ import codegen.models.HeroParentTypeDependentFieldQuery import codegen.models.MergedFieldWithSameShapeQuery import codegen.models.type.Episode import com.apollographql.apollo3.ApolloClient -import com.apollographql.apollo3.api.ApolloRequest import com.apollographql.apollo3.api.ApolloResponse import com.apollographql.apollo3.api.Query import com.apollographql.apollo3.cache.normalized.ApolloStore @@ -42,9 +41,9 @@ class BasicTest { private fun basicTest(resourceName: String, query: Query, block: ApolloResponse.() -> Unit) = runTest(before = { setUp() }, after = { tearDown() }) { mockServer.enqueue(readJson(resourceName)) - var response = apolloClient.query(ApolloRequest.Builder(query).fetchPolicy(FetchPolicy.NetworkOnly).build()) + var response = apolloClient.query(query).fetchPolicy(FetchPolicy.NetworkOnly).execute() response.block() - response = apolloClient.query(ApolloRequest.Builder(query).fetchPolicy(FetchPolicy.CacheOnly).build()) + response = apolloClient.query(query).fetchPolicy(FetchPolicy.CacheOnly).execute() response.block() } diff --git a/tests/models-operation-based/src/commonTest/kotlin/test/StoreTest.kt b/tests/models-operation-based/src/commonTest/kotlin/test/StoreTest.kt index e026e6ef05..4ffc8e411b 100644 --- a/tests/models-operation-based/src/commonTest/kotlin/test/StoreTest.kt +++ b/tests/models-operation-based/src/commonTest/kotlin/test/StoreTest.kt @@ -40,7 +40,7 @@ class StoreTest { @Test fun readFragmentFromStore() = runTest(before = { setUp() }, after = { tearDown() }) { mockServer.enqueue(readJson("HeroAndFriendsWithTypename.json")) - apolloClient.query(HeroAndFriendsWithTypenameQuery()) + apolloClient.query(HeroAndFriendsWithTypenameQuery()).execute() val heroWithFriendsFragment = store.readFragment( HeroWithFriendsFragmentImpl(), @@ -86,7 +86,7 @@ class StoreTest { fun fragments() = runTest(before = { setUp() }, after = { tearDown() }) { mockServer.enqueue(readJson("HeroAndFriendsNamesWithIDs.json")) val query = HeroAndFriendsWithFragmentsQuery() - var response = apolloClient.query(query) + var response = apolloClient.query(query).execute() assertEquals(response.data?.hero?.__typename, "Droid") assertEquals(response.data?.hero?.heroWithFriendsFragment?.id, "2001") assertEquals(response.data?.hero?.heroWithFriendsFragment?.name, "R2-D2") @@ -133,7 +133,7 @@ class StoreTest { ) // Values should have changed - response = apolloClient.query(query) + response = apolloClient.query(query).execute() assertEquals(response.data?.hero?.__typename, "Droid") assertEquals(response.data?.hero?.heroWithFriendsFragment?.id, "2001") assertEquals(response.data?.hero?.heroWithFriendsFragment?.name, "R222-D222") diff --git a/tests/models-response-based/src/commonTest/kotlin/test/BasicTest.kt b/tests/models-response-based/src/commonTest/kotlin/test/BasicTest.kt index d182c990b1..d3156109d8 100644 --- a/tests/models-response-based/src/commonTest/kotlin/test/BasicTest.kt +++ b/tests/models-response-based/src/commonTest/kotlin/test/BasicTest.kt @@ -9,7 +9,6 @@ import codegen.models.MergedFieldWithSameShapeQuery.Data.Hero.Companion.asDroid import codegen.models.MergedFieldWithSameShapeQuery.Data.Hero.Companion.asHuman import codegen.models.type.Episode import com.apollographql.apollo3.ApolloClient -import com.apollographql.apollo3.api.ApolloRequest import com.apollographql.apollo3.api.ApolloResponse import com.apollographql.apollo3.api.Query import com.apollographql.apollo3.cache.normalized.ApolloStore @@ -46,9 +45,9 @@ class BasicTest { private fun basicTest(resourceName: String, query: Query, block: ApolloResponse.() -> Unit) = runTest(before = { setUp() }, after = { tearDown() }) { mockServer.enqueue(readJson(resourceName)) - var response = apolloClient.query(ApolloRequest.Builder(query).fetchPolicy(FetchPolicy.NetworkOnly).build()) + var response = apolloClient.query(query).fetchPolicy(FetchPolicy.NetworkOnly).execute() response.block() - response = apolloClient.query(ApolloRequest.Builder(query).fetchPolicy(FetchPolicy.CacheOnly).build()) + response = apolloClient.query(query).fetchPolicy(FetchPolicy.CacheOnly).execute() response.block() } diff --git a/tests/models-response-based/src/commonTest/kotlin/test/StoreTest.kt b/tests/models-response-based/src/commonTest/kotlin/test/StoreTest.kt index 5cb0e18ca1..b96ec05d4c 100644 --- a/tests/models-response-based/src/commonTest/kotlin/test/StoreTest.kt +++ b/tests/models-response-based/src/commonTest/kotlin/test/StoreTest.kt @@ -41,7 +41,7 @@ class StoreTest { @Test fun readFragmentFromStore() = runTest(before = { setUp() }, after = { tearDown() }) { mockServer.enqueue(readJson("HeroAndFriendsWithTypename.json")) - apolloClient.query(HeroAndFriendsWithTypenameQuery()) + apolloClient.query(HeroAndFriendsWithTypenameQuery()).execute() val heroWithFriendsFragment = store.readFragment( HeroWithFriendsFragmentImpl(), @@ -87,7 +87,7 @@ class StoreTest { fun fragments() = runTest(before = { setUp() }, after = { tearDown() }) { mockServer.enqueue(readJson("HeroAndFriendsNamesWithIDs.json")) val query = HeroAndFriendsWithFragmentsQuery() - var response = apolloClient.query(query) + var response = apolloClient.query(query).execute() assertEquals(response.data?.hero?.__typename, "Droid") assertEquals(response.data?.hero?.heroWithFriendsFragment()?.id, "2001") assertEquals(response.data?.hero?.heroWithFriendsFragment()?.name, "R2-D2") @@ -130,7 +130,7 @@ class StoreTest { ) // Values should have changed - response = apolloClient.query(query) + response = apolloClient.query(query).execute() assertEquals(response.data?.hero?.__typename, "Droid") assertEquals(response.data?.hero?.heroWithFriendsFragment()?.id, "2001") assertEquals(response.data?.hero?.heroWithFriendsFragment()?.name, "R222-D222") diff --git a/tests/rxjava/src/test/kotlin/RxJavaTest.kt b/tests/rxjava/src/test/kotlin/RxJavaTest.kt index 06592a003d..b38db980e4 100644 --- a/tests/rxjava/src/test/kotlin/RxJavaTest.kt +++ b/tests/rxjava/src/test/kotlin/RxJavaTest.kt @@ -1,5 +1,4 @@ import com.apollographql.apollo3.ApolloClient -import com.apollographql.apollo3.api.ApolloRequest import com.apollographql.apollo3.cache.normalized.ApolloStore import com.apollographql.apollo3.cache.normalized.FetchPolicy import com.apollographql.apollo3.cache.normalized.MemoryCacheFactory @@ -8,9 +7,8 @@ import com.apollographql.apollo3.cache.normalized.store import com.apollographql.apollo3.exception.ApolloException import com.apollographql.apollo3.mockserver.MockServer import com.apollographql.apollo3.mockserver.enqueue -import com.apollographql.apollo3.rx2.Rx2ApolloClient import com.apollographql.apollo3.rx2.Rx2ApolloStore -import com.apollographql.apollo3.rx2.toRx2ApolloClient +import com.apollographql.apollo3.rx2.rxSingle import com.apollographql.apollo3.rx2.toRx2ApolloStore import kotlinx.coroutines.runBlocking import rxjava.GetRandomQuery @@ -20,14 +18,14 @@ import kotlin.test.Test class RxJavaTest { private lateinit var mockServer: MockServer - private lateinit var rx2ApolloClient: Rx2ApolloClient + private lateinit var apolloClient: ApolloClient private lateinit var rx2ApolloStore: Rx2ApolloStore @BeforeTest fun setUp() { val store = ApolloStore(MemoryCacheFactory()) mockServer = MockServer() - rx2ApolloClient = ApolloClient.Builder().serverUrl(runBlocking { mockServer.url() }).store(store).build().toRx2ApolloClient() + apolloClient = ApolloClient.Builder().serverUrl(runBlocking { mockServer.url() }).store(store).build() rx2ApolloStore = store.toRx2ApolloStore() } @@ -43,7 +41,9 @@ class RxJavaTest { fun querySucceeds() { mockServer.enqueue(response) - rx2ApolloClient.query(GetRandomQuery()).test() + apolloClient.query(GetRandomQuery()) + .rxSingle() + .test() .awaitDone(1, TimeUnit.SECONDS) .assertComplete() .assertValue { @@ -55,7 +55,9 @@ class RxJavaTest { fun errorsAreReceived() { mockServer.enqueue("bad response") - rx2ApolloClient.query(GetRandomQuery()).test() + apolloClient.query(GetRandomQuery()) + .rxSingle() + .test() .awaitDone(1, TimeUnit.SECONDS) .assertError { it is ApolloException @@ -65,9 +67,10 @@ class RxJavaTest { @Test fun writingToTheStoreWorks() { rx2ApolloStore.rxWriteOperation(GetRandomQuery(), GetRandomQuery.Data(random = 43)).blockingGet() - rx2ApolloClient.query( - ApolloRequest.Builder(GetRandomQuery()).fetchPolicy(FetchPolicy.CacheOnly).build() - ).test() + apolloClient.query(GetRandomQuery()) + .fetchPolicy(FetchPolicy.CacheOnly) + .rxSingle() + .test() .awaitDone(1, TimeUnit.SECONDS) .assertValue { it.data?.random == 43 diff --git a/tests/websockets/src/commonTest/kotlin/AppSyncTest.kt b/tests/websockets/src/commonTest/kotlin/AppSyncTest.kt index 5232c9615a..f4e7604e91 100644 --- a/tests/websockets/src/commonTest/kotlin/AppSyncTest.kt +++ b/tests/websockets/src/commonTest/kotlin/AppSyncTest.kt @@ -1,10 +1,9 @@ + import appsync.CommentsSubscription import com.apollographql.apollo3.ApolloClient -import com.apollographql.apollo3.network.http.HttpNetworkTransport import com.apollographql.apollo3.network.ws.AppSyncWsProtocol import com.apollographql.apollo3.network.ws.WebSocketNetworkTransport import com.apollographql.apollo3.testing.runTest -import fullstack.tutorial.TripsBookedSubscription import kotlinx.coroutines.flow.collect import kotlin.test.Ignore import kotlin.test.Test @@ -37,7 +36,7 @@ class AppSyncTest { ) ) - apolloClient.subscribe(CommentsSubscription()) + apolloClient.subscribe(CommentsSubscription()).execute() .collect { println("comment: ${it.data?.subscribeToEventComments?.content}") } diff --git a/tests/websockets/src/commonTest/kotlin/FullStackTest.kt b/tests/websockets/src/commonTest/kotlin/FullStackTest.kt index 9777b3860d..4568feb492 100644 --- a/tests/websockets/src/commonTest/kotlin/FullStackTest.kt +++ b/tests/websockets/src/commonTest/kotlin/FullStackTest.kt @@ -22,6 +22,7 @@ class FullStackTest { .build() apolloClient.subscribe(TripsBookedSubscription()) + .execute() .collect { println("trips booked: ${it.data?.tripsBooked}") } diff --git a/tests/websockets/src/commonTest/kotlin/GraphQLWsTest.kt b/tests/websockets/src/commonTest/kotlin/GraphQLWsTest.kt index 2a453e6605..6b0f72caa1 100644 --- a/tests/websockets/src/commonTest/kotlin/GraphQLWsTest.kt +++ b/tests/websockets/src/commonTest/kotlin/GraphQLWsTest.kt @@ -25,7 +25,7 @@ class GraphQLWsTest { ) .build() - assertEquals("Hello World!", apolloClient.query(HelloQuery()).data?.hello) + assertEquals("Hello World!", apolloClient.query(HelloQuery()).execute().data?.hello) } @Test @@ -39,7 +39,7 @@ class GraphQLWsTest { ) .build() - assertEquals("Hello Mutation!", apolloClient.mutate(SetHelloMutation()).data?.hello) + assertEquals("Hello Mutation!", apolloClient.mutate(SetHelloMutation()).execute().data?.hello) } @@ -54,7 +54,9 @@ class GraphQLWsTest { ) .build() - val list = apolloClient.subscribe(GreetingsSubscription()).toList() + val list = apolloClient.subscribe(GreetingsSubscription()) + .execute() + .toList() assertEquals(listOf("Hi", "Bonjour", "Hola", "Ciao", "Zdravo"), list.map { it.data?.greetings }) } } \ No newline at end of file diff --git a/tests/websockets/src/jvmTest/kotlin/CachedSubscriptionTest.kt b/tests/websockets/src/jvmTest/kotlin/CachedSubscriptionTest.kt index e454cee99b..1d31bdffdf 100644 --- a/tests/websockets/src/jvmTest/kotlin/CachedSubscriptionTest.kt +++ b/tests/websockets/src/jvmTest/kotlin/CachedSubscriptionTest.kt @@ -56,7 +56,8 @@ class CachedSubscriptionTest { runBlocking { val channel = Channel() val job = launch { - apolloClient.watch(TimeQuery()) + apolloClient.query(TimeQuery()) + .watch() .map { it.data!!.time } .collect { channel.send(it) @@ -68,6 +69,7 @@ class CachedSubscriptionTest { println("starting subscription") apolloClient.subscribe(TimeSubscription()) + .execute() .take(3) .map { it.data!!.time } .collect { diff --git a/tests/websockets/src/jvmTest/kotlin/SampleServerTest.kt b/tests/websockets/src/jvmTest/kotlin/SampleServerTest.kt index d776d1aa3c..6abc64a0f5 100644 --- a/tests/websockets/src/jvmTest/kotlin/SampleServerTest.kt +++ b/tests/websockets/src/jvmTest/kotlin/SampleServerTest.kt @@ -53,6 +53,7 @@ class SampleServerTest { runBlocking { val list = apolloClient.subscribe(CountSubscription(5, 0)) + .execute() .map { it.data!!.count } .toList() assertEquals(0.until(5).toList(), list) @@ -73,12 +74,14 @@ class SampleServerTest { val items = mutableListOf() launch { apolloClient.subscribe(CountSubscription(5, 1000)) + .execute() .collect { items.add(it.data!!.count * 2) } } delay(500) apolloClient.subscribe(CountSubscription(5, 1000)) + .execute() .collect { items.add(2 * it.data!!.count + 1) } @@ -97,14 +100,14 @@ class SampleServerTest { .build() runBlocking { - apolloClient.subscribe(CountSubscription(50, 1000)).first() + apolloClient.subscribe(CountSubscription(50, 1000)).execute().first() withTimeout(500) { transport.subscriptionCount.first { it == 0 } } delay(1500) - val number = apolloClient.subscribe(CountSubscription(50, 0)).drop(3).first().data?.count + val number = apolloClient.subscribe(CountSubscription(50, 0)).execute().drop(3).first().data?.count assertEquals(3, number) } } @@ -120,6 +123,7 @@ class SampleServerTest { * (which is still probably slower than the server) and make sure we didn't drop any items */ val number = apolloClient.subscribe(CountSubscription(1000, 0)) + .execute() .map { it.data!!.count } .onEach { if (it < 3) { @@ -146,7 +150,7 @@ class SampleServerTest { /** * Collect all items the server sends us */ - apolloClient.subscribe(CountSubscription(50, 0)).toList() + apolloClient.subscribe(CountSubscription(50, 0)).execute().toList() /** * Make sure we're unsubscribed @@ -170,6 +174,7 @@ class SampleServerTest { runBlocking { var caught: Throwable? = null apolloClient.subscribe(OperationErrorSubscription()) + .execute() .catch { caught = it }