Code Monkey home page Code Monkey logo

graphql's Introduction

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

graphql's People

Contributors

aymensoft avatar kyawsoewin321 avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.