You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@s2graph.apache.org by da...@apache.org on 2018/06/18 06:15:29 UTC

[2/5] incubator-s2graph git commit: make middleware for GraphFormatWriter

make middleware for GraphFormatWriter


Project: http://git-wip-us.apache.org/repos/asf/incubator-s2graph/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-s2graph/commit/c13a5e92
Tree: http://git-wip-us.apache.org/repos/asf/incubator-s2graph/tree/c13a5e92
Diff: http://git-wip-us.apache.org/repos/asf/incubator-s2graph/diff/c13a5e92

Branch: refs/heads/master
Commit: c13a5e92d248f028fab5a110a61f9a98ea0d7993
Parents: bb78c7d
Author: daewon <da...@apache.org>
Authored: Tue Jun 12 17:04:33 2018 +0900
Committer: daewon <da...@apache.org>
Committed: Tue Jun 12 17:04:33 2018 +0900

----------------------------------------------------------------------
 .../apache/s2graph/graphql/GraphQLServer.scala  |  11 +-
 .../graphql/middleware/GraphFormatWriter.scala  | 101 +++++++++++++++++++
 .../s2graph/graphql/middleware/SigmaJS.scala    | 101 -------------------
 3 files changed, 110 insertions(+), 103 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-s2graph/blob/c13a5e92/s2graphql/src/main/scala/org/apache/s2graph/graphql/GraphQLServer.scala
----------------------------------------------------------------------
diff --git a/s2graphql/src/main/scala/org/apache/s2graph/graphql/GraphQLServer.scala b/s2graphql/src/main/scala/org/apache/s2graph/graphql/GraphQLServer.scala
index d333c9e..b444213 100644
--- a/s2graphql/src/main/scala/org/apache/s2graph/graphql/GraphQLServer.scala
+++ b/s2graphql/src/main/scala/org/apache/s2graph/graphql/GraphQLServer.scala
@@ -28,7 +28,7 @@ import akka.http.scaladsl.server._
 import com.typesafe.config.ConfigFactory
 import org.apache.s2graph.core.S2Graph
 import org.apache.s2graph.core.utils.SafeUpdateCache
-import org.apache.s2graph.graphql.middleware.{SigmaJSFormatted}
+import org.apache.s2graph.graphql.middleware.{GraphFormatted}
 import org.apache.s2graph.graphql.repository.GraphRepository
 import org.apache.s2graph.graphql.types.SchemaDef
 import org.slf4j.LoggerFactory
@@ -123,6 +123,13 @@ object GraphQLServer {
     import GraphRepository._
     val resolver: DeferredResolver[GraphRepository] = DeferredResolver.fetchers(vertexFetcher, edgeFetcher)
 
+    val includeGrpaph = vars.fields.get("includeGraph").contains(spray.json.JsBoolean(true))
+    val middleWares = if (includeGrpaph) {
+      GraphFormatted :: Nil
+    } else {
+      Nil
+    }
+
     Executor.execute(
       s2schema,
       query,
@@ -130,7 +137,7 @@ object GraphQLServer {
       variables = vars,
       operationName = op,
       deferredResolver = resolver,
-      middleware = SigmaJSFormatted :: Nil
+      middleware = middleWares
     )
       .map((res: spray.json.JsValue) => OK -> res)
       .recover {

http://git-wip-us.apache.org/repos/asf/incubator-s2graph/blob/c13a5e92/s2graphql/src/main/scala/org/apache/s2graph/graphql/middleware/GraphFormatWriter.scala
----------------------------------------------------------------------
diff --git a/s2graphql/src/main/scala/org/apache/s2graph/graphql/middleware/GraphFormatWriter.scala b/s2graphql/src/main/scala/org/apache/s2graph/graphql/middleware/GraphFormatWriter.scala
new file mode 100644
index 0000000..91fea62
--- /dev/null
+++ b/s2graphql/src/main/scala/org/apache/s2graph/graphql/middleware/GraphFormatWriter.scala
@@ -0,0 +1,101 @@
+package org.apache.s2graph.graphql.middleware
+
+import org.apache.s2graph.core.schema.ServiceColumn
+import org.apache.s2graph.core.{GraphElement, S2EdgeLike, S2VertexLike}
+import org.apache.s2graph.graphql.types.PlayJsonScalarType
+import org.slf4s.LoggerFactory
+import play.api.libs.json._
+import sangria.execution._
+import sangria.schema.Context
+
+
+object GraphFormatted extends Middleware[Any] with MiddlewareAfterField[Any] with MiddlewareExtension[Any] {
+  implicit val logger = LoggerFactory.getLogger(this.getClass)
+
+  type QueryVal = java.util.concurrent.ConcurrentHashMap[GraphElement, Unit]
+
+  type FieldVal = Long
+
+
+  def beforeQuery(context: MiddlewareQueryContext[Any, _, _]) = {
+    new java.util.concurrent.ConcurrentHashMap[GraphElement, Unit]()
+  }
+
+  def afterQuery(queryVal: QueryVal, context: MiddlewareQueryContext[Any, _, _]) = ()
+
+  def toVertexId(v: S2VertexLike, c: ServiceColumn): String = {
+    val innerId = v.innerId.toIdString()
+
+    s"${c.service.serviceName}.${c.columnName}.${innerId}"
+  }
+
+  def toVertexJson(v: S2VertexLike, c: ServiceColumn): JsValue = {
+    Json.obj(
+      "id" -> toVertexId(v, c),
+      "label" -> v.innerId.toIdString()
+    )
+  }
+
+  def toEdgeJson(e: S2EdgeLike): JsValue = {
+    Json.obj(
+      "source" -> toVertexId(e.srcVertex, e.innerLabel.srcColumn),
+      "target" -> toVertexId(e.tgtVertex, e.innerLabel.tgtColumn),
+      "id" -> s"${toVertexId(e.srcVertex, e.innerLabel.srcColumn)}.${e.label()}.${toVertexId(e.tgtVertex, e.innerLabel.tgtColumn)}",
+      "label" -> e.label()
+    )
+  }
+
+  def afterQueryExtensions(queryVal: QueryVal,
+                           context: MiddlewareQueryContext[Any, _, _]
+                          ): Vector[Extension[_]] = {
+
+    import scala.collection.JavaConverters._
+    val elements = queryVal.keys().asScala.toVector
+
+    val edges = elements.collect { case e: S2EdgeLike => e }
+    val vertices = elements.collect { case v: S2VertexLike => v -> v.serviceColumn }
+    val verticesFromEdges = edges.flatMap { e =>
+      val label = e.innerLabel
+      Vector((e.srcVertex, label.srcColumn), (e.tgtVertex, label.srcColumn))
+    }
+
+    val verticesJson = (vertices ++ verticesFromEdges).map { case (v, c) => toVertexJson(v, c) }.distinct
+    val edgeJson = edges.map(toEdgeJson).distinct
+
+    val jsElements = Json.obj(
+      "nodes" -> verticesJson,
+      "edges" -> edgeJson
+    )
+
+    val graph = Json.obj("graph" -> jsElements)
+
+    /**
+      * nodes: [{id, label, x, y, size}, ..],
+      * edges: [{id, source, target, label}]
+      */
+    implicit val iu = PlayJsonScalarType.PlayJsonInputUnmarshaller
+    Vector(Extension[JsValue](graph))
+  }
+
+  def beforeField(queryVal: QueryVal, mctx: MiddlewareQueryContext[Any, _, _], ctx: Context[Any, _]) = {
+    continue(System.currentTimeMillis())
+  }
+
+  def afterField(queryVal: QueryVal, fieldVal: FieldVal, value: Any, mctx: MiddlewareQueryContext[Any, _, _], ctx: Context[Any, _]) = {
+    //    logger.info(s"${ctx.parentType.name}.${ctx.field.name} = ${value.getClass.getName}")
+
+    value match {
+      case ls: Seq[_] => ls.foreach {
+        case e: GraphElement => queryVal.put(e, ())
+        case _ =>
+      }
+      case e: GraphElement => queryVal.put(e, ())
+      case _ =>
+    }
+
+    None
+  }
+}
+
+
+

http://git-wip-us.apache.org/repos/asf/incubator-s2graph/blob/c13a5e92/s2graphql/src/main/scala/org/apache/s2graph/graphql/middleware/SigmaJS.scala
----------------------------------------------------------------------
diff --git a/s2graphql/src/main/scala/org/apache/s2graph/graphql/middleware/SigmaJS.scala b/s2graphql/src/main/scala/org/apache/s2graph/graphql/middleware/SigmaJS.scala
deleted file mode 100644
index bf03ac2..0000000
--- a/s2graphql/src/main/scala/org/apache/s2graph/graphql/middleware/SigmaJS.scala
+++ /dev/null
@@ -1,101 +0,0 @@
-package org.apache.s2graph.graphql.middleware
-
-import org.apache.s2graph.core.schema.ServiceColumn
-import org.apache.s2graph.core.{GraphElement, S2EdgeLike, S2VertexLike}
-import org.apache.s2graph.graphql.types.PlayJsonScalarType
-import org.slf4s.LoggerFactory
-import play.api.libs.json._
-import sangria.execution._
-import sangria.schema.Context
-
-
-object SigmaJSFormatted extends Middleware[Any] with MiddlewareAfterField[Any] with MiddlewareExtension[Any] {
-  implicit val logger = LoggerFactory.getLogger(this.getClass)
-
-  type QueryVal = java.util.concurrent.ConcurrentHashMap[GraphElement, Unit]
-
-  type FieldVal = Long
-
-
-  def beforeQuery(context: MiddlewareQueryContext[Any, _, _]) = {
-    new java.util.concurrent.ConcurrentHashMap[GraphElement, Unit]()
-  }
-
-  def afterQuery(queryVal: QueryVal, context: MiddlewareQueryContext[Any, _, _]) = ()
-
-  def toVertexId(v: S2VertexLike, c: ServiceColumn): String = {
-    val innerId = v.innerId.toIdString()
-
-    s"${c.service.serviceName}.${c.columnName}.${innerId}"
-  }
-
-  def toVertexJson(v: S2VertexLike, c: ServiceColumn): JsValue = {
-    Json.obj(
-      "id" -> toVertexId(v, c),
-      "label" -> v.innerId.toIdString()
-    )
-  }
-
-  def toEdgeJson(e: S2EdgeLike): JsValue = {
-    Json.obj(
-      "source" -> toVertexId(e.srcVertex, e.innerLabel.srcColumn),
-      "target" -> toVertexId(e.tgtVertex, e.innerLabel.tgtColumn),
-      "id" -> s"${toVertexId(e.srcVertex, e.innerLabel.srcColumn)}.${e.label()}.${toVertexId(e.tgtVertex, e.innerLabel.tgtColumn)}",
-      "label" -> e.label()
-    )
-  }
-
-  def afterQueryExtensions(queryVal: QueryVal,
-                           context: MiddlewareQueryContext[Any, _, _]
-                          ): Vector[Extension[_]] = {
-
-    import scala.collection.JavaConverters._
-    val elements = queryVal.keys().asScala.toVector
-
-    val edges = elements.collect { case e: S2EdgeLike => e }
-    val vertices = elements.collect { case v: S2VertexLike => v -> v.serviceColumn }
-    val verticesFromEdges = edges.flatMap { e =>
-      val label = e.innerLabel
-      Vector((e.srcVertex, label.srcColumn), (e.tgtVertex, label.srcColumn))
-    }
-
-    val verticesJson = (vertices ++ verticesFromEdges).distinct.map { case (v, c) => toVertexJson(v, c) }
-    val edgeJson = edges.distinct.map(toEdgeJson)
-
-    val jsElements = Json.obj(
-      "nodes" -> verticesJson,
-      "edges" -> edgeJson
-    )
-
-    val graph = Json.obj("graph" -> jsElements)
-
-    /**
-      * nodes: [{id, label, x, y, size}, ..],
-      * edges: [{id, source, target, label}]
-      */
-    implicit val iu = PlayJsonScalarType.PlayJsonInputUnmarshaller
-    Vector(Extension[JsValue](graph))
-  }
-
-  def beforeField(queryVal: QueryVal, mctx: MiddlewareQueryContext[Any, _, _], ctx: Context[Any, _]) = {
-    continue(System.currentTimeMillis())
-  }
-
-  def afterField(queryVal: QueryVal, fieldVal: FieldVal, value: Any, mctx: MiddlewareQueryContext[Any, _, _], ctx: Context[Any, _]) = {
-    //    logger.info(s"${ctx.parentType.name}.${ctx.field.name} = ${value.getClass.getName}")
-
-    value match {
-      case ls: Seq[_] => ls.foreach {
-        case e: GraphElement => queryVal.put(e, ())
-        case _ =>
-      }
-      case e: GraphElement => queryVal.put(e, ())
-      case _ =>
-    }
-
-    None
-  }
-}
-
-
-