-
Notifications
You must be signed in to change notification settings - Fork 645
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support for tree shaking in the __Schema.possibleTypes() #5808
Comments
Thanks @rohandhruva ! Would something like below work? public object __Schema {
public fun nodePossibleTypes(): List<ObjectType> = listOf(Node, Product, Review)
public fun productPossibleTypes(): List<ObjectType> = listOf(Product)
public fun reviewPossibleTypes(): List<ObjectType> = listOf(Product)
public fun shippingInformationPossibleTypes(): List<ObjectType> = listOf(ShippingInformation)
} Or are you operating on strings or public object __Schema {
public fun nodePossibleTypes(): List<String> = listOf("Node", "Product", "Review")
public fun productPossibleTypes(): List<String> = listOf("Product")
public fun reviewPossibleTypes(): List<String> = listOf("Product")
public fun shippingInformationPossibleTypes(): List<String> = listOf("ShippingInformation")
} Can you share an example of how you're planning to use it at the callsite? |
Since we removed the code-gened
From a quick look at our code, most of the current usage is for cache normalization in |
Almost feels like using the opposite of override fun cacheKeyForObject(obj: Map<String, Any?>, context: CacheKeyGeneratorContext): CacheKey? {
val keyFields = context.field.type.rawType().keyFields()
val typename = obj["__typename"]
if (__Schema.getObject(typename).implements.map { it.name }.contains("Video")) {
// do sometihng
}
|
@martinbonnin thank you so much for that suggestion. To be honest, I had completely missed the |
I will look into changing the cache key resolver based on override fun getEntityId(): String {
return when (entity.unifiedEntity?.__typename) {
in possibleVideoTypes -> {
entity.unifiedEntity?.onVideo?.videoSummary?.videoId?.toString().orEmpty()
}
in possibleGameTypes -> {
entity.unifiedEntity?.onGame?.gameSummary?.gameId?.toString().orEmpty()
}
else -> {
""
}
}
} Note that for now Do you think we could easily use |
I did too and we were right to forget about it. {
video { # type is interface Video
__typename # typename is object Movie
}
} So you'll need something like Regarding your sample, sounds like something that could be using the super types too instead of the possible types: override fun getEntityId(): String {
val superTypes = __Schema.object(entity.unifiedEntity?.__typename).implements.map { it.name }.toSet()
return when {
superTypes.contains("Video") -> {
entity.unifiedEntity?.onVideo?.videoSummary?.videoId?.toString().orEmpty()
}
superTypes.contains("Game") -> {
entity.unifiedEntity?.onGame?.gameSummary?.gameId?.toString().orEmpty()
}
else -> {
""
}
}
} Note that the two branches above are theorically not disjoint. Something could be both a |
While all of this makes sense, I think as soon as we have |
Right 👍 Unless R8 is smart enough for that but I have doubts... Oh my... I'm starting to doubt everything now. Can we even treeshake the public fun videoPossibleTypes(): List<ObjectType> = listOf(Movie, /* all (possibly) unused types here */) In the At least Strings mitigate this a litlle bit public fun videoPossibleTypes(): List<String> = listOf("Movie", /* all (possibly) unused types here but only as Strings*/) |
I think that sounds reasonable, assuming you are using I think we might still be able to have R8 remove all the types that are completely unused from the schema and |
👍
Sounds like it, I'll dig! |
Pull request here |
Do you have any feedback for the maintainers? Please tell us by taking a one-minute survey. Your responses will help us understand Apollo Kotlin usage and allow us to serve you better. |
Use case
We have configured apollo-kotlin gradle plugin with
alwaysGenerateTypesMatching.set(listOf(".*"))
. This is so that we can match against union or interface types which don't have explicit spreads but we can use the generated.type.name
to match against__typename
.Originally, we also had
generateSchema = true
, so that we could use__Schema.possibleTypes()
, which is very useful for enumerating over interface or union types. However, becausepossibleTypes()
operates at runtime, it's not possible for R8 or proguard to strip unused types from the android app APK.Would it be possible to add support for being able to tree shake unused types out of the generated
__Schema
file? Maybe a middle ground is that we specify all thepossibleTypes
usages in our gradle configuration, and only those are generated, e.g.__Schema.possibleTypesOfFoo()
.Describe the solution you'd like
No response
The text was updated successfully, but these errors were encountered: