Chukwuemeka Ndefo
06/07/2023, 4:04 PM// 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
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
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
{
"data": {
"continents": [
{
"code": "AF",
"name": "Africa"
},
{
"code": "AN",
"name": "Antarctica"
},
...
}
mbonnin
06/07/2023, 4:36 PMsampleResponse.errors
to check for graphql errorsChukwuemeka Ndefo
06/07/2023, 4:38 PMChukwuemeka Ndefo
06/07/2023, 4:44 PMChukwuemeka Ndefo
06/07/2023, 4:53 PMresponseText = Log.e("API_Error", sampleResponse.errors.toString()).toString()
It outputed:
E null
mbonnin
06/07/2023, 4:55 PMChukwuemeka Ndefo
06/07/2023, 5:04 PMHttpLoggingInterceptor logging = new HttpLoggingInterceptor(new Logger() {
@Override public void log(String message) {
Timber.tag("OkHttp").d(message);
}
});
Chukwuemeka Ndefo
06/07/2023, 5:16 PMChukwuemeka Ndefo
06/07/2023, 5:32 PMPOST <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?Chukwuemeka Ndefo
06/07/2023, 6:10 PMPOST <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.Chrimaeon
06/07/2023, 6:26 PMINTERNET
permission in your manifest?Chrimaeon
06/07/2023, 6:27 PMChukwuemeka Ndefo
06/07/2023, 6:38 PM<?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
<?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.Chrimaeon
06/07/2023, 6:41 PMChukwuemeka Ndefo
06/07/2023, 6:42 PMChrimaeon
06/07/2023, 6:43 PMChukwuemeka Ndefo
06/07/2023, 6:44 PMChrimaeon
06/07/2023, 6:44 PMChukwuemeka Ndefo
06/07/2023, 6:45 PMChrimaeon
06/07/2023, 6:46 PM