Add Apollo-level logging and Kotlin-driven map filter.
Pass the map marker online filter as a GraphQL variable from Kotlin so offline-inclusive behavior is controllable in code, and add a shared Apollo interceptor to log each request payload and response outcome for all operations. Made-with: Cursor
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
query MapMarkers {
|
||||
getMarkers {
|
||||
query MapMarkers($isOnline: Boolean!) {
|
||||
getMarkers(filter: { isOnline: $isOnline }) {
|
||||
_id
|
||||
name
|
||||
latitude
|
||||
|
||||
@@ -7,8 +7,9 @@ import dagger.Provides
|
||||
import dagger.hilt.InstallIn
|
||||
import dagger.hilt.components.SingletonComponent
|
||||
import javax.inject.Singleton
|
||||
import org.db3.airmq.sdk.map.ApolloMapService
|
||||
import org.db3.airmq.sdk.map.MapServiceImpl
|
||||
import org.db3.airmq.sdk.map.MapService
|
||||
import org.db3.airmq.sdk.network.ApolloLoggingInterceptor
|
||||
|
||||
@Module
|
||||
@InstallIn(SingletonComponent::class)
|
||||
@@ -20,6 +21,7 @@ object SDKModule {
|
||||
fun provideApolloClient(): ApolloClient {
|
||||
return ApolloClient.Builder()
|
||||
.serverUrl(API_URL)
|
||||
.addInterceptor(ApolloLoggingInterceptor())
|
||||
.build()
|
||||
}
|
||||
}
|
||||
@@ -29,5 +31,5 @@ object SDKModule {
|
||||
abstract class SDKBindModule {
|
||||
@Binds
|
||||
@Singleton
|
||||
abstract fun bindMapService(impl: ApolloMapService): MapService
|
||||
abstract fun bindMapService(impl: MapServiceImpl): MapService
|
||||
}
|
||||
|
||||
@@ -6,13 +6,15 @@ import javax.inject.Inject
|
||||
import org.db3.airmq.sdk.MapMarkersQuery
|
||||
import org.db3.airmq.sdk.map.domain.MapItem
|
||||
|
||||
class ApolloMapService @Inject constructor(
|
||||
class MapServiceImpl @Inject constructor(
|
||||
private val apolloClient: ApolloClient,
|
||||
private val mapper: ApolloMapItemMapper
|
||||
) : MapService {
|
||||
override suspend fun fetchMapItems(): List<MapItem> {
|
||||
Log.d(TAG, "Executing MapMarkers Apollo query")
|
||||
val response = apolloClient.query(MapMarkersQuery()).execute()
|
||||
val response = apolloClient
|
||||
.query(MapMarkersQuery(isOnline = false))
|
||||
.execute()
|
||||
|
||||
response.errors?.firstOrNull()?.let { gqlError ->
|
||||
Log.e(TAG, "MapMarkers Apollo query failed: ${gqlError.message}")
|
||||
@@ -0,0 +1,39 @@
|
||||
package org.db3.airmq.sdk.network
|
||||
|
||||
import android.util.Log
|
||||
import com.apollographql.apollo.api.ApolloRequest
|
||||
import com.apollographql.apollo.api.ApolloResponse
|
||||
import com.apollographql.apollo.api.Operation
|
||||
import com.apollographql.apollo.interceptor.ApolloInterceptor
|
||||
import com.apollographql.apollo.interceptor.ApolloInterceptorChain
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import kotlinx.coroutines.flow.onEach
|
||||
|
||||
class ApolloLoggingInterceptor : ApolloInterceptor {
|
||||
override fun <D : Operation.Data> intercept(
|
||||
request: ApolloRequest<D>,
|
||||
chain: ApolloInterceptorChain
|
||||
): Flow<ApolloResponse<D>> {
|
||||
val operationName = request.operation.name()
|
||||
val requestData = request.operation.toString()
|
||||
Log.d(TAG, "Apollo request -> $operationName, data=$requestData")
|
||||
return chain.proceed(request).onEach { response ->
|
||||
val errorsCount = response.errors?.size ?: 0
|
||||
if (response.exception != null) {
|
||||
Log.e(
|
||||
TAG,
|
||||
"Apollo response <- $operationName failed: ${response.exception?.message}"
|
||||
)
|
||||
} else {
|
||||
Log.d(
|
||||
TAG,
|
||||
"Apollo response <- $operationName success, errors=$errorsCount, data=${response.data}"
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private companion object {
|
||||
private const val TAG = "ApolloNetwork"
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user