KyawSoeW1n / GraphQL

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

GraphQL

  • simple application to show an exemple of using GraphQL with Kotlin
  • to get the tutorial, read Meduim Article

Dependencies

  • start by implementing dependencies in build.gradle
//apollo  
implementation "com.apollographql.apollo3:apollo-runtime:3.0.0"  
implementation 'com.apollographql.apollo:apollo-android-support:1.0.0'  
// lifecycle  
implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.4.0'  
//okhttp3  
implementation 'com.squareup.okhttp3:okhttp:4.9.3'  
implementation "com.squareup.okhttp3:logging-interceptor:4.8.1"
  • add Apollo plugin to build.gradle (must be added before kotlin plugin)
plugins {  
  id 'com.android.application'  
  id("com.apollographql.apollo3").version("3.0.0")  
    id 'kotlin-android'  
}
  • in the end of build.gradle, specify the package in which the Kotlin files will be generated.
apollo {  
  packageName.set("com.aymen.graphql")  
}

.graphqlconfig

  • start configuring graphql by creating a .graphqlconfig file in app directory:

app\src\main\graphql

  • add the graphql configuration, and put graphql server url in Default GraphQL Endpoint:
{
  "name": "Untitled GraphQL Schema",
  "schemaPath": "schema.graphqls",
  "extensions": {
    "endpoints": {
      "Default GraphQL Endpoint": {
        "url": "http://api.spacex.land/graphql/",
        "headers": {
          "user-agent": "JS GraphQL"
        },
        "introspect": true
      }
    }
  }
}

schema.graphqls

  • use Android Studio terminal to import schema.graphqls using this commande:
gradlew :app:downloadApolloSchema --endpoint="http://api.spacex.land/graphql/" --schema=D:\KotlinStudioProjects\GraphQL\app\src\main\graphql\schema.graphqls

usersList.graphql

  • to import users list from graphql server, we need to create a graphql query
  • use SpaceX GraphQL to create a query and copy it
query UsersList($limit:Int!) {
  users(limit: $limit, order_by: {timestamp: desc}) {
    id
    name
    rocket
    timestamp
    twitter
  }
}

Apollo Client Instance

  • create an apollo client instance
private var BASE_URL = "http://api.spacex.land/graphql/"  
  
private val httpClient : OkHttpClient by lazy {  
  val httpLoggingInterceptor = HttpLoggingInterceptor()  
    httpLoggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY)  
    OkHttpClient.Builder()  
        .callTimeout(60, TimeUnit.SECONDS)  
        .readTimeout(60, TimeUnit.SECONDS)  
        .addInterceptor(httpLoggingInterceptor).build()  
}  
  
fun get(): ApolloClient {  
    return ApolloClient.Builder()  
        .serverUrl(BASE_URL)  
        .okHttpClient(httpClient)  
        .build()  
}

get users list

  • to get users list, use users list graphql query:
val response = client.query(UsersListQuery(10)).execute()
  • to get users list from response :
val users = response.data?.users

About


Languages

Language:Kotlin 100.0%