@@ -10,6 +10,8 @@ import com.apollographql.apollo.ast.GQLDocument
1010import com.apollographql.apollo.ast.GQLFragmentDefinition
1111import com.apollographql.apollo.ast.GQLOperationDefinition
1212import com.apollographql.apollo.ast.GQLSchemaDefinition
13+ import com.apollographql.apollo.ast.GQLSchemaExtension
14+ import com.apollographql.apollo.ast.GQLStringValue
1315import com.apollographql.apollo.ast.GQLTypeDefinition
1416import com.apollographql.apollo.ast.IncompatibleDefinition
1517import 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 )
0 commit comments