Skip to content

Add local schema file support to Gradle plugin extension #1212

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

Merged
merged 3 commits into from
Jul 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions plugins/graphql-kotlin-gradle-plugin/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ graphql {
allowDeprecatedFields = false
// List of custom GraphQL scalar to converter mapping containing information about corresponding Java type and converter that should be used to serialize/deserialize values.
customScalars = listOf(GraphQLScalar("UUID", "java.util.UUID", "com.example.UUIDScalarConverter"))
// GraphQL server endpoint that will be used to for running introspection queries. Alternatively you can download schema directly from `sdlEndpoint`.
// GraphQL server endpoint that will be used to for running introspection queries. Alternatively you can download schema directly from `sdlEndpoint` or specify local `schemaFileName`.
endpoint = "http://localhost:8080/graphql"
// Optional HTTP headers to be specified on an introspection query or SDL request.
headers = mapOf("X-Custom-Header" to "Custom-Header-Value")
Expand All @@ -44,7 +44,9 @@ graphql {
queryFiles = listOf(file("${project.projectDir}/src/main/resources/queries/MyQuery.graphql"))
// JSON serializer that will be used to generate the data classes.
serializer = GraphQLSerializer.JACKSON
// GraphQL server SDL endpoint that will be used to download schema. Alternatively you can run introspection query against `endpoint`.
// GraphQL schema file location. Can be used instead of `endpoint` or `sdlEndpoint`.
schemaFileName = "${project.projectDir}/src/main/resources/myLocalSchema.graphql"
// GraphQL server SDL endpoint that will be used to download schema. Alternatively you can run introspection query against `endpoint` or specify local `schemaFileName`.
sdlEndpoint = "http://localhost:8080/sdl"
// Timeout configuration for introspection query/downloading SDL
timeout {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,11 @@ class GraphQLGradlePlugin : Plugin<Project> {
generateClientTask.dependsOn(downloadSDLTask.path)
generateClientTask.schemaFile.convention(downloadSDLTask.outputFile)
}
extension.clientExtension.schemaFileName != null -> {
generateClientTask.schemaFileName.convention(extension.clientExtension.schemaFileName)
}
else -> {
throw RuntimeException("Invalid GraphQL client extension configuration - missing required endpoint/sdlEndpoint property")
throw RuntimeException("Invalid GraphQL client extension configuration - missing required endpoint/sdlEndpoint/schemaFileName property")
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,12 @@ open class GraphQLPluginExtension {
}

open class GraphQLPluginClientExtension {
/** GraphQL server endpoint that will be used to for running introspection queries. Alternatively you can download schema directly from [sdlEndpoint]. */
/** GraphQL server endpoint that will be used to for running introspection queries. Alternatively you can download schema directly from [sdlEndpoint] or specify local [schemaFileName]. */
var endpoint: String? = null
/** GraphQL server SDL endpoint that will be used to download schema. Alternatively you can run introspection query against [endpoint]. */
/** GraphQL server SDL endpoint that will be used to download schema. Alternatively you can run introspection query against [endpoint] or specify local [schemaFileName]. */
var sdlEndpoint: String? = null
/** GraphQL schema file location. Can be used instead of [endpoint] or [sdlEndpoint]. */
var schemaFileName: String? = null
/** Target package name to be used for generated classes. */
var packageName: String? = null
/** Optional HTTP headers to be specified on an introspection query or SDL request. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,43 @@ class GraphQLGradlePluginIT : GraphQLGradlePluginAbstractIT() {
assertEquals(TaskOutcome.SUCCESS, buildResult.task(":run")?.outcome)
}

@Test
@Tag("kts")
fun `apply the plugin extension to generate client with local schema file (kts)`(@TempDir tempDir: Path) {
val testProjectDirectory = tempDir.toFile()
val buildFileContents =
"""
|application {
| applicationDefaultJvmArgs = listOf("-DgraphQLEndpoint=${wireMockServer.baseUrl()}/graphql")
| mainClassName = "com.example.ApplicationKt"
|}
|
|graphql {
| client {
| schemaFileName = "${'$'}{project.projectDir}/src/main/resources/schema.graphql"
| packageName = "com.example.generated"
| }
|}
""".trimMargin()
testProjectDirectory.generateBuildFileForClient(buildFileContents)
testProjectDirectory.createTestFile("JUnitQuery.graphql", "src/main/resources")
.writeText(testQuery)
testProjectDirectory.createTestFile("Application.kt", "src/main/kotlin/com/example")
.writeText(loadTemplate("Application", mapOf("customScalarsEnabled" to false)))
testProjectDirectory.createTestFile("schema.graphql", "src/main/resources")
.writeText(testSchema)

val buildResult = GradleRunner.create()
.withProjectDir(testProjectDirectory)
.withPluginClasspath()
.withArguments("build", "run", "--stacktrace")
.build()

assertEquals(TaskOutcome.SUCCESS, buildResult.task(":$GENERATE_CLIENT_TASK_NAME")?.outcome)
assertTrue(File(testProjectDirectory, "build/generated/source/graphql/main/com/example/generated/JUnitQuery.kt").exists())
assertEquals(TaskOutcome.SUCCESS, buildResult.task(":run")?.outcome)
}

@Test
@Tag("kts")
fun `apply the plugin extension to generate client (kts)`(@TempDir tempDir: Path) {
Expand Down
12 changes: 8 additions & 4 deletions website/docs/plugins/gradle-plugin-tasks.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ graphql {
allowDeprecatedFields = false
// List of custom GraphQL scalar to converter mapping containing information about corresponding Java type and converter that should be used to serialize/deserialize values.
customScalars = listOf(GraphQLScalar("UUID", "java.util.UUID", "com.example.UUIDScalarConverter"))
// GraphQL server endpoint that will be used to for running introspection queries. Alternatively you can download schema directly from `sdlEndpoint`.
// GraphQL server endpoint that will be used to for running introspection queries. Alternatively you can download schema directly from `sdlEndpoint` or specify local `schemaFileName`.
endpoint = "http://localhost:8080/graphql"
// Optional HTTP headers to be specified on an introspection query or SDL request.
headers = mapOf("X-Custom-Header" to "Custom-Header-Value")
Expand All @@ -130,7 +130,9 @@ graphql {
queryFiles = listOf(file("${project.projectDir}/src/main/resources/queries/MyQuery.graphql"))
// JSON serializer that will be used to generate the data classes.
serializer = GraphQLSerializer.JACKSON
// GraphQL server SDL endpoint that will be used to download schema. Alternatively you can run introspection query against `endpoint`.
// GraphQL schema file location. Can be used instead of `endpoint` or `sdlEndpoint`.
schemaFileName = "${project.projectDir}/src/main/resources/myLocalSchema.graphql"
// GraphQL server SDL endpoint that will be used to download schema. Alternatively you can run introspection query against `endpoint` or specify local `schemaFileName`.
sdlEndpoint = "http://localhost:8080/sdl"
// Timeout configuration for introspection query/downloading SDL
timeout {
Expand Down Expand Up @@ -164,7 +166,7 @@ graphql {
allowDeprecatedFields = false
// List of custom GraphQL scalar to converter mapping containing information about corresponding Java type and converter that should be used to serialize/deserialize values.
customScalars = [new GraphQLScalar("UUID", "java.util.UUID", "com.example.UUIDScalarConverter")]
// GraphQL server endpoint that will be used to for running introspection queries. Alternatively you can download schema directly from `sdlEndpoint`.
// GraphQL server endpoint that will be used to for running introspection queries. Alternatively you can download schema directly from `sdlEndpoint` or specify local `schemaFileName`.
endpoint = "http://localhost:8080/graphql"
// Optional HTTP headers to be specified on an introspection query or SDL request.
headers = ["X-Custom-Header" : "My-Custom-Header-Value"]
Expand All @@ -176,7 +178,9 @@ graphql {
queryFiles = [file("${project.projectDir}/src/main/resources/queries/MyQuery.graphql")]
// JSON serializer that will be used to generate the data classes.
serializer = GraphQLSerializer.JACKSON
// GraphQL server SDL endpoint that will be used to download schema. Alternatively you can run introspection query against `endpoint`.
// GraphQL schema file location. Can be used instead of `endpoint` or `sdlEndpoint`.
schemaFileName = "${project.projectDir}/src/main/resources/myLocalSchema.graphql"
// GraphQL server SDL endpoint that will be used to download schema. Alternatively you can run introspection query against `endpoint` or specify local `schemaFileName`.
sdlEndpoint = "http://localhost:8080/sdl"
// Timeout configuration for introspection query/downloading SDL
timeout { t ->
Expand Down