Skip to content

Apollo 4 Migration #98

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

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
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
47 changes: 13 additions & 34 deletions README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ Maven plugin that calls the https://github.com/apollographql/apollo-kotlin[Apoll

* A complete project with https://github.com/{project-owner}/spring-boot-apollo-graphql[Spring Boot].

=== Apollo 3 support

* ToDo

=== Apollo 2 support

* Go to support https://github.com/aoudiamoncef/apollo-client-maven-plugin/tree/support/apollo-2[branch].
Expand All @@ -31,9 +35,9 @@ Maven plugin that calls the https://github.com/apollographql/apollo-kotlin[Apoll
----
<dependencies>
<dependency>
<groupId>com.apollographql.apollo3</groupId>
<artifactId>apollo-runtime</artifactId>
<version>3.8.2</version>
<groupId>com.apollographql.apollo</groupId>
<artifactId>apollo-runtime</artifactId>
<version>4.1.1</version>
</dependency>
</dependencies>
----
Expand Down Expand Up @@ -83,12 +87,12 @@ At build time, the plugin will query the server and install this file per the va
[source,xml]
---
<dependency>
<groupId>com.apollographql.apollo3</groupId>
<groupId>com.apollographql.apollo</groupId>
<artifactId>apollo-api-jvm</artifactId>
<version>3.8.2</version>
</dependency>
<dependency>
<groupId>com.apollographql.apollo3</groupId>
<groupId>com.apollographql.apollo</groupId>
<artifactId>apollo-runtime-jvm</artifactId>
<version>3.8.2</version>
</dependency>
Expand Down Expand Up @@ -121,9 +125,6 @@ All plugin configuration properties and their defaults:
<outputDirectory>
${project.build.directory}/generated-sources/graphql-client/lahzouz/
</outputDirectory>
<testDirectory>
${project.build.directory}/generated-sources/graphql-client/lahzouz/test/
</testDirectory>
<debugDirectory>
${project.build.directory}/generated-sources/graphql-client/lahzouz/debug/
</debugDirectory>
Expand All @@ -146,9 +147,7 @@ All plugin configuration properties and their defaults:
<generateKotlinModels>false</generateKotlinModels>
<generateOptionalOperationVariables>false</generateOptionalOperationVariables>
<generateQueryDocument>true</generateQueryDocument>
<generateResponseFields>false</generateResponseFields>
<generateSchema>false</generateSchema>
<generateTestBuilders>false</generateTestBuilders>
<generateDataBuilders>false</generateDataBuilders>
<generateModelBuilders>false</generateModelBuilders>
<nullableFieldStyle>NONE</nullableFieldStyle>
Expand Down Expand Up @@ -208,10 +207,10 @@ Implementation of a custom adapter for `java.time.LocalDate`:

[source,java]
----
import com.apollographql.apollo3.api.Adapter;
import com.apollographql.apollo3.api.CustomScalarAdapters;
import com.apollographql.apollo3.api.json.JsonReader;
import com.apollographql.apollo3.api.json.JsonWriter;
import com.apollographql.apollo.api.Adapter;
import com.apollographql.apollo.api.CustomScalarAdapters;
import com.apollographql.apollo.api.json.JsonReader;
import com.apollographql.apollo.api.json.JsonWriter;
import org.jetbrains.annotations.NotNull;

import java.io.IOException;
Expand Down Expand Up @@ -248,26 +247,6 @@ public class LocalDateGraphQLAdapter implements Adapter<LocalDate> {
</configuration>
----

==== Test Sources

To generate sources for tests:
[source,xml]
----
<configuration>
...
<addSourceRoot>false</addSourceRoot>
<addTestSourceRoot>true</addTestSourceRoot>
<compilationUnit>
<name>example</name>
<outputDirectory>${project.basedir}/target/generated-test-sources/apollo/example</outputDirectory>
<debugDirectory>${project.basedir}/target/generated-test-sources/apollo/example/debug</debugDirectory>
<testDirectory>${project.basedir}/target/generated-test-sources/apollo/example/debug</testDirectory>
<compilationUnit/>
...
</configuration>
----


=== Using Apollo Client

See https://www.apollographql.com/docs/kotlin/[documentation]
14 changes: 4 additions & 10 deletions apollo-client-maven-plugin-tests/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>com.github.aoudiamoncef</groupId>
<artifactId>apollo-client-maven-plugin-parent</artifactId>
<version>7.2.0-SNAPSHOT</version>
<version>8.0.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand All @@ -22,12 +22,12 @@

<dependencies>
<dependency>
<groupId>com.apollographql.apollo3</groupId>
<groupId>com.apollographql.apollo</groupId>
<artifactId>apollo-api-jvm</artifactId>
<version>${apollo.version}</version>
</dependency>
<dependency>
<groupId>com.apollographql.apollo3</groupId>
<groupId>com.apollographql.apollo</groupId>
<artifactId>apollo-runtime-jvm</artifactId>
<version>${apollo.version}</version>
</dependency>
Expand Down Expand Up @@ -105,7 +105,7 @@
<plugin>
<groupId>com.github.aoudiamoncef</groupId>
<artifactId>apollo-client-maven-plugin</artifactId>
<version>7.2.0-SNAPSHOT</version>
<version>8.0.0-SNAPSHOT</version>
<executions>
<execution>
<goals>
Expand All @@ -132,9 +132,6 @@
<outputDirectory>
${project.build.directory}/generated-sources/graphql-client/books/
</outputDirectory>
<testDirectory>
${project.build.directory}/generated-sources/graphql-client/books/test/
</testDirectory>
<debugDirectory>
${project.build.directory}/generated-sources/graphql-client/books/debug/
</debugDirectory>
Expand All @@ -159,13 +156,10 @@
<generateAsInternal>false</generateAsInternal>
<generateFilterNotNull>false</generateFilterNotNull>
<generateFragmentImplementations>false</generateFragmentImplementations>
<generateResponseFields>false</generateResponseFields>
<generateQueryDocument>true</generateQueryDocument>
<generateSchema>false</generateSchema>
<generateTestBuilders>false</generateTestBuilders>
<generateOptionalOperationVariables>false
</generateOptionalOperationVariables>
<nullableFieldStyle>NONE</nullableFieldStyle>
<useSemanticNaming>true</useSemanticNaming>
<targetLanguage>KOTLIN_1_5</targetLanguage>
<sealedClassesForEnumsMatching></sealedClassesForEnumsMatching>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package com.lahzouz.java.graphql.client.tests

import com.apollographql.apollo3.ApolloClient
import com.apollographql.apollo3.api.*
import com.apollographql.apollo3.api.json.JsonReader
import com.apollographql.apollo3.api.json.JsonWriter
import com.apollographql.apollo3.network.okHttpClient
import com.apollographql.apollo.ApolloClient
import com.apollographql.apollo.api.*
import com.apollographql.apollo.api.json.JsonReader
import com.apollographql.apollo.api.json.JsonWriter
import com.apollographql.apollo.network.okHttpClient
import com.coxautodev.graphql.tools.SchemaParser
import com.lahzouz.apollo.graphql.client.GetAuthorsQuery
import com.lahzouz.apollo.graphql.client.GetBooksQuery
Expand All @@ -25,58 +25,75 @@ import javax.servlet.Servlet

@TestInstance(PER_CLASS)
class ApolloClientMavenPluginTest {

private lateinit var server: Undertow
private var port: Int = 0
private lateinit var client: ApolloClient

@BeforeAll
fun setupSpec() {
val libSchema = SchemaParser.newParser()
.file("schema.graphqls")
.resolvers(Query())
.build()
.makeExecutableSchema()
val libSchema =
SchemaParser
.newParser()
.file("schema.graphqls")
.resolvers(Query())
.build()
.makeExecutableSchema()

val servlet = createServlet(libSchema)

val servletBuilder = Servlets.deployment()
.setClassLoader(javaClass.classLoader)
.setContextPath("/")
.setDeploymentName("test")
.addServlets(
Servlets.servlet(
"GraphQLServlet",
Servlet::class.java,
ImmediateInstanceFactory(servlet as Servlet),
).addMapping("/graphql/*"),
)
val servletBuilder =
Servlets
.deployment()
.setClassLoader(javaClass.classLoader)
.setContextPath("/")
.setDeploymentName("test")
.addServlets(
Servlets
.servlet(
"GraphQLServlet",
Servlet::class.java,
ImmediateInstanceFactory(servlet as Servlet),
).addMapping("/graphql/*"),
)

val manager = Servlets.defaultContainer().addDeployment(servletBuilder)
manager.deploy()
server = Undertow.builder()
.addHttpListener(0, "127.0.0.1")
.setHandler(manager.start()).build()
server =
Undertow
.builder()
.addHttpListener(0, "127.0.0.1")
.setHandler(manager.start())
.build()
server.start()

val inetSocketAddress: InetSocketAddress = server.listenerInfo[0].address as InetSocketAddress
port = inetSocketAddress.port

val longCustomScalarTypeAdapter = object : Adapter<Long> {
override fun fromJson(reader: JsonReader, customScalarAdapters: CustomScalarAdapters): Long {
return reader.nextLong() // Assuming the Long value is directly parseable from JSON
}
val longCustomScalarTypeAdapter =
object : Adapter<Long> {
override fun fromJson(
reader: JsonReader,
customScalarAdapters: CustomScalarAdapters,
): Long {
return reader.nextLong() // Assuming the Long value is directly parseable from JSON
}

override fun toJson(writer: JsonWriter, customScalarAdapters: CustomScalarAdapters, value: Long) {
writer.value(value) // Write the Long value to JSON
override fun toJson(
writer: JsonWriter,
customScalarAdapters: CustomScalarAdapters,
value: Long,
) {
writer.value(value) // Write the Long value to JSON
}
}
}

client = ApolloClient.Builder()
.serverUrl("http://127.0.0.1:$port/graphql")
.addCustomScalarAdapter(com.lahzouz.apollo.graphql.client.type.Long.type, longCustomScalarTypeAdapter)
.okHttpClient(OkHttpClient())
.build()
client =
ApolloClient
.Builder()
.serverUrl("http://127.0.0.1:$port/graphql")
.addCustomScalarAdapter(com.lahzouz.apollo.graphql.client.type.Long.type, longCustomScalarTypeAdapter)
.okHttpClient(OkHttpClient())
.build()
}

@AfterAll
Expand All @@ -86,17 +103,19 @@ class ApolloClientMavenPluginTest {

@Test
@DisplayName("generated book query returns data")
fun bookQueryTest(): Unit = runBlocking {
val response = client.query(GetBooksQuery()).execute()
assertThat(response.data?.books).isNotEmpty.hasSize(4)
}
fun bookQueryTest(): Unit =
runBlocking {
val response = client.query(GetBooksQuery()).execute()
assertThat(response.data?.books).isNotEmpty.hasSize(4)
}

@Test
@DisplayName("generated author query returns data")
fun authorQueryTest(): Unit = runBlocking {
val response = client.query(GetAuthorsQuery()).execute()
assertThat(response.data?.authors).isNotEmpty.hasSize(2)
}
fun authorQueryTest(): Unit =
runBlocking {
val response = client.query(GetAuthorsQuery()).execute()
assertThat(response.data?.authors).isNotEmpty.hasSize(2)
}

private fun createServlet(schema: GraphQLSchema): SimpleGraphQLHttpServlet {
val schemaProvider = DefaultGraphQLSchemaProvider(schema)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import java.nio.file.Paths
import kotlin.io.path.exists

class OperationManifestTest {

@Test
@DisplayName("Checks that operationManifest.json file has been generated and it's not empty")
fun operationManifestTest() {
Expand Down
25 changes: 16 additions & 9 deletions apollo-client-maven-plugin-tests/src/test/kotlin/Query.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,29 @@ package com.lahzouz.java.graphql.client.tests
import com.coxautodev.graphql.tools.GraphQLQueryResolver

class Query : GraphQLQueryResolver {

private val books: List<Book>
private val authors: List<Author>

init {
val dickens = Author(name = "Charles Dickens")
val twain = Author(name = "Mark Twain")
authors = listOf(dickens, twain)
books = listOf(
Book(title = "A Christmas Carol", author = dickens, id = 1L),
Book(title = "David Copperfield", author = dickens, id = 2L),
Book(title = "The Adventures of Tom Sawyer", author = twain, id = 3L),
Book(title = "Adventures of Huckleberry Finn", author = twain, id = 4L),
)
books =
listOf(
Book(title = "A Christmas Carol", author = dickens, id = 1L),
Book(title = "David Copperfield", author = dickens, id = 2L),
Book(title = "The Adventures of Tom Sawyer", author = twain, id = 3L),
Book(title = "Adventures of Huckleberry Finn", author = twain, id = 4L),
)
}

data class Book(val title: String, val author: Author, val id: Long)
data class Author(val name: String)
data class Book(
val title: String,
val author: Author,
val id: Long,
)

data class Author(
val name: String,
)
}
8 changes: 4 additions & 4 deletions apollo-client-maven-plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>com.github.aoudiamoncef</groupId>
<artifactId>apollo-client-maven-plugin-parent</artifactId>
<version>7.2.0-SNAPSHOT</version>
<version>8.0.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down Expand Up @@ -35,17 +35,17 @@
<artifactId>maven-project</artifactId>
</dependency>
<dependency>
<groupId>com.apollographql.apollo3</groupId>
<groupId>com.apollographql.apollo</groupId>
<artifactId>apollo-compiler</artifactId>
<version>${apollo.version}</version>
</dependency>
<dependency>
<groupId>com.apollographql.apollo3</groupId>
<groupId>com.apollographql.apollo</groupId>
<artifactId>apollo-api-jvm</artifactId>
<version>${apollo.version}</version>
</dependency>
<dependency>
<groupId>com.apollographql.apollo3</groupId>
<groupId>com.apollographql.apollo</groupId>
<artifactId>apollo-ast</artifactId>
<version>${apollo.version}</version>
</dependency>
Expand Down
Loading