Skip to content

Commit 3cc7893

Browse files
authored
If the new cache is present, do not import @typePolicy and @fieldPolicy (#6896)
1 parent 77814d6 commit 3cc7893

File tree

4 files changed

+81
-4
lines changed

4 files changed

+81
-4
lines changed

libraries/apollo-compiler/src/main/kotlin/com/apollographql/apollo/compiler/ApolloCompiler.kt

Lines changed: 52 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import com.apollographql.apollo.ast.GQLDocument
1010
import com.apollographql.apollo.ast.GQLFragmentDefinition
1111
import com.apollographql.apollo.ast.GQLOperationDefinition
1212
import com.apollographql.apollo.ast.GQLSchemaDefinition
13+
import com.apollographql.apollo.ast.GQLSchemaExtension
14+
import com.apollographql.apollo.ast.GQLStringValue
1315
import com.apollographql.apollo.ast.GQLTypeDefinition
1416
import com.apollographql.apollo.ast.IncompatibleDefinition
1517
import com.apollographql.apollo.ast.Issue
@@ -55,6 +57,35 @@ object ApolloCompiler {
5557
fun warning(message: String)
5658
}
5759

60+
private val cacheCompilerPluginVersion: String? = try {
61+
Class.forName("com.apollographql.cache.apollocompilerplugin.VersionKt")
62+
.getDeclaredField("VERSION")
63+
.get(null) as String
64+
} catch (_: Exception) {
65+
null
66+
}
67+
68+
/**
69+
* The Cache plugin starts providing the cache directives in v1.0.0-alpha.7
70+
*/
71+
private val cacheCompilerPluginHasCacheDirectives: Boolean = cacheCompilerPluginVersion?.isAtLeastAlpha(7) == true
72+
73+
private fun String.isAtLeastAlpha(alpha: Int): Boolean {
74+
val parts = this.split('.', '-')
75+
val major = parts[0].toInt()
76+
if (major < 1) return false
77+
if (major > 1) return true
78+
val minor = parts[1].toInt()
79+
if (minor > 0) return true
80+
val patch = parts[2].toInt()
81+
if (patch > 0) return true
82+
if (parts.size <= 3) return true
83+
val preRelease = parts[3]
84+
if (preRelease != "alpha") return true
85+
val alphaVersion = parts[4].toInt()
86+
return alphaVersion >= alpha
87+
}
88+
5889
fun buildCodegenSchema(
5990
schemaFiles: List<InputFile>,
6091
logger: Logger?,
@@ -114,6 +145,26 @@ object ApolloCompiler {
114145
append(")\n")
115146
}
116147
}
148+
149+
if (schemaDefinitions.none {
150+
it is GQLSchemaExtension && it.directives.any {
151+
it.name == "link" && it.arguments.any {
152+
it.name == "url" && (it.value as? GQLStringValue)?.value?.startsWith("https://specs.apollo.dev/kotlin_labs/") == true
153+
}
154+
}
155+
}) {
156+
if (cacheCompilerPluginHasCacheDirectives) {
157+
// The cache docs recommend importing @typePolicy and @fieldPolicy so we leave them with the default (long) import
158+
appendLine("""
159+
extend schema @link(url: "https://specs.apollo.dev/kotlin_labs/v0.3", import: ["@optional", "@nonnull", "@requiresOptIn", "@targetName"])
160+
""".trimIndent())
161+
} else {
162+
// No cache plugin, import everything
163+
appendLine("""
164+
extend schema @link(url: "https://specs.apollo.dev/kotlin_labs/v0.3", import: ["@optional", "@nonnull", "@requiresOptIn", "@targetName", "@typePolicy", "@fieldPolicy"])
165+
""".trimIndent())
166+
}
167+
}
117168
}
118169
val scalarExtensions = sdl.toGQLDocument().definitions
119170

@@ -124,10 +175,7 @@ object ApolloCompiler {
124175

125176
val result = schemaDocument.validateAsSchema(
126177
validationOptions = SchemaValidationOptions(
127-
/**
128-
* TODO: switch to false
129-
*/
130-
addKotlinLabsDefinitions = true,
178+
addKotlinLabsDefinitions = false,
131179
builtinForeignSchemas() + foreignSchemas
132180
)
133181
)
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
plugins {
2+
id("org.jetbrains.kotlin.jvm")
3+
id("com.apollographql.apollo")
4+
}
5+
6+
apolloTest()
7+
8+
dependencies {
9+
implementation("com.apollographql.apollo:apollo-api")
10+
implementation("com.apollographql.cache:normalized-cache:1.0.0")
11+
}
12+
13+
apollo {
14+
service("service") {
15+
packageName.set("com.example")
16+
17+
plugin("com.apollographql.cache:normalized-cache-apollo-compiler-plugin:1.0.0") {
18+
argument("com.apollographql.cache.packageName", packageName.get())
19+
}
20+
}
21+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
query GetFoo{
2+
foo
3+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
extend schema @link(url: "https://specs.apollo.dev/cache/v0.4", import: ["@typePolicy", "@fieldPolicy"])
2+
3+
type Query @typePolicy(keyFields: "foo"){
4+
foo: Int!
5+
}

0 commit comments

Comments
 (0)