paddockpass/graph/main.go

126 lines
1.7 KiB
Go
Raw Normal View History

2020-07-24 20:17:25 +02:00
package graph
2020-07-25 00:39:53 +02:00
import (
"context"
"log"
"github.com/dgraph-io/dgo/v2"
"github.com/dgraph-io/dgo/v2/protos/api"
"google.golang.org/grpc"
)
2020-07-24 20:17:25 +02:00
func ConnectToDgraph() (*dgo.Dgraph, error) {
2020-07-25 00:39:53 +02:00
client, err := grpc.Dial("localhost:9080", grpc.WithInsecure())
2020-07-24 20:17:25 +02:00
if err != nil {
log.Fatal(err)
}
2020-07-25 00:39:53 +02:00
return dgo.NewDgraphClient(
api.NewDgraphClient(client),
), err
}
func InitSchema(c *dgo.Dgraph) error {
err := c.Alter(context.Background(), &api.Operation{
2020-07-26 22:59:23 +02:00
// DropOp: api.Operation_ALL,
2020-07-25 00:39:53 +02:00
Schema: `
averageSpeed: uid .
circuit: uid .
constructor: uid .
constructorId: string .
date: string .
2020-07-28 00:01:06 +02:00
dateOfBirth: string @index(term) .
driver: uid .
fastestLap: uid .
2020-07-26 22:59:23 +02:00
givenName: string .
grid: string .
lap: string .
laps: string .
2020-07-26 22:59:23 +02:00
familyName: string @index(term) .
location: string @index(term) .
millis: string .
name: string @index(term) .
2020-07-25 00:39:53 +02:00
nationality: string @index(term) .
number: string .
points: string .
position: string @index(term) .
2020-07-26 22:59:23 +02:00
race: [uid] .
raceName: string @index(term) .
rank: string .
round: string .
2020-07-26 22:59:23 +02:00
results: [uid] .
season: string @index(term) .
speed: string .
status: string .
time: string .
2020-07-25 00:52:24 +02:00
type: string @index(term) .
units: string .
url: string .
2020-07-25 00:39:53 +02:00
2020-07-26 22:59:23 +02:00
driver_name: string @index(exact) .
2020-07-25 00:39:53 +02:00
type Driver {
2020-07-26 22:59:23 +02:00
dateOfBirth
driver_name
familyName
givenName
nationality
type
2020-07-25 00:39:53 +02:00
}
2020-07-26 22:59:23 +02:00
constructor_name: string @index(fulltext) .
type Constructor {
2020-07-26 22:59:23 +02:00
constructorId
constructor_name
name
nationality
type
url
}
type Result {
2020-07-26 22:59:23 +02:00
constructor
driver
grid
laps
number
points
position
race
status
time
type
}
2020-07-25 00:39:53 +02:00
2020-07-26 22:59:23 +02:00
circuit_name: string @index(fulltext) .
2020-07-25 00:39:53 +02:00
type Circuit {
2020-07-26 22:59:23 +02:00
circuit_name
location
name
type
url
2020-07-25 00:39:53 +02:00
}
2020-07-26 22:59:23 +02:00
race_name: string @index(fulltext) .
2020-07-25 00:39:53 +02:00
2020-07-26 22:59:23 +02:00
type Race {
circuit
date
raceName
race_name
results
round
season
type
url
}`})
2020-07-25 00:39:53 +02:00
if err != nil {
return err
}
return nil
2020-07-24 20:17:25 +02:00
}