Skip to content

Instantly share code, notes, and snippets.

View duanebester's full-sized avatar

Duane Bester duanebester

View GitHub Profile
{
"bbox": {
"topLeft": {
"lat": "37.488854011108415",
"lon": "-120.18521828357393"
},
"bottomRight": {
"lat": "18.496235092613475",
"lon": "-93.08337778423378"
}
{
"bbox": {
"topLeft": {
"lat": "37.488854011108415",
"lon": "-120.18521828357393"
},
"bottomRight": {
"lat": "23.496235092613475",
"lon": "-93.08337778423378"
}
@duanebester
duanebester / gql-elastic.endpoint.scala
Created November 20, 2019 19:10
Endpoint with vars
QueryParser.parse(query) match {
case Success(queryAst) =>
val variables = fields.get("variables") match {
case Some(obj: JsObject) => obj
case _ => JsObject.empty
}
complete(executeGraphQLQuery(queryAst, None, variables))
case Failure(error) =>
complete(BadRequest, JsObject("error" -> JsString(error.getMessage)))
private def executeGraphQLQuery(
query: Document,
operation: Option[String], // Not defined yet
vars: JsObject
)(
implicit ec: ExecutionContext
) =
Executor
.execute(
GraphQLSchema.SchemaDefinition,
object GraphQLServer {
val elastic = new Elastic(
ElasticProperties("http://localhost:9200")
)
def endpoint(requestJSON: JsValue)(implicit ec: ExecutionContext): Route = {
val JsObject(fields) = requestJSON
val JsString(query) = fields("query")
@duanebester
duanebester / gql-elastic.Elastic-0.scala
Last active November 26, 2019 16:44
Elastic example class
class Elastic(props: ElasticProperties) extends ElasticHelpers {
implicit val ec: ExecutionContext = ExecutionContext.global
override val elasticProps: ElasticProperties = props
override val elasticClient = ElasticClient(JavaClient(elasticProps))
def searchUsers(filter: Filter) =
elasticClient.execute {
search(USER_INDEX).bool(buildQuery(filter))
}.map(resp => UsersResponse(resp.result.to[User], resp.result.totalHits))
@duanebester
duanebester / typescript-woes-0.ts
Created November 14, 2019 15:24
TypeScript Woes
interface Product {
id: number
}
function getId(product: Product): number {
return product.id;
}
// Does not work in development with typescript :sunglasses:
let product: Product = { id: null };
@duanebester
duanebester / gql-elastic.ElasticHelper.scala
Last active November 22, 2019 22:50
ElasticHelper trait
trait ElasticHelpers {
final val USER_INDEX = "test-users"
final val COFFEE_SHOPS_INDEX = "test-coffee-shops"
val elasticProps: ElasticProperties
val elasticClient: ElasticClient
def searchUsers(filter: Filter): Future[SearchResponse[User]]
def searchCoffeeShops(filter: Filter): Future[CoffeeShopsResponse]
@duanebester
duanebester / gql-elastic.GraphQLSchema-0.scala
Last active November 22, 2019 22:49
Example GraphQLSchema definitions
import sangria.schema._
import sangria.macros.derive._
import sangria.marshalling.sprayJson._
import models.responses._
import models.variables._
import models.common._
object GraphQLSchema {
// Responses
package models.variables
import spray.json.DefaultJsonProtocol
import models.common.Location
// BBox.scala
case class BBox(topLeft: Location, bottomRight: Location)
object BBox extends DefaultJsonProtocol {
implicit val format = jsonFormat2(BBox.apply)
}