-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5300438
commit 971e2c6
Showing
4 changed files
with
244 additions
and
0 deletions.
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
80 changes: 80 additions & 0 deletions
80
src/main/groovy/example/carnival/micronaut/vine/DatabaseVine.groovy
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,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
132
src/main/groovy/example/carnival/micronaut/vine/GraphqlVine.groovy
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,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 | ||
} | ||
} | ||
|
||
|
||
} |