Skip to content

Commit

Permalink
Add a temporary "compatibility layer" keeping the previous With-ers A…
Browse files Browse the repository at this point in the history
…PI available, to ease the transition
  • Loading branch information
BoD committed Oct 13, 2021
1 parent 59218b1 commit c2424fd
Show file tree
Hide file tree
Showing 8 changed files with 249 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
package com.apollographql.apollo3.api

import com.apollographql.apollo3.api.http.HttpHeader
import com.apollographql.apollo3.api.http.HttpMethod
import com.apollographql.apollo3.api.http.httpHeader
import com.apollographql.apollo3.api.http.httpHeaders
import com.apollographql.apollo3.api.http.httpMethod
import com.apollographql.apollo3.api.http.sendApqExtensions
import com.apollographql.apollo3.api.http.sendDocument
import com.benasher44.uuid.Uuid
import com.benasher44.uuid.uuid4

Expand Down Expand Up @@ -46,3 +53,25 @@ class ApolloRequest<D : Operation.Data> private constructor(
}
}
}

// BEGIN With-ers to Builders compatibility layer

@Deprecated("Please use ApolloRequest.Builder methods instead. This will be removed in v3.0.0.")
fun <D : Operation.Data> ApolloRequest<D>.withHttpMethod(httpMethod: HttpMethod) = newBuilder().httpMethod(httpMethod).build()

@Deprecated("Please use ApolloRequest.Builder methods instead. This will be removed in v3.0.0.")
fun <D : Operation.Data> ApolloRequest<D>.withHttpHeaders(httpHeaders: List<HttpHeader>) = newBuilder().httpHeaders(httpHeaders).build()

@Deprecated("Please use ApolloRequest.Builder methods instead. This will be removed in v3.0.0.")
fun <D : Operation.Data> ApolloRequest<D>.withHttpHeader(httpHeader: HttpHeader) = newBuilder().httpHeader(httpHeader).build()

@Deprecated("Please use ApolloRequest.Builder methods instead. This will be removed in v3.0.0.")
fun <D : Operation.Data> ApolloRequest<D>.withHttpHeader(name: String, value: String) = newBuilder().httpHeader(name, value).build()

@Deprecated("Please use ApolloRequest.Builder methods instead. This will be removed in v3.0.0.")
fun <D : Operation.Data> ApolloRequest<D>.withSendApqExtensions(sendApqExtensions: Boolean) = newBuilder().sendApqExtensions(sendApqExtensions).build()

@Deprecated("Please use ApolloRequest.Builder methods instead. This will be removed in v3.0.0.")
fun <D : Operation.Data> ApolloRequest<D>.withSendDocument(sendDocument: Boolean) = newBuilder().sendDocument(sendDocument).build()

// END With-ers to Builders compatibility layer
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.apollographql.apollo3.cache.http

import com.apollographql.apollo3.ApolloClient
import com.apollographql.apollo3.api.ApolloRequest
import com.apollographql.apollo3.api.ApolloResponse
import com.apollographql.apollo3.api.HasMutableExecutionContext
import com.apollographql.apollo3.api.Operation
Expand Down Expand Up @@ -82,4 +83,39 @@ fun <T> HasMutableExecutionContext<T>.httpExpireAfterRead(expireAfterRead: Boole

fun <T> HasMutableExecutionContext<T>.httpDoNotStore(doNotStore: Boolean) where T : HasMutableExecutionContext<T> = httpHeader(
CachingHttpEngine.CACHE_DO_NOT_STORE, doNotStore.toString()
)
)


// BEGIN With-ers to Builders compatibility layer

@Deprecated("Please use ApolloClient.Builder methods instead. This will be removed in v3.0.0.")
fun ApolloClient.withHttpCache(
directory: File,
maxSize: Long,
): ApolloClient = newBuilder().httpCache(directory, maxSize).build()

@Deprecated("Please use ApolloClient.Builder methods instead. This will be removed in v3.0.0.")
fun ApolloClient.withHttpFetchPolicy(httpFetchPolicy: HttpFetchPolicy) = newBuilder().httpFetchPolicy(httpFetchPolicy).build()

@Deprecated("Please use ApolloRequest.Builder methods instead. This will be removed in v3.0.0.")
fun <D : Operation.Data> ApolloRequest<D>.withHttpFetchPolicy(httpFetchPolicy: HttpFetchPolicy) = newBuilder().httpFetchPolicy(httpFetchPolicy).build()

@Deprecated("Please use ApolloClient.Builder methods instead. This will be removed in v3.0.0.")
fun ApolloClient.withHttpExpireTimeout(millis: Long) = newBuilder().httpExpireTimeout(millis).build()

@Deprecated("Please use ApolloRequest.Builder methods instead. This will be removed in v3.0.0.")
fun <D : Operation.Data> ApolloRequest<D>.withHttpExpireTimeout(millis: Long) = newBuilder().httpExpireTimeout(millis).build()

@Deprecated("Please use ApolloClient.Builder methods instead. This will be removed in v3.0.0.")
fun ApolloClient.withHttpExpireAfterRead(expireAfterRead: Boolean) = newBuilder().httpExpireAfterRead(expireAfterRead).build()

@Deprecated("Please use ApolloRequest.Builder methods instead. This will be removed in v3.0.0.")
fun <D : Operation.Data> ApolloRequest<D>.withHttpExpireAfterRead(expireAfterRead: Boolean) = newBuilder().httpExpireAfterRead(expireAfterRead).build()

@Deprecated("Please use ApolloClient.Builder methods instead. This will be removed in v3.0.0.")
fun ApolloClient.withHttpDoNotStore(doNotStore: Boolean) = newBuilder().httpDoNotStore(doNotStore).build()

@Deprecated("Please use ApolloRequest.Builder methods instead. This will be removed in v3.0.0.")
fun <D : Operation.Data> ApolloRequest<D>.withHttpDoNotStore(doNotStore: Boolean) = newBuilder().httpDoNotStore(doNotStore).build()

// END With-ers to Builders compatibility layer
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,15 @@ class ApolloIdlingResource(
}
}

fun ApolloClient.Builder.withIdlingResource(idlingResource: ApolloIdlingResource): ApolloClient.Builder {
fun ApolloClient.Builder.idlingResource(idlingResource: ApolloIdlingResource): ApolloClient.Builder {
return addFlowDecorator {
it.onStart {
idlingResource.operationStart()
}.onCompletion {
idlingResource.operationEnd()
}
}
}
}

@Deprecated("Please use ApolloClient.Builder methods instead. This will be removed in v3.0.0.")
fun ApolloClient.withIdlingResource(idlingResource: ApolloIdlingResource) = newBuilder().idlingResource(idlingResource).build()
Original file line number Diff line number Diff line change
Expand Up @@ -275,3 +275,67 @@ internal class WatchContext(val value: Boolean) : ExecutionContext.Element {

companion object Key : ExecutionContext.Key<WatchContext>
}


// BEGIN With-ers to Builders compatibility layer

@Deprecated("Please use ApolloClient.Builder methods instead. This will be removed in v3.0.0.")
fun ApolloClient.withNormalizedCache(
normalizedCacheFactory: NormalizedCacheFactory,
objectIdGenerator: ObjectIdGenerator = TypePolicyObjectIdGenerator,
cacheResolver: CacheResolver = FieldPolicyCacheResolver,
writeToCacheAsynchronously: Boolean = false,
) = newBuilder().normalizedCache(
normalizedCacheFactory = normalizedCacheFactory,
objectIdGenerator = objectIdGenerator,
cacheResolver = cacheResolver,
writeToCacheAsynchronously = writeToCacheAsynchronously,
)
.build()

@Deprecated("Please use ApolloClient.Builder methods instead. This will be removed in v3.0.0.")
fun ApolloClient.withStore(
store: ApolloStore,
writeToCacheAsynchronously: Boolean = false,
) = newBuilder().store(store, writeToCacheAsynchronously).build()

@Deprecated("Please use ApolloRequest.Builder methods instead. This will be removed in v3.0.0.")
fun <D : Query.Data> ApolloRequest<D>.withFetchPolicy(fetchPolicy: FetchPolicy) = newBuilder().fetchPolicy(fetchPolicy).build()

@Deprecated("Please use ApolloClient.Builder methods instead. This will be removed in v3.0.0.")
fun ApolloClient.withFetchPolicy(fetchPolicy: FetchPolicy) = newBuilder().fetchPolicy(fetchPolicy).build()

@Deprecated("Please use ApolloRequest.Builder methods instead. This will be removed in v3.0.0.")
fun <D : Query.Data> ApolloRequest<D>.withRefetchPolicy(refetchPolicy: FetchPolicy) = newBuilder().refetchPolicy(refetchPolicy).build()

@Deprecated("Please use ApolloClient.Builder methods instead. This will be removed in v3.0.0.")
fun ApolloClient.withRefetchPolicy(refetchPolicy: FetchPolicy) = newBuilder().refetchPolicy(refetchPolicy).build()

@Deprecated("Please use ApolloClient.Builder methods instead. This will be removed in v3.0.0.")
fun ApolloClient.withDoNotStore(doNotStore: Boolean) = newBuilder().doNotStore(doNotStore).build()

@Deprecated("Please use ApolloRequest.Builder methods instead. This will be removed in v3.0.0.")
fun <D : Operation.Data> ApolloRequest<D>.withDoNotStore(doNotStore: Boolean) = newBuilder().doNotStore(doNotStore).build()

@Deprecated("Please use ApolloClient.Builder methods instead. This will be removed in v3.0.0.")
fun ApolloClient.withStorePartialResponses(storePartialResponses: Boolean) = newBuilder().storePartialResponses(storePartialResponses).build()

@Deprecated("Please use ApolloRequest.Builder methods instead. This will be removed in v3.0.0.")
fun <D : Operation.Data> ApolloRequest<D>.withStorePartialResponses(storePartialResponses: Boolean) = newBuilder().storePartialResponses(storePartialResponses).build()

@Deprecated("Please use ApolloClient.Builder methods instead. This will be removed in v3.0.0.")
fun ApolloClient.withCacheHeaders(cacheHeaders: CacheHeaders) = newBuilder().cacheHeaders(cacheHeaders).build()

@Deprecated("Please use ApolloRequest.Builder methods instead. This will be removed in v3.0.0.")
fun <D : Operation.Data> ApolloRequest<D>.withCacheHeaders(cacheHeaders: CacheHeaders) = newBuilder().cacheHeaders(cacheHeaders).build()

@Deprecated("Please use ApolloClient.Builder methods instead. This will be removed in v3.0.0.")
fun ApolloClient.withWriteToCacheAsynchronously(writeToCacheAsynchronously: Boolean) = newBuilder().writeToCacheAsynchronously(writeToCacheAsynchronously).build()

@Deprecated("Please use ApolloRequest.Builder methods instead. This will be removed in v3.0.0.")
fun <D : Operation.Data> ApolloRequest<D>.withWriteToCacheAsynchronously(writeToCacheAsynchronously: Boolean) = newBuilder().writeToCacheAsynchronously(writeToCacheAsynchronously).build()

@Deprecated("Please use ApolloRequest.Builder methods instead. This will be removed in v3.0.0.")
fun <D : Mutation.Data> ApolloRequest<D>.withOptimisticUpdates(data: D) = newBuilder().optimisticUpdates(data).build()

// END With-ers to Builders compatibility layer
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,13 @@ 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 com.apollographql.apollo3.api.http.HttpHeader
import com.apollographql.apollo3.api.http.HttpMethod
import com.apollographql.apollo3.api.http.httpHeader
import com.apollographql.apollo3.api.http.httpHeaders
import com.apollographql.apollo3.api.http.httpMethod
import com.apollographql.apollo3.api.http.sendApqExtensions
import com.apollographql.apollo3.api.http.sendDocument
import com.apollographql.apollo3.interceptor.ApolloInterceptor
import com.apollographql.apollo3.interceptor.AutoPersistedQueryInterceptor
import com.apollographql.apollo3.interceptor.DefaultInterceptorChain
Expand All @@ -30,25 +35,37 @@ 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<ApolloResponse<*>>) -> Flow<ApolloResponse<*>>

/**
* The main entry point for the Apollo runtime. An [ApolloClient] is responsible for executing queries, mutations and subscriptions
*/
class ApolloClient private constructor(
class ApolloClient @JvmOverloads @Deprecated("Please use ApolloClient.Builder instead. This will be removed in v3.0.0.") constructor(
private val networkTransport: NetworkTransport,
private val customScalarAdapters: CustomScalarAdapters,
private val subscriptionNetworkTransport: NetworkTransport,
val interceptors: List<ApolloInterceptor>,
override val executionContext: ExecutionContext,
private val requestedDispatcher: CoroutineDispatcher?,
private val flowDecorators: List<FlowDecorator>,
private val customScalarAdapters: CustomScalarAdapters = CustomScalarAdapters.Empty,
private val subscriptionNetworkTransport: NetworkTransport = networkTransport,
val interceptors: List<ApolloInterceptor> = emptyList(),
override val executionContext: ExecutionContext = ExecutionContext.Empty,
private val requestedDispatcher: CoroutineDispatcher? = null,
private val flowDecorators: List<FlowDecorator> = emptyList(),
) : HasExecutionContext {

private val dispatcher = defaultDispatcher(requestedDispatcher)
private val clientScope = ClientScope(CoroutineScope(dispatcher))

/**
* A short-hand constructor
*/
@Deprecated("Please use ApolloClient.Builder instead. This will be removed in v3.0.0.")
constructor(
serverUrl: String,
) : this(
networkTransport = HttpNetworkTransport(serverUrl = serverUrl),
subscriptionNetworkTransport = WebSocketNetworkTransport(serverUrl = serverUrl),
)

/**
* Executes the given query and returns a response or throws on transport errors
* use [query] ([ApolloRequest]) to customize the request
Expand Down Expand Up @@ -109,6 +126,36 @@ class ApolloClient private constructor(
subscriptionNetworkTransport.dispose()
}

@Deprecated("Please use ApolloClient.Builder methods instead. This will be removed in v3.0.0.")
fun <T> withCustomScalarAdapter(customScalarType: CustomScalarType, customScalarAdapter: Adapter<T>): ApolloClient {
return copy(
customScalarAdapters = CustomScalarAdapters(
customScalarAdapters.customScalarAdapters + mapOf(customScalarType.name to customScalarAdapter)
)
)
}

@Deprecated("Please use ApolloClient.Builder methods instead. This will be removed in v3.0.0.")
fun withInterceptor(interceptor: ApolloInterceptor): ApolloClient {
return copy(
interceptors = interceptors + interceptor
)
}

@Deprecated("Please use ApolloClient.Builder methods instead. This will be removed in v3.0.0.")
fun withFlowDecorator(flowDecorator: FlowDecorator): ApolloClient {
return copy(
flowDecorators = flowDecorators + flowDecorator
)
}

@Deprecated("Please use ApolloClient.Builder methods instead. This will be removed in v3.0.0.")
fun withExecutionContext(executionContext: ExecutionContext): ApolloClient {
return copy(
executionContext = this.executionContext + executionContext
)
}

private fun copy(
networkTransport: NetworkTransport = this.networkTransport,
subscriptionNetworkTransport: NetworkTransport = this.subscriptionNetworkTransport,
Expand Down Expand Up @@ -273,4 +320,35 @@ fun ApolloClient.Builder.autoPersistedQueries(
it
}
}
}
}

// BEGIN With-ers to Builders compatibility layer

@Deprecated("Please use ApolloClient.Builder methods instead. This will be removed in v3.0.0.")
fun ApolloClient.withAutoPersistedQueries(
httpMethodForHashedQueries: HttpMethod = HttpMethod.Get,
httpMethodForDocumentQueries: HttpMethod = HttpMethod.Post,
hashByDefault: Boolean = true,
): ApolloClient {
return this.newBuilder().autoPersistedQueries(httpMethodForHashedQueries, httpMethodForDocumentQueries, hashByDefault).build()
}

@Deprecated("Please use ApolloClient.Builder methods instead. This will be removed in v3.0.0.")
fun ApolloClient.withHttpMethod(httpMethod: HttpMethod) = newBuilder().httpMethod(httpMethod).build()

@Deprecated("Please use ApolloClient.Builder methods instead. This will be removed in v3.0.0.")
fun ApolloClient.withHttpHeaders(httpHeaders: List<HttpHeader>) = newBuilder().httpHeaders(httpHeaders).build()

@Deprecated("Please use ApolloClient.Builder methods instead. This will be removed in v3.0.0.")
fun ApolloClient.withHttpHeader(httpHeader: HttpHeader) = newBuilder().httpHeader(httpHeader).build()

@Deprecated("Please use ApolloClient.Builder methods instead. This will be removed in v3.0.0.")
fun ApolloClient.withHttpHeader(name: String, value: String) = newBuilder().httpHeader(name, value).build()

@Deprecated("Please use ApolloClient.Builder methods instead. This will be removed in v3.0.0.")
fun ApolloClient.withSendApqExtensions(sendApqExtensions: Boolean) = newBuilder().sendApqExtensions(sendApqExtensions).build()

@Deprecated("Please use ApolloClient.Builder methods instead. This will be removed in v3.0.0.")
fun ApolloClient.withSendDocument(sendDocument: Boolean) = newBuilder().sendDocument(sendDocument).build()

// END With-ers to Builders compatibility layer
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
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
Expand Down Expand Up @@ -32,3 +33,14 @@ fun <D : Operation.Data> ApolloResponse<D>.withAutoPersistedQueryInfo(hit: Boole
* A shorthand method that sets sendDocument and sendApqExtensions at the same time.
*/
fun <T> HasMutableExecutionContext<T>.hashedQuery(hashed: Boolean) where T : HasMutableExecutionContext<T> = sendDocument(!hashed).sendApqExtensions(hashed)


// BEGIN With-ers to Builders compatibility layer

@Deprecated("Please use ApolloClient.Builder methods instead. This will be removed in v3.0.0.")
fun ApolloClient.withHashedQuery(hashed: Boolean) = newBuilder().hashedQuery(hashed).build()

@Deprecated("Please use ApolloRequest.Builder methods instead. This will be removed in v3.0.0.")
fun <D : Operation.Data> ApolloRequest<D>.withHashedQuery(hashed: Boolean) = newBuilder().hashedQuery(hashed).build()

// END With-ers to Builders compatibility layer
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package com.apollographql.apollo3.network.http

import com.apollographql.apollo3.ApolloClient
import com.apollographql.apollo3.api.AnyAdapter
import com.apollographql.apollo3.api.ApolloRequest
import com.apollographql.apollo3.api.HasMutableExecutionContext
import com.apollographql.apollo3.api.Operation
import com.apollographql.apollo3.api.http.HttpBody
import com.apollographql.apollo3.api.http.HttpMethod
import com.apollographql.apollo3.api.http.HttpRequest
Expand Down Expand Up @@ -207,3 +210,14 @@ class BatchingHttpEngine(
fun <T> HasMutableExecutionContext<T>.canBeBatched(canBeBatched: Boolean) where T : HasMutableExecutionContext<T> = httpHeader(
CAN_BE_BATCHED, canBeBatched.toString()
)

// BEGIN With-ers to Builders compatibility layer

@Deprecated("Please use ApolloClient.Builder methods instead. This will be removed in v3.0.0.")
fun ApolloClient.withCanBeBatched(canBeBatched: Boolean) = newBuilder().canBeBatched(canBeBatched).build()

@Deprecated("Please use ApolloRequest.Builder methods instead. This will be removed in v3.0.0.")
fun <D : Operation.Data> ApolloRequest<D>.withCanBeBatched(canBeBatched: Boolean) = newBuilder().canBeBatched(canBeBatched).build()

// END With-ers to Builders compatibility layer

4 changes: 2 additions & 2 deletions tests/idling-resource/src/test/java/IdlingResourceTest.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import com.apollographql.apollo3.ApolloClient
import com.apollographql.apollo3.android.ApolloIdlingResource
import com.apollographql.apollo3.android.withIdlingResource
import com.apollographql.apollo3.android.idlingResource
import com.apollographql.apollo3.mockserver.MockResponse
import com.apollographql.apollo3.mockserver.MockServer
import idling.resource.IdlingResourceQuery
Expand All @@ -24,7 +24,7 @@ class IdlingResourceTest {
val idlingResource = ApolloIdlingResource("test")
val apolloClient = ApolloClient.Builder()
.serverUrl(mockServer.url())
.withIdlingResource(idlingResource)
.idlingResource(idlingResource)
.build()

launch(start = CoroutineStart.UNDISPATCHED) {
Expand Down

0 comments on commit c2424fd

Please sign in to comment.