Join us from October 8-10 in New York City to learn the latest tips, trends, and news about GraphQL Federation and API platform engineering.Join us for GraphQL Summit 2024 in NYC
Docs
Start for Free
You're viewing documentation for a previous version of this software. Switch to the latest stable version.

Get started with Java


Add the Gradle plugin

In your app Gradle file, apply the com.apollographql.apollo plugin:

Using the plugins DSL:

plugins {
// ...
id("com.apollographql.apollo").version("x.y.z")
}
plugins {
// ...
id("com.apollographql.apollo").version("x.y.z")
}

Or using the legacy syntax:

buildscript {
// ...
classpath("com.apollographql.apollo:apollo-gradle-plugin:x.y.z")
}
apply plugin: "com.apollographql.apollo"
buildscript {
// ...
classpath("com.apollographql.apollo:apollo-gradle-plugin:x.y.z")
}
apply(plugin = "com.apollographql.apollo")

The plugin is hosted on the Gradle plugin portal, Jcenter and Maven Central.

Add the runtime dependencies

dependencies {
implementation("com.apollographql.apollo:apollo-runtime:x.y.z")
}

Download your schema.json file

Apollo Android requires your 's schema as a schema.json file. You can obtain the contents of this file by running an on your server.

Note: If you don't have a GraphQL server yet, you can use the server from the tutorial: https://apollo-fullstack-tutorial.herokuapp.com/graphql.

The Apollo Gradle plugin exposes a downloadApolloSchema task to help you obtain your schema. Provide this task your server's endpoint and the output location for the schema.json file:

./gradlew downloadApolloSchema \
--endpoint="https://your.domain/graphql/endpoint" \
--schema="src/main/graphql/com/example/schema.json"

If your GraphQL endpoint requires authentication, you can pass custom HTTP headers:

./gradlew downloadApolloSchema \
--endpoint="https://your.domain/graphql/endpoint" \
--schema="app/src/main/graphql/com/example/schema.json" \
--header="Authorization: Bearer $TOKEN"

Add your query

  1. Create a directory for your GraphQL files: src/main/graphql/com/example/
  2. Add your schema.json to the directory: src/main/graphql/com/example/schema.json
  3. Put your query in a .graphql file, next to the schema: src/main/graphql/com/example/LaunchDetails.graphql
src/main/graphql/com/example/LaunchDetails.graphql
query LaunchDetails($id:ID!) {
launch(id: $id) {
id
site
mission {
name
missionPatch(size:LARGE)
}
}
}
  1. Build your project, this will generate the model

Executing your query

You use an instance of the ApolloClient class to interact with your server and cache.

To make a query using your generated models:

// First, create an `ApolloClient`
// Replace the serverUrl with your GraphQL endpoint
ApolloClient apolloClient = ApolloClient.builder()
.serverUrl("https://your.domain/graphql/endpoint")
.build();
// Then enqueue your query
apolloClient.query(new LaunchDetailsQuery("83"))
.enqueue(new ApolloCall.Callback<LaunchDetailsQuery.Data>() {
@Override
public void onResponse(@NotNull Response<LaunchDetailsQuery.Data> response) {
Log.e("Apollo", "Launch site: " + response.getData().launch.site);
}
@Override
public void onFailure(@NotNull ApolloException e) {
Log.e("Apollo", "Error", e);
}
});

What's next

Previous
Get started with Kotlin
Next
Get started with Multiplatform (Experimental)
Rate articleRateEdit on GitHubEditForumsDiscord

© 2024 Apollo Graph Inc., d/b/a Apollo GraphQL.

Privacy Policy

Company