I ran into the problem of Apollo Client returning ...
# android
c
I ran into the problem of Apollo Client returning null from the graphql api which was tested using graphql plugin on android studio (Giraffe edition) and works fine. I suspect it has to do with way the request is being sent. Android emulator and my physical android device (inlcuding signed apk) were used for running the app. Please help me with what is actually wrong. The below are my code: MaintActivity.kt
Copy code
// Create a client
fun provideApolloClientPost(url: String): ApolloClient {
    return ApolloClient.Builder()
        .serverUrl(url)
        .httpMethod(<http://HttpMethod.Post|HttpMethod.Post>)
        .okHttpClient(createOkHttpWithValidToken())
        .build()
}

val sampleClient =  provideApolloClientPost("<https://countries.trevorblades.com/graphql>")

suspend fun getSample() {
    try {
        val sampleResponse: ApolloResponse<ContinentsQuery.Data> = sampleClient.query(ContinentsQuery(false, true)).execute()
        responseText = sampleResponse.data.toString()
    }catch (e: Exception){
        responseText = "ERROR! $e"
        e.printStackTrace()
    }
}
responseText returns null. I appreciate any help on this. Thank you. *schema.gr*aphqls
Copy code
type Country {
    name: String!
    native: String!
    capital: String!
    phone: String!
    emoji: String!
    currency: String!
    languages: Languages!
    continent: Languages
}

type Continent {
        countries: [Country]
}

type Languages {
    code: String!
    name: String!
}

type Query {
    country(
        code: String!
    ): Country

    continent(
        code: String!
    ): Continent

    continents(
        if: Boolean
    ): [Languages]

    countries(
        if: Boolean
    ): [Country]
}
ContinentsQuery.graphql
Copy code
query ContinentsQuery (
    $skipContinents: Boolean!,
    $skipCountries: Boolean!
    ) {
        continents @skip(if: $skipContinents) {
            code
            name
        }
        countries @skip(if: $skipCountries) {
            name
            native
            capital
            emoji
            currency
            languages {
                code
                name
            }
            continent{
                code
                name
            }
        }
}
Result in Postman
Copy code
{
    "data": {
        "continents": [
            {
                "code": "AF",
                "name": "Africa"
            },
            {
                "code": "AN",
                "name": "Antarctica"
            },
...
}
not kotlin but kotlin colored 3
🧵 5
m
Looks like you may have graphql errors. You can dump
sampleResponse.errors
to check for graphql errors
c
Actually, I ran the graphql in both postman and android studio graphql plugin. I worked fine. Let me add the schema to the question.
I just updated the question with the schema
I then did this:
Copy code
responseText = Log.e("API_Error", sampleResponse.errors.toString()).toString()
It outputed:
Copy code
E  null
m
Something else you can try is log the network traffic with a HttpLoggingInterceptor
c
This class HttpLoggingInterceptor wasn't included, even though I use okHttpClient. Hence, can't implement:
Copy code
HttpLoggingInterceptor logging = new HttpLoggingInterceptor(new Logger() {
  @Override public void log(String message) {
    Timber.tag("OkHttp").d(message);
  }
});
Adding the implementation of HttpLoggingInterceptor...
The log showed:
Copy code
POST <https://countries.trevorblades.com/graphql> (357-byte body)
2023-06-07 18:29:11.199 23422-23520 okhttp.OkHttpClient     com.myapp  I  <-- HTTP FAILED: java.net.UnknownHostException: Unable to resolve host "<http://countries.trevorblades.com|countries.trevorblades.com>": No address associated with hostname
2023-06-07 18:29:11.226 23422-23422 API_Error               com.myapp  E  null
This same endpoint works well on postman. How come?
Also, tested it with localhost api, and got this:
Copy code
POST <http://localhost/myapp/graphql> (137-byte body)
Failed to connect to localhost/127.0.0.1:80
The strange behaviour of combining both localhost & 127.0.0.1, something I can't decipher. The localhost api runs well on postman too.
c
Do you have the
INTERNET
permission in your manifest?
Also, this is a workspace for the Kotlin Programming Language. Your general android question is better asked somewhere else - there are links in the channel description.
c
I do have internet permission. As you can see, my code are all in kotlin. Just that I develop android app in kotlin. One more thing: look at my AndroidManifest.xml and network_security_config.xml I think they have something to do with this error AndroidManifest.xml
Copy code
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="<http://schemas.android.com/apk/res/android>"
    xmlns:tools="<http://schemas.android.com/tools>">

    <uses-permission android:name="android.permission.INTERNET" />

    <application
        android:allowBackup="true"
        android:dataExtractionRules="@xml/data_extraction_rules"
        android:fullBackupContent="@xml/backup_rules"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.MyApp"
        tools:targetApi="31"
        android:largeHeap="true"
        android:networkSecurityConfig="@xml/network_security_config">
network_security_config.xml
Copy code
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
    <domain-config cleartextTrafficPermitted="true">
        <domain includeSubdomains="true">localhost</domain>
        <domain includeSubdomains="true">10.0.2.2</domain>
        <domain includeSubdomains="true"><http://countries.trevorblades.com|countries.trevorblades.com></domain>
    </domain-config>
</network-security-config>
The above config file was added because I was using android studio emulator, and I read that the emulator sends request to 10.0.2.2 as the localhost, and not necessarily suing localhost.
c
Sure, your code is Kotlin, but your problem is not Kotlin related. you have an issue with a library and the andorid framework.
c
I see. Ok. please, point me in the right direction. I am kinda stuck on this problem for now. I will appreciate Sir.
c
no clue whats your problem, my idea was the permission.
c
I mean the right Channel I need to go to
c
This Channel reads, "android" Ok. Thank you so much for your help. I learned a lot, especially the use of okhttp3:logging-interceptor for debugging. God bless.
c
👍
120 Views