Skip to content

Commit 2fc57c7

Browse files
authored
[Gradle Plugin] make convertApolloSchema never up-to-date (#2761)
* mark the convertApolloSchema task as never up-to-date as it's intented to be used from the command line * Annotate parameters as Input
1 parent ef393a1 commit 2fc57c7

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-1
lines changed

apollo-gradle-plugin/src/main/kotlin/com/apollographql/apollo/gradle/internal/ApolloConvertSchemaTask.kt

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import com.apollographql.apollo.compiler.toJson
99
import org.gradle.api.DefaultTask
1010
import org.gradle.api.provider.Property
1111
import org.gradle.api.tasks.Input
12+
import org.gradle.api.tasks.InputFile
1213
import org.gradle.api.tasks.Optional
1314
import org.gradle.api.tasks.OutputFile
1415
import org.gradle.api.tasks.TaskAction
@@ -20,11 +21,24 @@ abstract class ApolloConvertSchemaTask: DefaultTask() {
2021
@get:Option(option = "from", description = "schema to convert from")
2122
abstract val from: Property<String>
2223

23-
@get:OutputFile
24+
// Even if this is points to an output file, for the purpose of the task this is seen as an input
25+
@get:Input
2426
@get:Option(option = "to", description = "schema to convert to")
2527
abstract val to: Property<String>
2628

29+
init {
30+
/**
31+
* This task is really meant to be called from the command line so don't do any up-to-date checks
32+
* If someone wants to register its own conversion task, it should be easy to do using the
33+
* compiler APIs directly (see [convert] below)
34+
* This code actually redundant because the task has no output but adding it make it explicit.
35+
*/
36+
outputs.upToDateWhen { false }
37+
outputs.cacheIf { false }
38+
}
39+
2740
private fun File.isIntrospection() = extension == "json"
41+
2842
fun convert(from: File, to: File) {
2943
check (from.isIntrospection() && !to.isIntrospection() || !from.isIntrospection() && to.isIntrospection()) {
3044
"Cannot convert from ${from.name} to ${to.name}, they are already the same format"

apollo-gradle-plugin/src/test/kotlin/com/apollographql/apollo/gradle/test/ConvertSchemaTests.kt

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,31 @@ class ConvertSchemaTests {
2525
}
2626
}
2727

28+
@Test
29+
fun `convert is never up-to-date`() {
30+
TestUtils.withTestProject("convertSchema") { dir ->
31+
val from = File(dir, "schemas/schema.sdl")
32+
val to = File(dir, "schema.json")
33+
var result = TestUtils.executeTask("convertApolloSchema",
34+
dir,
35+
"--from",
36+
from.absolutePath,
37+
"--to",
38+
to.absolutePath
39+
)
40+
Assert.assertEquals(TaskOutcome.SUCCESS, result.task(":convertApolloSchema")!!.outcome)
41+
result = TestUtils.executeTask("convertApolloSchema",
42+
dir,
43+
"--from",
44+
from.absolutePath,
45+
"--to",
46+
to.absolutePath
47+
)
48+
// even if inputs are the same,
49+
Assert.assertEquals(TaskOutcome.SUCCESS, result.task(":convertApolloSchema")!!.outcome)
50+
}
51+
}
52+
2853
@Test
2954
fun `convert from Json to SDL works`() {
3055
TestUtils.withTestProject("convertSchema") { dir ->

0 commit comments

Comments
 (0)