Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Doc Updates [WIP] #243

Merged
merged 18 commits into from
Mar 31, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
b561211
Updating some of the notes about GRIP capabilities and how GraphQL
kellrott Feb 16, 2021
dc1fe22
Updating docs around command line usage and graphql setup
kellrott Feb 16, 2021
516de8d
Adding some more documentation
kellrott Feb 16, 2021
561e6e8
Additional documentation on commands
kellrott Feb 16, 2021
ca3d588
Updating menu ordering
kellrott Feb 16, 2021
afc4293
Starting to add framework for including graphql tests in unit testing…
kellrott Feb 17, 2021
dffc616
Updating some bindings to make server and gripql handler code a bit m…
kellrott Feb 17, 2021
10ac192
Run CI on all PRs/
kellrott Feb 17, 2021
27385be
Moving some of the auth tests into the main test directory
kellrott Feb 17, 2021
d8dfce3
Fixing unit tests
kellrott Feb 17, 2021
f948dd2
Unit test now setting up graphql endpoint and sending request.
kellrott Feb 18, 2021
2d4f3d9
Merge remote-tracking branch 'origin/develop-0.7.0' into graphql-testing
kellrott Feb 18, 2021
f8230b5
Getting graphql test to do correct query and running tidy
kellrott Feb 18, 2021
5b93f21
Fixing query optimization issue and having graphql query check output…
kellrott Feb 19, 2021
2a5afac
Merge remote-tracking branch 'origin/develop-0.7.0' into doc-updates
kellrott Feb 19, 2021
c4d49d1
Merge remote-tracking branch 'origin/doc-updates' into graphql-testing
kellrott Feb 23, 2021
6d41fca
Merge pull request #244 from bmeg/graphql-testing
kellrott Mar 12, 2021
c9b5857
Merge remote-tracking branch 'origin/develop-0.7.0' into doc-updates
kellrott Mar 31, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: Go

on: [pull_request]
on: [ pull_request ]

jobs:

Expand Down Expand Up @@ -43,6 +43,9 @@ jobs:
./grip server --rpc-port 18202 --http-port 18201 --config ./test/badger.yml &
sleep 5
make test-conformance
- name: GRIDs unit tests
run: |
go test ./test/... -config badger.yml

gridsTest:
needs: build
Expand Down
File renamed without changes.
File renamed without changes.
19 changes: 3 additions & 16 deletions cmd/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"os/signal"

"github.com/bmeg/grip/config"
"github.com/bmeg/grip/gripql"
"github.com/bmeg/grip/log"
"github.com/bmeg/grip/server"
_ "github.com/go-sql-driver/mysql" //import so mysql will register as a sql driver
Expand All @@ -18,12 +17,11 @@ import (

var conf = &config.Config{}
var configFile string
var schemaFile string

// Run runs an Grip server.
// This opens a database and starts an API server.
// This blocks indefinitely.
func Run(conf *config.Config, schemas map[string]*gripql.Graph, baseDir string) error {
func Run(conf *config.Config, baseDir string) error {
log.ConfigureLogger(conf.Logger)
log.WithFields(log.Fields{"Config": conf}).Info("Starting Server")

Expand All @@ -36,7 +34,7 @@ func Run(conf *config.Config, schemas map[string]*gripql.Graph, baseDir string)
cancel()
}()

srv, err := server.NewGripServer(conf, schemas, baseDir)
srv, err := server.NewGripServer(conf, baseDir, nil)
if err != nil {
return err
}
Expand Down Expand Up @@ -84,24 +82,13 @@ var Cmd = &cobra.Command{
return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
schemaMap := make(map[string]*gripql.Graph)
if schemaFile != "" {
schemas, err := gripql.ParseYAMLGraphFile(schemaFile)
if err != nil {
return fmt.Errorf("error processing schema file: %v", err)
}
for _, s := range schemas {
schemaMap[s.Graph] = s
}
}
return Run(conf, schemaMap, configFile)
return Run(conf, configFile)
},
}

func init() {
flags := Cmd.Flags()
flags.StringVarP(&configFile, "config", "c", configFile, "Config file")
flags.StringVarP(&schemaFile, "schema", "s", schemaFile, "Schema file")
flags.StringVar(&conf.Server.HTTPPort, "http-port", conf.Server.HTTPPort, "HTTP port")
flags.StringVar(&conf.Server.RPCPort, "rpc-port", conf.Server.RPCPort, "TCP+RPC port")
flags.BoolVar(&conf.Server.ReadOnly, "read-only", conf.Server.ReadOnly, "Start server in read-only mode")
Expand Down
10 changes: 6 additions & 4 deletions contrib/grip.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
Database: mongo
Default: mongo

Server:
HTTPPort: 80
ContentDir: /usr/share/grip
MongoDB:
URL: localhost
DBName: grip
Drivers:
mongo:
MongoDB:
URL: mongodb://localhost:27017
DBName: grip
6 changes: 4 additions & 2 deletions engine/core/optimize.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@ func IndexStartOptimize(pipe []*gripql.GraphStatement) []*gripql.GraphStatement
break
}
if i == 0 {
if _, ok := step.GetStatement().(*gripql.GraphStatement_V); ok {
//lookupV = lv
if v, ok := step.GetStatement().(*gripql.GraphStatement_V); ok {
if v.V != nil && len(v.V.Values) > 0 {
break
}
} else {
break
}
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ require (
github.com/kr/pretty v0.1.0
github.com/lib/pq v1.2.0
github.com/logrusorgru/aurora v0.0.0-20190428105938-cea283e61946
github.com/machinebox/graphql v0.2.2
github.com/mitchellh/mapstructure v1.1.2 // indirect
github.com/mongodb/mongo-tools-common v4.0.18+incompatible
github.com/oliveagle/jsonpath v0.0.0-20180606110733-2e52cf6e6852
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,8 @@ github.com/lib/pq v1.2.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
github.com/logrusorgru/aurora v0.0.0-20181002194514-a7b3b318ed4e/go.mod h1:7rIyQOR62GCctdiQpZ/zOJlFyk6y+94wXzv6RNZgaR4=
github.com/logrusorgru/aurora v0.0.0-20190428105938-cea283e61946 h1:z+WaKrgu3kCpcdnbK9YG+JThpOCd1nU5jO5ToVmSlR4=
github.com/logrusorgru/aurora v0.0.0-20190428105938-cea283e61946/go.mod h1:7rIyQOR62GCctdiQpZ/zOJlFyk6y+94wXzv6RNZgaR4=
github.com/machinebox/graphql v0.2.2 h1:dWKpJligYKhYKO5A2gvNhkJdQMNZeChZYyBbrZkBZfo=
github.com/machinebox/graphql v0.2.2/go.mod h1:F+kbVMHuwrQ5tYgU9JXlnskM8nOaFxCAEolaQybkjWA=
github.com/magiconair/properties v1.8.0/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ=
github.com/magiconair/properties v1.8.1/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ=
github.com/mailru/easyjson v0.0.0-20180730094502-03f2033d19d5 h1:0x4qcEHDpruK6ML/m/YSlFUUu0UpRD3I2PHsNCuGnyA=
Expand Down
19 changes: 15 additions & 4 deletions graphql/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ func NewHTTPHandler(rpcAddress, user, password string) (http.Handler, error) {
if err != nil {
return nil, err
}
return NewClientHTTPHandler(client)
}

// NewClientHTTPHandler initilizes a new GraphQLHandler
func NewClientHTTPHandler(client gripql.Client) (http.Handler, error) {
h := &Handler{
client: client,
handlers: map[string]*graphHandler{},
Expand Down Expand Up @@ -164,16 +169,20 @@ func buildObject(name string, obj map[string]interface{}) (*graphql.Object, erro

// handle map
if x, ok := val.(map[string]interface{}); ok {
objFields[key], err = buildObjectField(key, x)
// make object name parent_field
objFields[key], err = buildObjectField(name+"_"+key, x)

// handle slice
} else if x, ok := val.([]interface{}); ok {
objFields[key], err = buildSliceField(key, x)

// handle string
} else if x, ok := val.(string); ok {
objFields[key], err = buildField(x)

if f, err := buildField(x); err == nil {
objFields[key] = f
} else {
log.WithFields(log.Fields{"object": name, "field": key, "error": err}).Error("graphql: buildField ignoring field")
}
// handle other cases
} else {
err = fmt.Errorf("unhandled type: %T %v", val, val)
Expand Down Expand Up @@ -260,6 +269,7 @@ func buildQueryObject(client gripql.Client, graph string, objects map[string]*gr
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
q := gripql.V().HasLabel(label)
if id, ok := p.Args["id"].(string); ok {
fmt.Printf("Doing %s id=%s query", label, id)
q = gripql.V(id).HasLabel(label)
}
result, err := client.Traversal(&gripql.GraphQuery{Graph: graph, Query: q.Statements})
Expand All @@ -268,6 +278,7 @@ func buildQueryObject(client gripql.Client, graph string, objects map[string]*gr
}
out := []interface{}{}
for r := range result {
fmt.Printf("ID query traversal: %s\n", r)
d := r.GetVertex().GetDataMap()
d["id"] = r.GetVertex().Gid
out = append(out, d)
Expand All @@ -285,7 +296,7 @@ func buildQueryObject(client gripql.Client, graph string, objects map[string]*gr
Fields: queryFields,
},
)

fmt.Printf("Query fields: %s\n", queryFields)
return query
}

Expand Down
58 changes: 38 additions & 20 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,39 +48,54 @@ type GripServer struct {
}

// NewGripServer initializes a GRPC server to connect to the graph store
func NewGripServer(conf *config.Config, schemas map[string]*gripql.Graph, baseDir string) (*GripServer, error) {
func NewGripServer(conf *config.Config, baseDir string, drivers map[string]gdbi.GraphDB) (*GripServer, error) {
_, err := os.Stat(conf.Server.WorkDir)
if os.IsNotExist(err) {
err = os.Mkdir(conf.Server.WorkDir, 0700)
if err != nil {
return nil, fmt.Errorf("creating work dir: %v", err)
}
}
if schemas == nil {
schemas = make(map[string]*gripql.Graph)
}
schemas := make(map[string]*gripql.Graph)

gdbs := map[string]gdbi.GraphDB{}
if drivers != nil {
for i, d := range drivers {
gdbs[i] = d
}
}
for name, dConfig := range conf.Drivers {
g, err := StartDriver(dConfig, baseDir)
if err == nil {
gdbs[name] = g
if _, ok := gdbs[name]; !ok {
g, err := StartDriver(dConfig, baseDir)
if err == nil {
gdbs[name] = g
}
}
}

server := &GripServer{dbs: gdbs, conf: conf, schemas: schemas}
/*
for graph, schema := range schemas {
if !server.graphExists(graph) {
_, err := server.AddGraph(context.Background(), &gripql.GraphID{Graph: graph})
if err != nil {
return nil, fmt.Errorf("error creating graph defined by schema '%s': %v", graph, err)
}
}
err = server.addSchemaGraph(context.Background(), schema)
if err != nil {
return nil, err
}*/

if conf.Default == "" {
//if no default is found set it to the first driver found
for i := range gdbs {
if conf.Default == "" {
conf.Default = i
}
}
}
if _, ok := gdbs[conf.Default]; !ok {
return nil, fmt.Errorf("Default driver '%s' does not exist", conf.Default)
}
fmt.Printf("Default graph driver: %s\n", conf.Default)
server.updateGraphMap()
return server, nil
}
Expand Down Expand Up @@ -176,16 +191,19 @@ func (server *GripServer) Serve(pctx context.Context) error {
mux := http.NewServeMux()

// Setup GraphQL handler
user := ""
password := ""
if len(server.conf.Server.BasicAuth) > 0 {
user = server.conf.Server.BasicAuth[0].User
password = server.conf.Server.BasicAuth[0].Password
}
gqlHandler, err := graphql.NewHTTPHandler(server.conf.Server.RPCAddress(), user, password)
if err != nil {
return fmt.Errorf("setting up GraphQL handler: %v", err)
}
/*
user := ""
password := ""
if len(server.conf.Server.BasicAuth) > 0 {
user = server.conf.Server.BasicAuth[0].User
password = server.conf.Server.BasicAuth[0].Password
}
gqlHandler, err := graphql.NewHTTPHandler(server.conf.Server.RPCAddress(), user, password)
if err != nil {
return fmt.Errorf("setting up GraphQL handler: %v", err)
}*/
gqlHandler, err := graphql.NewClientHTTPHandler(gripql.WrapClient(gripql.NewQueryDirectClient(server), nil))

mux.Handle("/graphql/", gqlHandler)

// Setup web ui handler
Expand Down
110 changes: 110 additions & 0 deletions test/graphql/graphql_query_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package graphql

import (
"fmt"
//"io/ioutil"
//"net/http"
//"bytes"
"context"
"testing"

//"encoding/json"

"github.com/machinebox/graphql"
"github.com/oliveagle/jsonpath"
)

var tests = [][]string{
{
`
query {
Character(id:"1"){
id
name
starships_to_Starship {
name
}
}
}
`,
`{}`,
},
}

func TestQuerySet(t *testing.T) {
url := fmt.Sprintf("http://%s/graphql/%s", GraphQLAddr, GraphName)
client := graphql.NewClient(url)

for _, pair := range tests {
req := graphql.NewRequest(pair[0])

ctx := context.Background()
respData := map[string]interface{}{}
if err := client.Run(ctx, req, &respData); err != nil {
t.Error(err)
}
//out, err := json.Marshal(respData)
//if err != nil {
// t.Error(err)
//}
//fmt.Printf("out: %s\n", out)
}
}

func TestCharacterQuery(t *testing.T) {
url := fmt.Sprintf("http://%s/graphql/%s", GraphQLAddr, GraphName)
client := graphql.NewClient(url)

// make a request
req := graphql.NewRequest(`
query {
Character(id:"Character:1"){
id
name
starships_to_Starship {
name
}
}
}
`)

ctx := context.Background()
respData := map[string]interface{}{}
if err := client.Run(ctx, req, &respData); err != nil {
t.Error(err)
}

out, err := jsonpath.JsonPathLookup(respData, "$.Character[0].id")
if err != nil {
t.Error(err)
}
if idStr, ok := out.(string); ok {
if idStr != "Character:1" {
t.Errorf("id mismatch: %s != %s", "Character:1", idStr)
}
} else {
t.Errorf("ID not string")
}
}

func TestIntrospectionQuery(t *testing.T) {
url := fmt.Sprintf("http://%s/graphql/%s", GraphQLAddr, GraphName)
client := graphql.NewClient(url)

// make a request
req := graphql.NewRequest(`{
__type(name:"Character") {
fields {
name
description
}
}
}`)

ctx := context.Background()
respData := map[string]interface{}{}
if err := client.Run(ctx, req, &respData); err != nil {
t.Error(err)
}
//fmt.Printf("Response: %#v\n", respData)
}
Loading