Skip to content

Using the CoCore API from Java

To assist Java developers in integrating with the CoCore GraphQL API, here's a comprehensive guide utilizing the Apollo GraphQL Java library.

1. Add Apollo Client to Your Project

Begin by adding the Apollo Client dependencies to your project. If you're using Gradle, include the following in your build.gradle file:

groovy
plugins {
  id("com.apollographql.apollo3").version("x.y.z")
}

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

Replace x.y.z with the latest version of Apollo Client. You can find the latest version on the Apollo Client GitHub repository.

2. Configure the Apollo Client

Apollo Kotlin generates Kotlin code by default. To use it in a Java project, configure the plugin to generate Java models by adding the following to your build.gradle file:

groovy
apollo {
  service("service") {
    generateKotlinModels.set(false)
  }
}

3. Download the GraphQL Schema

Apollo requires your GraphQL server's schema as a schema.json file. You can obtain this by running an introspection query on your server. The Apollo Gradle plugin provides a downloadApolloSchema task to assist with this:

bash
./gradlew downloadApolloSchema \
  --endpoint="$MY_COCORE_URL/graphql" \
  --schema="src/main/graphql/com/example/schema.json"

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

bash
./gradlew downloadApolloSchema \
  --endpoint="$MY_COCORE_URL/graphql" \
  --schema="src/main/graphql/com/example/schema.json" \
  --header="Authorization: Bearer YOUR_API_KEY"

4. Define GraphQL Queries

Create a directory for your GraphQL files: src/main/graphql/com/example/. Add your schema.json to this directory. Then, define your queries in .graphql files within the same directory. For example, to fetch job information, create a GetJob.graphql file:

graphql
query GetJob($id: ID!) {
  job(id: $id) {
    id
    name
    quantity
  }
}

5. Initialize the Apollo Client in Java

In your Java code, initialize the Apollo Client:

java
import com.apollographql.apollo3.ApolloClient;

ApolloClient apolloClient = new ApolloClient.Builder()
    .serverUrl("$MY_COCORE_URL/graphql")
    .addHttpHeader("Authorization", "Bearer YOUR_API_KEY")
    .build();

6. Execute Queries and Handle Responses

To execute the query and process the response:

java
import com.apollographql.apollo3.api.Response;
import com.apollographql.apollo3.exception.ApolloException;
import com.apollographql.apollo3.rx3.Rx3Apollo;
import io.reactivex.rxjava3.core.Single;

public void fetchJob(String jobId) {
    GetJobQuery query = new GetJobQuery(jobId);
    Single<Response<GetJobQuery.Data>> response = Rx3Apollo.single(apolloClient.query(query));

    response.subscribe(
        result -> {
            if (result.getData() != null) {
                GetJobQuery.Job job = result.getData().getJob();
                System.out.println("Name: " + job.getName() + ", Quantity: " + job.getQuantity());
            } else if (result.getErrors() != null) {
                result.getErrors().forEach(error -> System.out.println("Error: " + error.getMessage()));
            }
        },
        throwable -> System.out.println("Exception: " + throwable.getMessage())
    );
}

7. Error Handling

The above example includes basic error handling by checking for errors in the response and printing them. Ensure you handle exceptions and errors appropriately in your production code.

8. Explore API Documentation

For detailed information on available queries, mutations, and schema definitions, refer to the CoCore API documentation at https://docs.wearecococo.com.

By following these steps, Java developers can effectively integrate with the CoCore GraphQL API using the Apollo Client.

Connect. Combine. Collaborate.
The pioneering open integration platform, dedicated to transforming connectivity in the printing industry.