-
Notifications
You must be signed in to change notification settings - Fork 1
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
Add GraphQL example #14
Open
csh0711
wants to merge
1
commit into
master
Choose a base branch
from
graphql
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
# Quarkus GraphQL | ||
|
||
This module demonstrates how to create and test a [GraphQL](https://graphql.org/) interface | ||
with [Quarkus](https://quarkus.io/). | ||
|
||
For more information see [Quarkus Smallrye GraphQL](https://quarkus.io/guides/smallrye-graphql). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
val testcontainersVersion: String by project | ||
|
||
dependencies { | ||
implementation("io.quarkus:quarkus-smallrye-graphql") | ||
|
||
testImplementation("io.quarkus:quarkus-smallrye-graphql-client") | ||
} | ||
|
||
noArg { | ||
annotation("cnt.graphql.business.configuration.AddDefaultNoArgConstructor") | ||
} |
33 changes: 33 additions & 0 deletions
33
examples/graphql/src/main/kotlin/cnt/graphql/InitializeData.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package cnt.graphql | ||
|
||
import cnt.graphql.business.Movie | ||
import cnt.graphql.business.MovieService | ||
import io.quarkus.runtime.Startup | ||
import javax.enterprise.context.ApplicationScoped | ||
|
||
@Startup | ||
@ApplicationScoped | ||
class InitializeData { | ||
private val movieService: MovieService | ||
|
||
constructor(movieService: MovieService) { | ||
this.movieService = movieService | ||
// initializeMovies() // uncomment this to create a list of movies on app startup | ||
} | ||
|
||
private fun initializeMovies() = | ||
listOf( | ||
Movie( | ||
title = "Pulp Fiction", | ||
director = "Quentin Tarantino", | ||
publishYear = 1994 | ||
), | ||
Movie( | ||
title = "Jackie Brown", | ||
director = "Quentin Tarantino", | ||
publishYear = 1997 | ||
) | ||
).forEach { | ||
movieService.add(it) | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
examples/graphql/src/main/kotlin/cnt/graphql/api/MovieResource.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package cnt.graphql.api | ||
|
||
import cnt.graphql.business.Movie | ||
import cnt.graphql.business.MovieService | ||
import org.eclipse.microprofile.graphql.Description | ||
import org.eclipse.microprofile.graphql.GraphQLApi | ||
import org.eclipse.microprofile.graphql.Mutation | ||
import org.eclipse.microprofile.graphql.Query | ||
import java.util.* | ||
|
||
|
||
@GraphQLApi | ||
class MovieResource( | ||
private val movieService: MovieService | ||
) { | ||
|
||
@Query | ||
@Description("Get all movies") | ||
fun getAllMovies() = movieService.getAll() | ||
|
||
@Query | ||
@Description("Get a specific movie by its ID") | ||
fun getMovie(id: UUID) = movieService.getSingle(id) | ||
|
||
@Mutation | ||
@Description("Create and save a new movie") | ||
fun createMovie(movie: Movie) = movieService.add(movie) | ||
|
||
@Mutation | ||
@Description("Delete a movie by its ID") | ||
fun deleteMovie(id: UUID) = movieService.delete(id) | ||
|
||
// @Query("allActors") | ||
// @Description("Get all Actors") | ||
// fun getAllActors() = movieService.getAllActors() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. remove? |
||
} |
3 changes: 3 additions & 0 deletions
3
examples/graphql/src/main/kotlin/cnt/graphql/business/AddDefaultNoArgConstructor.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
package cnt.graphql.business | ||
|
||
annotation class AddDefaultNoArgConstructor |
9 changes: 9 additions & 0 deletions
9
examples/graphql/src/main/kotlin/cnt/graphql/business/IdGenerator.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package cnt.graphql.business | ||
|
||
import java.util.* | ||
import javax.enterprise.context.ApplicationScoped | ||
|
||
@ApplicationScoped | ||
class IdGenerator { | ||
fun generateId(): UUID = UUID.randomUUID() | ||
} |
24 changes: 24 additions & 0 deletions
24
examples/graphql/src/main/kotlin/cnt/graphql/business/MovieService.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package cnt.graphql.business | ||
|
||
import cnt.graphql.persistence.MovieRepository | ||
import java.util.* | ||
import javax.enterprise.context.ApplicationScoped | ||
|
||
@ApplicationScoped | ||
class MovieService( | ||
private val idGenerator: IdGenerator, | ||
private val movieRepository: MovieRepository | ||
) { | ||
|
||
fun add(movie: Movie): Movie { | ||
val id = idGenerator.generateId() | ||
return movieRepository.save(movie.copy(id = id)) | ||
} | ||
|
||
fun getAll(): List<Movie> = movieRepository.getAll() | ||
|
||
fun getSingle(id: UUID): Movie? = movieRepository.getById(id) | ||
|
||
fun delete(id: UUID): Boolean = movieRepository.deleteById(id) | ||
|
||
} |
11 changes: 11 additions & 0 deletions
11
examples/graphql/src/main/kotlin/cnt/graphql/business/model.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package cnt.graphql.business | ||
|
||
import java.util.* | ||
|
||
@AddDefaultNoArgConstructor | ||
data class Movie( | ||
var id: UUID? = null, | ||
var title: String, | ||
var director: String, | ||
var publishYear: Int | ||
) |
25 changes: 25 additions & 0 deletions
25
examples/graphql/src/main/kotlin/cnt/graphql/persistence/MovieRepository.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package cnt.graphql.persistence | ||
|
||
import cnt.graphql.business.Movie | ||
import java.util.* | ||
import javax.enterprise.context.ApplicationScoped | ||
|
||
@ApplicationScoped | ||
class MovieRepository { | ||
|
||
private val database = mutableMapOf<UUID, Movie>() | ||
|
||
fun save(movie: Movie): Movie { | ||
val id = movie.id ?: error("id of movie [$movie] was not set!") | ||
database[id] = movie | ||
return movie | ||
} | ||
|
||
fun deleteById(id: UUID) = database.remove(id) != null | ||
|
||
fun getById(id: UUID): Movie? = database[id] | ||
|
||
fun getAll(): List<Movie> = database.values.toMutableList() | ||
|
||
fun deleteAll() = database.clear() | ||
} |
284 changes: 284 additions & 0 deletions
284
examples/graphql/src/main/resources/META-INF/resources/index.html
Large diffs are not rendered by default.
Oops, something went wrong.
Empty file.
78 changes: 78 additions & 0 deletions
78
examples/graphql/src/test/kotlin/cnt/graphql/ApplicationEndToEndTests.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package cnt.graphql | ||
|
||
import cnt.graphql.business.Movie | ||
import io.quarkus.test.junit.QuarkusTest | ||
import io.smallrye.graphql.client.GraphQLClient | ||
import io.smallrye.graphql.client.core.Argument.arg | ||
import io.smallrye.graphql.client.core.Argument.args | ||
import io.smallrye.graphql.client.core.Document.document | ||
import io.smallrye.graphql.client.core.Field.field | ||
import io.smallrye.graphql.client.core.InputObject.inputObject | ||
import io.smallrye.graphql.client.core.InputObjectField.prop | ||
import io.smallrye.graphql.client.core.Operation.operation | ||
import io.smallrye.graphql.client.core.OperationType.MUTATION | ||
import io.smallrye.graphql.client.dynamic.api.DynamicGraphQLClient | ||
import org.assertj.core.api.Assertions.assertThat | ||
import org.junit.jupiter.api.Test | ||
import java.util.* | ||
import javax.inject.Inject | ||
|
||
@QuarkusTest | ||
class ApplicationEndToEndTests { | ||
|
||
@Inject | ||
@GraphQLClient("movie-test-client") | ||
private lateinit var dynamicClient: DynamicGraphQLClient | ||
|
||
@Test | ||
fun `create a movie and retrieve it afterwards`() { | ||
val createQuery = document( | ||
operation( | ||
MUTATION, | ||
field( | ||
"createMovie", | ||
args( | ||
arg( | ||
"movie", inputObject( | ||
prop("title", "Inception"), | ||
prop("director", "Christopher Nolan"), | ||
prop("publishYear", 2010) | ||
) | ||
) | ||
), | ||
field("id"), | ||
field("title"), | ||
field("director"), | ||
field("publishYear") | ||
) | ||
) | ||
) | ||
val createdMovie = dynamicClient.executeSync(createQuery).getObject(Movie::class.java, "createMovie") | ||
assertMovie(createdMovie, createdMovie.id) | ||
|
||
val retrieveQuery = document( | ||
operation( | ||
field( | ||
"movie", | ||
args(arg("id", createdMovie.id)), | ||
field("id"), | ||
field("title"), | ||
field("director"), | ||
field("publishYear") | ||
) | ||
) | ||
) | ||
val retrievedMovie = dynamicClient.executeSync(retrieveQuery).getObject(Movie::class.java, "movie") | ||
assertMovie(retrievedMovie, retrievedMovie.id) | ||
} | ||
|
||
private fun assertMovie(movie: Movie?, id: UUID?) { | ||
assertThat(movie).isNotNull | ||
with(movie!!) { | ||
assertThat(id).isEqualTo(id) | ||
assertThat(title).isEqualTo("Inception") | ||
assertThat(director).isEqualTo("Christopher Nolan") | ||
assertThat(publishYear).isEqualTo(2010) | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Detekt trained me: too many blank lines ;)