Skip to content

Check FetchPolicy in Apollo Interceptor #6859

@ersen-lw

Description

@ersen-lw

Use case

I want to write a interceptor to log when a cache miss occurs. I only want to log it when the FetchPolicy is CacheOnly. If a request is CacheOnly and the outcome is a miss implies something is wrong on my end so I want to log those cases whilst ignoring  other policies like CacheFirst where it is expected to be missed.

class MyApolloCacheMissLoggingInterceptor: ApolloInterceptor {
    override fun <D : Operation.Data> intercept(request: ApolloRequest<D>, chain: ApolloInterceptorChain): Flow<ApolloResponse<D>> {
        return chain.proceed(request).onEach {
            //TODO add check if fetch policy is CacheOnly
            if (it.exception is CacheMissException) {
                Timber.e(it.exception, "Apollo cache miss on ${request.operation.name()}")
            }
        }
    }
}

Describe the solution you'd like

I can access the executionContext in the request but FetchPolicyContext is marked as internal so I can't access it. Could this be made public ?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions