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

Migrating to 1.3

Migrate to Apollo Android 1.3


Apollo Android version 1.3.0 introduces some fixes and improvements that are incompatible with 1.2.x. Updating should be transparent for simple use cases and your project should compile fine. If you're using more advanced features such as custom schema/ files location, Kotlin Gradle scripts and/or transformed queries, or if you encounter a build error after updating, read on for details about the changes.

Gradle plugin changes

The plugin has been rewritten in Kotlin to make it more maintainable and have better support for multiple endpoints. Below are the main changes. Read plugin-configuration.md for a reference of the different options.

New plugin ID

The plugin ID has been changed from com.apollographql.android to com.apollographql.apollo3 to make it clear that the plugin works also for non-Android projects. com.apollographql.android will be removed in a future revision.

// Replace:
apply(plugin = "com.apollographql.android")
// With:
apply(plugin = "com.apollographql.apollo3")
// Replace:
apply plugin: 'com.apollographql.android'
// With:
apply plugin: 'com.apollographql.apollo3'

Using multiple services

The plugin now requires that you specify multiple services explicitly. If you previously had the following layout:

src/main/graphql/com/github/schema.json
src/main/graphql/com/github/GetRepositories.graphql
src/main/graphql/com/starwars/schema.json
src/main/graphql/com/starwars/GetHeroes.graphql

You will need to define 2 services:

build.gradle
apollo {
service("github") {
sourceFolder.set("com/github")
packageName.set("com.github")
}
service("starwars") {
sourceFolder.set("com/starwars")
packageName.set("com.starwars")
}
}

Specifying schema and GraphQL files location

The root schemaFilePath, outputPackageName and sourceSets.graphql are removed and will throw an error if you try to use them. Instead you can use [CompilationUnit] to control what files the compiler will use as inputs.

build.gradle
// Replace:
sourceSets {
main.graphql.srcDirs += "/path/to/your/graphql/queries/dir"
}
// With:
apollo {
service("service") {
srcDir("/path/to/your/graphql/queries/dir")
}
}
build.gradle
// Replace
apollo {
service("service") {
sourceSet {
schemaFilePath = "/path/to/your/schema.json"
exclude = "**/*.gql"
}
outputPackageName = "com.example"
}
}
// With:
apollo {
service("service") {
schemaFile.set(file("/path/to/your/schema.json"))
graphqlSourceDirectorySet.exclude("**/*.gql")
packageName.set("com.example")
}
}

Kotlin DSL

The plugin uses Gradle Properties to support lazy configuration and wiring tasks together.

If you're using Groovy build.gradle build scripts it should work transparently but Kotlin build.gradle.kts build scripts will require you to use the Property.set API:

build.gradle
apollo {
service("service") {
// Replace:
setGenerateKotlinModels(true)
// With:
generateKotlinModels.set(true)
}
}

Also, the classes of the plugin have been split into an api part and an internal one. If you were relying on fully qualified class names from your build.gradle.kts files, you will have to tweak them:

// Replace:
import com.apollographql.apollo3.gradle.ApolloExtension
// With:
import com.apollographql.apollo3.gradle.api.ApolloExtension

Breaking changes in generated Kotlin models with inline fragments:

inlineFragment is no longer generated with a new Apollo 1.3.0 release for Kotlin models.

For example:

previous version of model with inline fragments

data class Hero(
val __typename: String,
/**
* The name of the character
*/
val name: String,
val inlineFragment: HeroCharacter?
) {
val asHuman: AsHuman? = inlineFragment as? AsHuman
val asDroid: AsDroid? = inlineFragment as? AsDroid
...

new version of generated model with inline fragments

data class Hero(
val __typename: String,
/**
* The name of the character
*/
val name: String,
val asHuman: AsHuman?,
val asDroid: AsDroid?
)

Motivation: there is an issue with previous version of generated model, there are cases when specified multiple inline should be resolved for the same GraphQL type. For example imagine that defines this hierarchy of types Character <- Hero <- Human. Having this GraphQL :

query {
character {
name
... on Hero { ... }
... on Human { ... }
}
}

both inline fragments on Hero and on Human should be resolved for character type Human as Hero is super type of Human.

Previous version of generated model for Character didn't resolve both inline fragments but rather first declared ... on Hero. New version resolves both fragments on Hero and on Human.

Migration:

If you have this code to get access to the resolved inline :

when (hero.inlineFragment) {
is Hero.AsHuman -> // ...
is Hero.AsDroid -> // ...
}

you should change it to check all declared inline fragments for nullability, as it's possible now to have multiple resolved fragments:

if (hero.asHuman != null) {
// ...
}
if (hero.asDroid != null) {
// ...
}

Singularization

Singularization rules have been improved (see 1888). That means the name of some classes that were previously wrongly or badly singularized might have changed. Check for a generated class with a similar name if that happens.

Nested class names

Nested classes are now allowed to have the same name as their parent (see 1893). If you were previously using such a class, the numbered suffix will be removed.

Transformed queries removal

Version 1.3.0 can now optionally generate a operationOutput.json file. This file will contain the generated queries source, and operation ID. You can use them to whitelist the operation on your server or any other use case. See 1841 for details.

Since Output.json is a superset of the transformed queries, transformed queries have been removed. If you were using transformed queries, you will now have to use operationOutput.json.

Espresso Idling Resources

Idling Resources integration is moved to AndroidX! This is a potential breaking change for users who has not migrated to AndroidX yet. If you haven't you can still use the 1.2.x version in your test code.

The artifact is also renamed to make its intention more obvious. Documentation for idling resource can be found here

build.gradle
// Replace:
androidTestImplementation("com.apollographql.apollo3:apollo-espresso-support:x.y.z")
// With:
androidTestImplementation("com.apollographql.apollo3:deprecated-apollo-idling-resource:x.y.z")
Next
Get Started
Rate articleRateEdit on GitHubEditForumsDiscord

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

Privacy Policy

Company