Skip to content
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

#77 Upgrade GraphQL example to Knot.x 2.2.1 #79

Merged
merged 2 commits into from
Jul 30, 2020
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
16 changes: 4 additions & 12 deletions api-gateway/graphql-api/README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
[![][license img]][license]
[![][gitter img]][gitter]
# GraphQL Handler
This project provides an example implementation of using GraphQL with [Knot.x HTTP Server](https://github.com/Knotx/knotx-server-http).
It uses [Knot.x Fragments](https://github.com/Knotx/knotx-fragments) to deliver fault-tolerant
GraphQL fetchers logic.


# GraphQL

This project provides an example implementation of using GraphQL with Knotx.
See the [Using GraphQL with Knot.x](http://knotx.io/tutorials/graphql-usage/2_0.html) tutorial for an in depth explanation.

It was created with [Knot.x Starter Kit](https://github.com/Knotx/knotx-starter-kit).
Expand Down Expand Up @@ -51,10 +49,4 @@ The same request in curl:
curl -i -H 'Content-Type: application/json' -X POST -d '{"query": "{book(id: \"q5NoDwAAQBAJ\") {title publisher authors} books(match: \"java\") {title publisher authors}}"}' http://localhost:8092/api/graphql
```

[license]:https://github.com/Cognifide/knotx/blob/master/LICENSE
[license img]:https://img.shields.io/badge/License-Apache%202.0-blue.svg

[gitter]:https://gitter.im/Knotx/Lobby
[gitter img]:https://badges.gitter.im/Knotx/knotx-extensions.svg


16 changes: 11 additions & 5 deletions api-gateway/graphql-api/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,17 @@ plugins {
id("java")
}

repositories {
jcenter()
gradlePluginPortal()
}

dependencies {
subprojects.forEach { "dist"(project(":${it.name}")) }

testImplementation(group = "io.vertx", name = "vertx-core")
testImplementation(group = "io.rest-assured", name = "rest-assured", version = "3.3.0")
testImplementation(group = "com.graphql-java", name = "graphql-java", version = "6.0")
}

sourceSets.named("test") {
Expand All @@ -32,16 +41,13 @@ allprojects {

repositories {
jcenter()
mavenLocal()
maven { url = uri("https://plugins.gradle.org/m2/") }
maven { url = uri("https://oss.sonatype.org/content/groups/staging/") }
maven { url = uri("https://oss.sonatype.org/content/repositories/snapshots") }
}
}

tasks.named("build") {
dependsOn("runTest")
}

apply(from = "gradle/javaAndUnitTests.gradle.kts")
apply(from = "https://raw.githubusercontent.com/Knotx/knotx-starter-kit/2.2.0/gradle/docker.gradle.kts")
apply(from = "https://raw.githubusercontent.com/Knotx/knotx-starter-kit/${project.property("knotxVersion")}/gradle/docker.gradle.kts")
apply(from = "https://raw.githubusercontent.com/Knotx/knotx-starter-kit/${project.property("knotxVersion")}/gradle/javaAndUnitTests.gradle.kts")
6 changes: 2 additions & 4 deletions api-gateway/graphql-api/buildSrc/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
repositories {
mavenLocal()
mavenCentral()
jcenter()
gradlePluginPortal()
}
dependencies {
implementation("com.bmuschko:gradle-docker-plugin:6.6.0")
implementation("io.knotx:knotx-gradle-plugins:0.1.2")
}
}
4 changes: 1 addition & 3 deletions api-gateway/graphql-api/gradle.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
version=2.0.0-SNAPSHOT
knotx.version=2.0.0
knotxVersion=2.0.0
knotx.conf=knotx
knotxVersion=2.2.1
knotxConf=knotx
docker.image.name=knotx-example/graphql-api
54 changes: 0 additions & 54 deletions api-gateway/graphql-api/gradle/javaAndUnitTests.gradle.kts

This file was deleted.

139 changes: 77 additions & 62 deletions api-gateway/graphql-api/knotx/conf/routes/handlers/graphqlHandler.conf
Original file line number Diff line number Diff line change
@@ -1,73 +1,88 @@
schema = "books.graphqls"
# GraphQL Schema
schema = "/books.graphqls"

# Task is a graph of Actions, see more https://github.com/Knotx/knotx-fragments/tree/master/handler#task
tasks {
# fetch a list of books from Google Books API and expose their data to GraphQL fetcher
get-books {
action = getBooks
onTransitions {
_success {
action = exposeInPayload-getBooks
# task factory (https://github.com/Knotx/knotx-fragments/tree/master/task/factory/default)
# that is able to create a task for the fragment is used
taskFactory {
tasks {
# fetch a list of books from Google Books API and expose their data to GraphQL fetcher
get-books {
action = getBooks
onTransitions {
_success {
action = exposeInPayload-getBooks
}
}
}
}

# fetch a single book from Google Books API and expose its data to GraphQL fetcher
get-book {
action = getBook
onTransitions {
_success {
action = exposeInPayload-getBook
# fetch a single book from Google Books API and expose its data to GraphQL fetcher
get-book {
action = getBook
onTransitions {
_success {
action = exposeInPayload-getBook
}
}
}
}
}
nodeFactories = [
{
factory = action
# pre-configured actions, https://github.com/Knotx/knotx-fragments/tree/master/action
config.actions = {
# https://github.com/Knotx/knotx-fragments/tree/master/action/library#http-action
getBooks {
factory = http
config {
endpointOptions {
# Google Books API with a list of books
path = "/books/v1/volumes?q={config.gql.match}"
domain = www.googleapis.com
port = 443
allowedRequestHeaders = ["Content-Type"]
}
webClientOptions {
ssl = true
}
}
}
# custom implementation
exposeInPayload-getBooks {
factory = expose-payload-data
config {
key = getBooks
exposeAs = fetchedData
}
}

# Actions transform Fragment, see more https://github.com/Knotx/knotx-fragments/blob/master/handler/README.md#actions
actions {
getBooks {
factory = http
config {
endpointOptions {
# Google Books API with a list of books
path = "/books/v1/volumes?q={config.gql.match}"
domain = www.googleapis.com
port = 443
allowedRequestHeaders = ["Content-Type"]
# https://github.com/Knotx/knotx-fragments/tree/master/action/library#http-action
getBook {
factory = http
config {
endpointOptions {
# Google Books API with a single book
path = "/books/v1/volumes/{config.gql.id}"
domain = www.googleapis.com
port = 443
allowedRequestHeaders = ["Content-Type"]
}
webClientOptions {
ssl = true
}
}
}
# custom implementation
exposeInPayload-getBook {
factory = expose-payload-data
config {
key = getBook
exposeAs = fetchedData
}
}
}
webClientOptions {
ssl = true
}
}
}
exposeInPayload-getBooks {
factory = expose-payload-data
config {
key = getBooks
exposeAs = fetchedData
}
}
]
}



getBook {
factory = http
config {
endpointOptions {
# Google Books API with a single book
path = "/books/v1/volumes/{config.gql.id}"
domain = www.googleapis.com
port = 443
allowedRequestHeaders = ["Content-Type"]
}
webClientOptions {
ssl = true
}
}
}
exposeInPayload-getBook {
factory = expose-payload-data
config {
key = getBook
exposeAs = fetchedData
}
}
}
45 changes: 38 additions & 7 deletions api-gateway/graphql-api/modules/books/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
plugins {
`java-library`
id("io.knotx.java-library")
}

dependencies {
Expand All @@ -8,18 +8,49 @@ dependencies {
implementation(group = "org.apache.commons", name = "commons-lang3")

"io.knotx:knotx".let { v ->
implementation(platform("$v-dependencies:${project.property("knotx.version")}"))
implementation("$v-server-http-api:${project.property("knotx.version")}")
implementation("$v-fragments-engine:${project.property("knotx.version")}")
implementation("$v-fragments-api:${project.property("knotx.version")}")
implementation("$v-fragments-handler-api:${project.property("knotx.version")}")
implementation("$v-fragments-handler-core:${project.property("knotx.version")}")
implementation(platform("$v-dependencies:${project.property("knotxVersion")}"))
implementation("$v-server-http-api:${project.property("knotxVersion")}")
implementation("$v-fragments-action-api:${project.property("knotxVersion")}")
implementation("$v-fragments-action-library:${project.property("knotxVersion")}")
implementation("$v-fragments-task-api:${project.property("knotxVersion")}")
implementation("$v-fragments-task-engine:${project.property("knotxVersion")}")
implementation("$v-fragments-task-factory-default:${project.property("knotxVersion")}")

testImplementation("$v-junit5:${project.property("knotxVersion")}")
}
"io.vertx:vertx".let { v ->
implementation("$v-web-graphql")
implementation("$v-web")
implementation("$v-web-client")
implementation("$v-rx-java2")
implementation("$v-circuit-breaker")

testImplementation("$v-unit")
testImplementation("$v-junit5")
}

testImplementation(group = "org.mockito", name = "mockito-core", version = "3.4.4")
testImplementation(group = "org.mockito", name = "mockito-junit-jupiter", version = "3.4.4")
}

sourceSets {
test {
resources.srcDir("../../knotx/conf")
}
}

tasks.withType<Test>().configureEach {
systemProperties(Pair("vertx.logger-delegate-factory-class-name", "io.vertx.core.logging.SLF4JLogDelegateFactory"))

failFast = true
useJUnitPlatform()
testLogging {
events = setOf(org.gradle.api.tasks.testing.logging.TestLogEvent.FAILED)
exceptionFormat = org.gradle.api.tasks.testing.logging.TestExceptionFormat.SHORT
}

dependencies {
testImplementation("org.junit.jupiter:junit-jupiter-api")
testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.knotx.example.books.handler.rx;
package io.knotx.example.books;

import static graphql.schema.idl.RuntimeWiring.newRuntimeWiring;

Expand Down Expand Up @@ -76,7 +76,7 @@ private GraphQL setupGraphQL(Vertx vertx, JsonObject config, RoutingContext rout
}

private Reader loadResource(String path) {
return new InputStreamReader(GraphQLHandlerFactory.class.getResourceAsStream("/" + path));
return new InputStreamReader(GraphQLHandlerFactory.class.getResourceAsStream(path));
}

}
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package io.knotx.example.books.action;

import io.knotx.fragments.handler.api.Action;
import io.knotx.fragments.handler.api.ActionFactory;
import io.knotx.fragments.handler.api.domain.FragmentResult;
import io.knotx.fragments.action.api.Action;
import io.knotx.fragments.action.api.ActionFactory;
import io.knotx.fragments.api.FragmentResult;

import io.reactivex.Single;
import io.vertx.core.AsyncResult;
import io.vertx.core.Future;
Expand Down
Loading