Skip to content

Commit

Permalink
#7
Browse files Browse the repository at this point in the history
  • Loading branch information
augustearth committed Oct 7, 2021
1 parent 5300438 commit 971e2c6
Show file tree
Hide file tree
Showing 4 changed files with 244 additions and 0 deletions.
9 changes: 9 additions & 0 deletions carnival-micronaut-home/config/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,15 @@ carnival-micronaut:
graph:
runtime: "neo4j"
test: "tinker"
database:
server: database-server-name
port: database-port-as-integer
user: database-auth-username
password: database-auth-password
graphql-service:
api:
token: an-api-token

carnival:
cache-mode: OPTIONAL
directories:
Expand Down
23 changes: 23 additions & 0 deletions src/main/groovy/example/carnival/micronaut/config/AppConfig.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,27 @@ class AppConfig {
}
SubConfig subConfig = new SubConfig()


@ConfigurationProperties("example-database")
static class ExampleDatabase {
String server
Integer port
String user
String password
}
ExampleDatabase exampleDatabase = new ExampleDatabase()


@ConfigurationProperties("graphql-service")
static class GraphqlService {

@ConfigurationProperties("api")
static class Api {
String token
}
Api api = new Api()

}
GraphqlService graphqlService = new GraphqlService()

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package example.carnival.micronaut.vine



import javax.inject.Singleton
import javax.inject.Inject

import groovy.transform.ToString
import groovy.util.logging.Slf4j
import groovy.sql.Sql

import static java.sql.ResultSet.*

import io.micronaut.context.annotation.Property

import carnival.util.GenericDataTable
import carnival.util.MappedDataTable
import carnival.core.vine.Vine
import carnival.core.vine.MappedDataTableVineMethod
import carnival.core.vine.GenericDataTableVineMethod

import example.carnival.micronaut.config.AppConfig



@ToString(includeNames=true)
@Slf4j
@Singleton
class DatabaseVine implements Vine {

///////////////////////////////////////////////////////////////////////////
// FIELDS
///////////////////////////////////////////////////////////////////////////

@Inject AppConfig config


///////////////////////////////////////////////////////////////////////////
// UTILITY
///////////////////////////////////////////////////////////////////////////

Sql connect() {
Sql.newInstance(
driver: 'com.microsoft.sqlserver.jdbc.SQLServerDriver',
url: "jdbc:sqlserver://${config.database.server}:${config.database.port};databaseName=TheDatabaseName;",
user: config.database.user,
password: config.database.password
)
}


class MyQuery extends MappedDataTableVineMethod {

MappedDataTable fetch(Map args) {
log.trace "database connect()"
def sql = connect()

def mdt = createDataTable(idFieldName:'ID')

String query = """\
SELECT SomeTable.ID, SomeTable.*
FROM SomeTable
"""
log.debug "query: ${query}"

try {
log.trace "sql.eachRow()"
sql.eachRow(query) { row ->
log.trace "row: $row"
mdt.dataAdd(row)
}
} finally {
if (sql) sql.close()
}
mdt
}

}

}
132 changes: 132 additions & 0 deletions src/main/groovy/example/carnival/micronaut/vine/GraphqlVine.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
package example.carnival.micronaut.vine



import groovy.transform.ToString
import groovy.transform.EqualsAndHashCode
import groovy.util.logging.Slf4j

import javax.inject.Singleton
import javax.annotation.PostConstruct
import javax.annotation.PreDestroy
import java.text.DateFormat
import javax.inject.Inject

import io.micronaut.http.HttpRequest
import io.micronaut.http.HttpResponse
import io.micronaut.http.HttpStatus
import io.micronaut.http.client.RxStreamingHttpClient
import io.micronaut.http.client.annotation.Client
import io.micronaut.context.annotation.Property
import io.reactivex.FlowableSubscriber
import io.reactivex.Flowable

import org.reactivestreams.Subscription
import org.reactivestreams.Subscriber

import carnival.util.MappedDataTable
import carnival.core.vine.Vine
import carnival.core.vine.JsonVineMethod
import carnival.core.vine.CacheMode
import example.carnival.micronaut.config.AppConfig



@ToString(includeNames=true)
@Slf4j
@Singleton
class GraphqlVine implements Vine {



///////////////////////////////////////////////////////////////////////////
// SHARED CLASSES
///////////////////////////////////////////////////////////////////////////

@ToString(includeNames=true)
@EqualsAndHashCode(includes=['query'])
static class QueryRequest {
String query
}


@ToString(includeNames=true)
static class QueryResponse {
String account_id
}



///////////////////////////////////////////////////////////////////////////
// FIELDS
///////////////////////////////////////////////////////////////////////////

@Inject AppConfig config

@Inject @Client("https://api.monday.com/v2")
RxStreamingHttpClient client


///////////////////////////////////////////////////////////////////////////
// CONVENIENCE METHODS
///////////////////////////////////////////////////////////////////////////

String getApiToken() { config.monday.api.token }



///////////////////////////////////////////////////////////////////////////
// Get USERS
///////////////////////////////////////////////////////////////////////////

@ToString(includeNames=true)
static class GetUsersResponse extends QueryResponse {

@ToString(includeNames=true)
static class UserData {

@ToString(includeNames=true)
static class User {
Integer id
String email
}

List<User> users
}
UserData data
}


@Slf4j
class GetUsers extends JsonVineMethod<GetUsersResponse> {
GetUsersResponse fetch(Map args = [:]) {
log.trace "GetUsersResponse args:${args}"

log.trace "getUsers args:${args}"

QueryRequest body = new QueryRequest(
query: "query { users() {id email} }"
)
log.trace "body: ${body}"

HttpRequest req = HttpRequest.POST('/', body)
req.headers.add("Authorization", apiToken)

GetUsersResponse response
client.exchange(req, GetUsersResponse.class)
.blockingSubscribe(
{ HttpResponse res ->
log.trace "res.status = ${res.status}"
response = res.body()
},
{ Throwable t ->
log.error("getUsers", t)
}
)

response
}
}


}

0 comments on commit 971e2c6

Please sign in to comment.