Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhancing Library Usability with Communication Error Handling in Queries and Mutations #12

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

psyrenpark
Copy link

Hello~
I've been enjoying using this library. During its use, I've made some modifications.
I would appreciate it if you could review them.

Thank you for creating such a great library. If circumstances allow while I continue to study, I plan to make further modifications.

P.S.:
I am a React Native developer currently studying Compose and SwiftUI in the React paradigm.

Below are the reference materials.

// query

@Composable
fun TestQuery() {
    Column() {

        val queryResult by useQuery {
            delay(200)
            val response =  Req2.client.get("https://www.test-error.com")
            "Fetched: ${response?.status}"
        }
        when(queryResult){
            is QueryState.Loading -> {
                Text("loading")
            }
            is QueryState.Error -> {
                val th = (queryResult as QueryState.Error).th
                val statusCode : Int = when (th){
                    is RedirectResponseException -> { //Http Code: 3xx
                        (th.response.status.value)
                    }
                    is ClientRequestException -> { //Http Code: 4xx
                        (th.response.status.value)
                    }
                    is ServerResponseException -> { //Http Code: 5xx
                        (th.response.status.value)
                    }
                    is UnresolvedAddressException -> { // Network Error - Internet Error
                        1000
                    }
                    else -> 9999
                }
                Text("error : ${statusCode}" )
            }
            is QueryState.Content -> {
                val data = (queryResult as QueryState.Content<String>).data;
                Text("data :  $data")
            }
        }

    }
}
// mutation
@Composable
fun TestMutation() {
    var buttonText by remember { mutableStateOf("idle") }

    val testMutation = useMutation { (username, password) ->
        delay(500)
        val url = "https://www.test-error.com"
        val response =  Req2.client.get(url)
        "secret_token:$username/$password Fetched ${response?.status} "
    }
    
    Column() {
        Button(onClick = {
            testMutation.reset()
        }) {
            Text("reset")
        }
        Button(onClick = {
            println("click")
            testMutation.mutate("pavi2410", "secretpw123") {
                buttonText = it
            }

        }) {
            Text(text = buttonText)
        }

        when(testMutation.state){
            is MutationState.Idle -> {
                Text("idle")
            }
            is MutationState.Loading -> {
                Text("loading")
            }
            is MutationState.Error -> {
                val th = (testMutation.state as MutationState.Error).th
                val statusCode : Int = when (th){

                    is RedirectResponseException -> { //Http Code: 3xx
                        (th.response.status.value)
                    }
                    is ClientRequestException -> { //Http Code: 4xx
                        (th.response.status.value)
                    }
                    is ServerResponseException -> { //Http Code: 5xx
                        (th.response.status.value)
                    }
                    is UnresolvedAddressException -> { // Network Error - Internet Error
                        1000
                    }
                    else -> 9999
                }
                Text("error : $statusCode" )
            }
            is MutationState.Success -> {
                Text("Success : $buttonText")
            }
        }
    }
}

//swift ui
https://github.com/unixzii/swiftui-for-react-devs

// compose
https://tigeroakes.com/posts/react-to-compose-dictionary/

Hello,
I've been enjoying using this library. During its use, I've made some modifications.
I would appreciate it if you could review them.

P.S:
I am a React Native developer currently studying Compose and SwiftUI in the React paradigm.
@pavi2410
Copy link
Owner

Wow! I thought nobody uses this library. Thanks for using this. It's bedtime right now, I will read this issue in the morning.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants