e2e face recognition
diff --git a/controller/schema/dgraph_schema_store.go b/controller/schema/dgraph_schema_store.go
index 0d345bb..4f434d7 100644
--- a/controller/schema/dgraph_schema_store.go
+++ b/controller/schema/dgraph_schema_store.go
@@ -2,6 +2,7 @@
import (
"bytes"
+ "errors"
"fmt"
"io/ioutil"
"net/http"
@@ -9,44 +10,49 @@
"github.com/golang/glog"
"github.com/itaysk/regogo"
+ "github.com/vektah/gqlparser"
"github.com/vektah/gqlparser/ast"
- "github.com/vektah/gqlparser/parser"
)
const jsonContentType = "application/json"
+const textContentType = "text/plain"
-const getSchemaQuery = `{ "query": "{ getGQLSchema() { schema } }" }`
+const getSchemaQuery = `{ "query": "{ getGQLSchema() { generatedSchema } }" }`
+const runQuery = `{ "query": "%s" }`
-const addSchemaQuery = `{
- "query": "mutation { updateGQLSchema(input: {set: {schema: \"%s\"}}) { gqlSchema { id schema } } }" }`
-
-type DgraphSchemaStore struct {
- dgraphAddress string
+type DgraphClient struct {
+ gqlAdddress string
+ schemaAddress string
gqlSchema string
- schema *ast.SchemaDocument
+ schema *ast.Schema
}
-func NewDgraphSchemaStore(dgraphAddress string) (SchemaStore, error) {
- ret := &DgraphSchemaStore{dgraphAddress: dgraphAddress, gqlSchema: ""}
+func NewDgraphClient(gqlAddress, schemaAddress string) (GraphQLClient, error) {
+ ret := &DgraphClient{
+ gqlAdddress: gqlAddress,
+ schemaAddress: schemaAddress,
+ gqlSchema: ""}
if err := ret.fetchSchema(); err != nil {
return nil, err
}
return ret, nil
}
-func (s *DgraphSchemaStore) Schema() *ast.SchemaDocument {
+func (s *DgraphClient) Schema() *ast.Schema {
return s.schema
}
-func (s *DgraphSchemaStore) AddSchema(gqlSchema string) error {
+func (s *DgraphClient) AddSchema(gqlSchema string) error {
return s.SetSchema(s.gqlSchema + gqlSchema)
}
-func (s *DgraphSchemaStore) SetSchema(gqlSchema string) error {
+func (s *DgraphClient) SetSchema(gqlSchema string) error {
glog.Info("Setting GraphQL schema:")
glog.Info(gqlSchema)
- req := fmt.Sprintf(addSchemaQuery, strings.ReplaceAll(strings.ReplaceAll(gqlSchema, "\n", " "), "\t", " "))
- resp, err := http.Post(s.dgraphAddress, jsonContentType, bytes.NewReader([]byte(req)))
+ resp, err := http.Post(
+ s.schemaAddress+"/schema",
+ textContentType,
+ bytes.NewReader([]byte(gqlSchema)))
if err != nil {
return err
}
@@ -60,9 +66,12 @@
return s.fetchSchema()
}
-func (s *DgraphSchemaStore) fetchSchema() error {
+func (s *DgraphClient) fetchSchema() error {
glog.Infof("Getting GraphQL schema with query: %s", getSchemaQuery)
- resp, err := http.Post(s.dgraphAddress, jsonContentType, bytes.NewReader([]byte(getSchemaQuery)))
+ resp, err := http.Post(
+ s.schemaAddress,
+ jsonContentType,
+ bytes.NewReader([]byte(getSchemaQuery)))
if err != nil {
return err
}
@@ -72,14 +81,56 @@
return err
}
glog.Infof("Result: %s", string(respBody))
- gqlSchema, err := regogo.Get(string(respBody), "input.data.getGQLSchema.schema")
+ gqlSchema, err := regogo.Get(
+ string(respBody),
+ "input.data.getGQLSchema.generatedSchema")
if err != nil {
return err
}
- schema, gqlErr := parser.ParseSchema(&ast.Source{Input: gqlSchema.String()})
+ schema, gqlErr := gqlparser.LoadSchema(&ast.Source{Input: gqlSchema.String()})
if gqlErr != nil {
return gqlErr
}
s.schema = schema
return nil
}
+
+func (s *DgraphClient) RunQuery(query string) (string, error) {
+ _, gqlErr := gqlparser.LoadQuery(s.Schema(), query)
+ if gqlErr != nil {
+ return "", errors.New(gqlErr.Error())
+ }
+ glog.Infof("Running GraphQL query: %s", query)
+ queryJson := fmt.Sprintf(runQuery, sanitizeQuery(query))
+ resp, err := http.Post(
+ s.gqlAdddress,
+ jsonContentType,
+ bytes.NewReader([]byte(queryJson)))
+ if err != nil {
+ return "", err
+ }
+ respBody, err := ioutil.ReadAll(resp.Body)
+ if err != nil {
+ return "", err
+ }
+ respStr := string(respBody)
+ glog.Infof("Result: %s", string(respStr))
+ // errStr, err := regogo.Get(respStr, "input.errors")
+ // if err == nil {
+ // return "", errors.New(errStr.JSON())
+ // }
+ data, err := regogo.Get(respStr, "input.data")
+ if err != nil {
+ return "", err
+ }
+ return data.JSON(), nil
+}
+
+func sanitizeSchema(schema string) string {
+ return strings.ReplaceAll(
+ strings.ReplaceAll(schema, "\n", " "), "\t", " ")
+}
+
+func sanitizeQuery(query string) string {
+ return strings.ReplaceAll(query, "\"", "\\\"")
+}
diff --git a/controller/schema/schema.go b/controller/schema/schema.go
index a077ce3..fed08e1 100644
--- a/controller/schema/schema.go
+++ b/controller/schema/schema.go
@@ -2,37 +2,11 @@
import (
"github.com/vektah/gqlparser/ast"
- "github.com/vektah/gqlparser/parser"
)
-type SchemaStore interface {
- Schema() *ast.SchemaDocument
- SetSchema(gqlSchema string) error
- AddSchema(gqlSchema string) error
-}
-
-type InMemorySchemaStore struct {
- gqlSchema string
- schema *ast.SchemaDocument
-}
-
-func NewInMemorySchemaStore() SchemaStore {
- return &InMemorySchemaStore{gqlSchema: ""}
-}
-
-func (s *InMemorySchemaStore) Schema() *ast.SchemaDocument {
- return s.schema
-}
-
-func (s *InMemorySchemaStore) AddSchema(gqlSchema string) error {
- return s.SetSchema(s.gqlSchema + gqlSchema)
-}
-
-func (s *InMemorySchemaStore) SetSchema(gqlSchema string) error {
- schema, err := parser.ParseSchema(&ast.Source{Input: gqlSchema})
- if err != nil {
- return err
- }
- s.schema = schema
- return nil
+type GraphQLClient interface {
+ Schema() *ast.Schema
+ SetSchema(schema string) error
+ AddSchema(schema string) error
+ RunQuery(query string) (string, error)
}
diff --git a/controller/schema/schema_test.go b/controller/schema/schema_test.go
deleted file mode 100644
index 0c73694..0000000
--- a/controller/schema/schema_test.go
+++ /dev/null
@@ -1,81 +0,0 @@
-package schema
-
-import (
- "fmt"
- "log"
- "testing"
-)
-
-func TestInMemorySimple(t *testing.T) {
- s := NewInMemorySchemaStore()
- err := s.AddSchema(`
-type M {
- X: Int
-}`)
- if err != nil {
- t.Fatal(err)
- }
- for _, def := range s.Schema().Definitions {
- fmt.Printf("%s - %s\n", def.Name, def.Kind)
- }
-}
-
-func TestInMemory(t *testing.T) {
- s := NewInMemorySchemaStore()
- err := s.AddSchema(`
-type Image {
- id: ID!
- objectPath: String!
-}
-
-type ImageSegment {
- id: ID! @search
- upperLeftX: Float!
- upperLeftY: Float!
- lowerRightX: Float!
- lowerRightY: Float!
- sourceImage: Image!
-}
-
-extend type Image {
- segments: [ImageSegment]
-}
-`)
- if err != nil {
- t.Fatal(err)
- }
- for _, def := range s.Schema().Definitions {
- fmt.Printf("%s - %s\n", def.Name, def.Kind)
- }
-}
-
-func TestDgraph(t *testing.T) {
- s, err := NewDgraphSchemaStore("http://localhost:8080/admin")
- if err != nil {
- t.Fatal(err)
- }
- if s.Schema() != nil {
- for _, def := range s.Schema().Definitions {
- fmt.Printf("%s - %s\n", def.Name, def.Kind)
- }
- }
- err = s.AddSchema("type N { Y: ID! Z: Float }")
- if err != nil {
- t.Fatal(err)
- }
- if s.Schema() != nil {
- for _, def := range s.Schema().Definitions {
- fmt.Printf("%s - %s\n", def.Name, def.Kind)
- }
- }
- log.Print("123123")
- err = s.AddSchema("type M { X: Int }")
- if err != nil {
- t.Fatal(err)
- }
- if s.Schema() != nil {
- for _, def := range s.Schema().Definitions {
- fmt.Printf("%s - %s\n", def.Name, def.Kind)
- }
- }
-}