Created
December 13, 2018 21:36
-
-
Save tonis2/7975cab032cb7703a3572b31bb97599f to your computer and use it in GitHub Desktop.
Caylel, save data and load with graphql
This file contains hidden or 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
| package main | |
| import ( | |
| "bytes" | |
| "context" | |
| "encoding/json" | |
| "fmt" | |
| "log" | |
| "math/rand" | |
| "strings" | |
| "github.com/cayleygraph/cayley" | |
| "github.com/cayleygraph/cayley/graph" | |
| _ "github.com/cayleygraph/cayley/graph/kv/bolt" | |
| "github.com/cayleygraph/cayley/quad" | |
| "github.com/cayleygraph/cayley/query/graphql" | |
| _ "github.com/cayleygraph/cayley/query/graphql" | |
| "github.com/cayleygraph/cayley/schema" | |
| ) | |
| var dbPath = "./databases" | |
| var queryString = `{ | |
| nodes(name:"admin1"){ | |
| name, | |
| actor | |
| } | |
| }` | |
| type Movies struct { | |
| ID quad.IRI `quad:"@id"` | |
| Name string `json:"name" quad:"name"` | |
| Actor string `json:"actor" quad:"actor"` | |
| Type string `json:"type" quad:"type"` | |
| } | |
| func init() { | |
| schema.RegisterType("Movies", Movies{}) | |
| } | |
| func checkErr(err error) { | |
| if err != nil { | |
| log.Fatal(err) | |
| } | |
| } | |
| func Insert(h *cayley.Handle, o interface{}) error { | |
| qw := graph.NewWriter(h) | |
| defer qw.Close() // don't forget to close a writer; it has some internal buffering | |
| _, err := schema.WriteAsQuads(qw, o) | |
| return err | |
| } | |
| func genID() quad.IRI { | |
| return quad.IRI(fmt.Sprintf("%x", rand.Intn(0xffff))) | |
| } | |
| func main() { | |
| store := initializeAndOpenGraph(dbPath) | |
| server := graphql.NewSession(store) | |
| Insert(store, Movies{ | |
| ID: genID(), | |
| Name: "admin1", | |
| Actor: "neeger", | |
| Type: "action", | |
| }) | |
| //--------------------------------------------------------- | |
| it := store.QuadsAllIterator() | |
| defer it.Close() | |
| // get all admins | |
| var savedMovies []Movies | |
| checkErr(schema.LoadTo(nil, store, &savedMovies)) | |
| fmt.Println("Movies:") | |
| for _, a := range savedMovies { | |
| fmt.Printf("%+v\n", a) | |
| } | |
| fmt.Println() | |
| q, err := graphql.Parse(strings.NewReader(queryString)) | |
| if err != nil { | |
| log.Fatalln(err) | |
| } | |
| out, err := q.Execute(context.Background(), store) | |
| if err != nil { | |
| log.Fatalln(err) | |
| } | |
| fmt.Println(toJson(out)) | |
| fmt.Println(server) | |
| } | |
| func initializeAndOpenGraph(dbFile string) *cayley.Handle { | |
| graph.InitQuadStore("bolt", dbFile, nil) | |
| // Open and use the database | |
| store, err := cayley.NewGraph("bolt", dbFile, nil) | |
| if err != nil { | |
| log.Fatalln(err) | |
| } | |
| return store | |
| } | |
| func toJson(o interface{}) string { | |
| buf := bytes.NewBuffer(nil) | |
| json.NewEncoder(buf).Encode(o) | |
| buf2 := bytes.NewBuffer(nil) | |
| json.Indent(buf2, buf.Bytes(), "", " ") | |
| return buf2.String() | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment