-
Notifications
You must be signed in to change notification settings - Fork 13
Codegen Migration
This guide covers migrating an existing retrofit-graphql project from the asset-based workflow (@GraphQuery + QueryContainerBuilder + .graphql files in assets/) to the build-time code generation workflow.
Note: The asset-based workflow remains fully supported. Code generation is an optional enhancement, not a replacement. You can also use both approaches in the same project.
| Asset-Based | Codegen |
|---|---|
@GraphQuery annotation on Retrofit methods |
Generated operation constants and request helpers |
QueryContainerBuilder for manual variable construction |
Typed variable classes with named parameters |
Runtime file discovery from assets/
|
Build-time document registry — no runtime file I/O |
| String-based variable keys | Type-safe variable classes |
| No enum generation | GraphQL enums generated as Kotlin enum classes |
plugins {
id("co.anitrend.retrofit.graphql.codegen") version "{tag}"
}Move your .graphql files from assets/graphql/ to src/main/graphql/:
# Before
app/src/main/assets/graphql/queries/GetMarketPlaceApps.graphql
# After
app/src/main/graphql/queries/GetMarketPlaceApps.graphql
Add the schema file:
app/src/main/graphql/schema.graphql
retrofitGraphQL {
packageName.set("your.package.generated")
schema.set(file("src/main/graphql/schema.graphql"))
operations.from(fileTree("src/main/graphql") {
include("**/*.graphql")
})
common {
generateVariables.set(true)
generateResponses.set(true) // optional response model generation
}
scalars {
// Map any custom scalars in your schema
map("DateTime", "kotlin.String")
map("GitObjectID", "kotlin.String")
}
}Remove direct :android-assets and :annotations dependencies — they are no longer needed for codegen-only projects:
// Before
dependencies {
implementation(project(":runtime"))
implementation(project(":api"))
implementation(project(":android-assets"))
implementation(project(":annotations"))
}
// After
dependencies {
implementation(project(":runtime"))
implementation(project(":api"))
}
:runtimestill pulls:android-assetsand:annotationstransitively, so asset-based fallback remains available if needed.
Replace manual converter construction with the registry-based factory:
// Before
val factory = GraphConverter(
processor = GraphProcessor(AssetManagerDiscoveryPlugin(context.assets)),
gson = Gson(),
)
// After
val factory = GraphConverter.create(
context = context,
registry = GeneratedGraphQLRegistry,
)Replace @GraphQuery + QueryContainerBuilder with GraphQLRequest:
// Before
@POST("/graphql")
@GraphQuery("GetMarketPlaceApps")
suspend fun getMarketPlaceApps(
@Body builder: QueryContainerBuilder,
): Response<GraphContainer<MarketPlaceListings>>
// After
@POST("/graphql")
suspend fun getMarketPlaceApps(
@Body request: GraphQLRequest<GetMarketPlaceAppsVariables>,
): Response<GraphContainer<MarketPlaceListings>>If you enable generateResponses, you can also replace hand-written payload DTOs with generated operation-scoped response models:
suspend fun getMarketPlaceApps(
@Body request: GraphQLRequest<GetMarketPlaceAppsVariables>,
): Response<GraphContainer<GetMarketPlaceAppsData>>Replace QueryContainerBuilder with generated request helpers:
// Before
val builder = QueryContainerBuilder()
.putVariable("first", 15)
.putVariable("after", cursor)
service.getMarketPlaceApps(builder)
// After
val request = GetMarketPlaceApps.request(
first = 15,
after = cursor,
)
service.getMarketPlaceApps(request)For multipart uploads, add a RequestBodyPassThroughConverterFactory before the GraphConverter:
Retrofit.Builder()
.addConverterFactory(RequestBodyPassThroughConverterFactory())
.addConverterFactory(GraphConverter.create(context, registry = GeneratedGraphQLRegistry))
.baseUrl(baseUrl)
.build()Use the generated operation metadata:
val request = UploadToStorageBucket.request(upload = filePath)
// Build MultipartBody from the GraphQLRequest fieldsSee GraphQL File Uploads for details.
You don't have to migrate everything at once. Both workflows can coexist:
- Apply the codegen plugin alongside existing
@GraphQueryusage. - Migrate one operation at a time — remove
@GraphQueryfrom a method and switch toGraphQLRequest. - Enable
generateResponsesonly for operations where you want generated payload models. - Once all operations are migrated, remove
:annotationsand:android-assetsdirect dependencies.
- Code Generation — Full codegen reference
- Getting Started — Quick start guide
-
Migration Guide — Migrating from
:libraryto modular dependencies