Last active
December 18, 2025 11:13
-
-
Save diego-gomez-olvera/1c3f420edc8218af01ea872f202effc9 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * Result of an operation using Apollo. It matches the possible states of a GraphQL response: | |
| * - https://www.apollographql.com/docs/kotlin/essentials/errors#truth-table | |
| * - https://github.com/HedvigInsurance/android/blob/0eb8c4a862644be4dd5beb717c8142666e6ff846/app/apollo/apollo-core/src/commonMain/kotlin/com/hedvig/android/apollo/ApolloCallExt.kt#L106-L127 | |
| */ | |
| sealed interface ApolloResult<out T> { | |
| /** | |
| * Response with a valid result. | |
| */ | |
| sealed interface Valid<out T : Any> : ApolloResult<T> { | |
| val result: T | |
| } | |
| /** | |
| * Response without a valid result. | |
| */ | |
| sealed interface Invalid : ApolloResult<Nothing> | |
| /** | |
| * Response with errors. | |
| */ | |
| sealed interface Flawed<out T> : ApolloResult<T> { | |
| val errors: List<Error> | |
| } | |
| /** | |
| * Complete data was received. | |
| */ | |
| @JvmInline | |
| value class Full<T : Any>(override val result: T) : Valid<T> | |
| /** | |
| * Partial data was received alongside field error(s). | |
| */ | |
| data class Partial<T : Any>( | |
| override val result: T, | |
| override val errors: List<Error>, | |
| ) : Valid<T>, Flawed<T> | |
| /** | |
| * A GraphQL request error happened or a Graph field error bubbled up. | |
| */ | |
| @JvmInline | |
| value class Empty(override val errors: List<Error>) : Invalid, Flawed<Nothing> | |
| /** | |
| * A fetch error happened. | |
| */ | |
| @JvmInline | |
| value class FetchError(val exception: ApolloException) : Invalid | |
| } | |
| /** | |
| * Transform an `ApolloResponse` into a `ApolloResult`. | |
| * | |
| * @return `ApolloResult` with the result of the operation. | |
| */ | |
| internal fun <D : Operation.Data> ApolloResponse<D>.toResult(): ApolloResult<D> { | |
| val data = data | |
| val errors = errors | |
| val exception = exception | |
| if (data != null) { | |
| return if (errors.isNullOrEmpty()) { | |
| ApolloResult.Full(data) | |
| } else { | |
| ApolloResult.Partial(data, errors) | |
| } | |
| } | |
| if (exception != null) { | |
| return ApolloResult.FetchError(exception) | |
| } | |
| if (!errors.isNullOrEmpty()) { | |
| return ApolloResult.Empty(errors) | |
| } | |
| return ApolloResult.FetchError( | |
| DefaultApolloException("Server returned no data, no errors, and no exception") | |
| ) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment