DEV Community

Adam Miller
Adam Miller

Posted on

Planning to Integrate Apollo GraphQL with Jetpack Compose? Here's a Complete Guide

Are you planning to incorporate Apollo GraphQL into a Jetpack Compose project? This guide will take you through the process step-by-step. For a complete implementation using Clean Architecture, be sure to visit my GitHub repository.

& It will be looks like this:

Image description

Project Setup

Modify app/build.gradle.kts: To begin, include the Apollo plugin:

{
    // ...
    alias(libs.plugins.apollo3Graph)
}
Enter fullscreen mode Exit fullscreen mode

Now, add Apollo runtime to your dependencies:

dependencies {
    // Existing dependencies
    implementation(libs.apollo.runtime)
}
Enter fullscreen mode Exit fullscreen mode

Edit libs.versions.toml: Define Apollo's version and libraries:

[versions]
apollo3 = "3.8.5"

[libraries]
apollo-runtime = { group = "com.apollographql.apollo3", name = "apollo-runtime" }
apollo-api = { group = "com.apollographql.apollo3", name = "apollo-api" }

[plugins]
apollo3Graph = { id = "com.apollographql.apollo3", version.ref = "apollo3" }
Enter fullscreen mode Exit fullscreen mode

Set Package for Generated Code: Specify the package for generated Kotlin files by adding the following to your app/build.gradle.kts:

{
    // Android block
    apollo {
        service("service") {
            packageName.set("com.example.appname")
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

GraphQL Schema Integration
Download the GraphQL Schema: Obtain the schema from your server using the Apollo CLI. Run this command in Android Studio's terminal:

./gradlew :app:downloadApolloSchema --endpoint='http://localhost:4000/graphql' --schema=app/src/main/graphql/schema.graphqls

Enter fullscreen mode Exit fullscreen mode

The schema will be saved in app/src/main/graphql/.

First GraphQL Query

  • Add Your First Query: Create a new GraphQL file in src/main/graphql/. Here’s an example query to fetch posts:
query Posts($limit: Int!, $cursor: String) {
    posts(limit: $limit, cursor: $cursor) {
        hasMore
        posts {
            id
            createdAt
            updatedAt
            title
            text
        }
    }
}
Enter fullscreen mode Exit fullscreen mode
  • Generate the Query Model: After building the project, check the generated model file at app/build/generated/source/apollo/service/com/example/appname/PostsQuery.kt.

Executing the Query

  • Create an Apollo Client: In Apollo.kt, create an instance of ApolloClient with your server's URL:
val apolloClient = ApolloClient.Builder()
    .serverUrl("http://10.0.2.2:4000/graphql")
    .build()
Enter fullscreen mode Exit fullscreen mode
  • Run the Query: Now, you can use the ApolloClient to execute the PostsQuery:
val response = apolloClient.query(PostsQuery(limit = limit, cursor = Optional.present(cursor))).execute()
val posts = response.data?.posts?.posts?.filterNotNull() ?: emptyList()
Enter fullscreen mode Exit fullscreen mode

Conclusion
You've successfully integrated Apollo GraphQL with Jetpack Compose! If you're looking for more advanced features like implementing GraphQL with Clean Architecture, take a look at my GitHub repository. Keep coding, and enjoy the process!

Top comments (0)