graphik

package module
v0.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 26, 2020 License: Apache-2.0 Imports: 24 Imported by: 0

README

graphik-client-goGoDoc

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

type Client struct {
	// contains filtered or unexported fields
}

func NewClient

func NewClient(ctx context.Context, target string, opts ...Opt) (*Client, error)
Example
package main

import (
	"context"
	"fmt"
	"github.com/autom8ter/graphik-client-go"
	"github.com/autom8ter/graphik/logger"
	"github.com/golang/protobuf/ptypes/empty"
	"go.uber.org/zap"
	"golang.org/x/oauth2/google"
	"log"
)

func main() {
	ctx := context.Background()

	// ensure graphik server is started with --auth.jwks=https://www.googleapis.com/oauth2/v3/certs
	source, err := google.DefaultTokenSource(context.Background(), "https://www.googleapis.com/auth/devstorage.full_control")
	if err != nil {
		log.Print(err)
		return
	}

	cli, err := graphik.NewClient(ctx, "localhost:7820", graphik.WithTokenSource(source))
	if err != nil {
		log.Print(err)
		return
	}
	pong, err := cli.Ping(ctx, &empty.Empty{})
	if err != nil {
		logger.Error("failed to ping server", zap.Error(err))
		return
	}
	fmt.Println(pong.Message)
}
Output:
PONG

func (*Client) CreateEdge

func (c *Client) CreateEdge(ctx context.Context, in *apipb.EdgeConstructor, opts ...grpc.CallOption) (*apipb.Edge, error)
Example
package main

import (
	"context"
	"fmt"
	"github.com/autom8ter/graphik-client-go"

	apipb "github.com/autom8ter/graphik/api"
	"log"
)

var client *graphik.Client

func main() {
	dogs, err := client.SearchNodes(context.Background(), &apipb.Filter{
		Gtype: "dog",
		Expressions: []string{
			`node.attributes.name.contains("Charl")`,
		},
		Limit: 1,
	})
	if err != nil {
		log.Print(err)
		return
	}
	charlie := dogs.GetNodes()[0]
	coleman, err := client.CreateNode(context.Background(), &apipb.NodeConstructor{
		Path: &apipb.Path{
			Gtype: "human",
		},
		Attributes: apipb.NewStruct(map[string]interface{}{
			"name": "Coleman",
		}),
	})
	if err != nil {
		log.Print(err)
		return
	}
	ownerEdge, err := client.CreateEdge(context.Background(), &apipb.EdgeConstructor{
		Path: &apipb.Path{
			Gtype: "owner",
		},
		Attributes: apipb.NewStruct(map[string]interface{}{
			"primary_owner": true,
		}),
		From: charlie.Path,
		To:   coleman.Path,
	})
	if err != nil {
		log.Print(err)
		return
	}
	primary := ownerEdge.Attributes.Fields["primary_owner"].GetBoolValue()
	fmt.Println(primary)
}
Output:
true

func (*Client) CreateEdges

func (c *Client) CreateEdges(ctx context.Context, in *apipb.EdgeConstructors, opts ...grpc.CallOption) (*apipb.Edges, error)

func (*Client) CreateNode

func (c *Client) CreateNode(ctx context.Context, in *apipb.NodeConstructor, opts ...grpc.CallOption) (*apipb.Node, error)
Example
package main

import (
	"context"
	"fmt"
	"github.com/autom8ter/graphik-client-go"

	apipb "github.com/autom8ter/graphik/api"
	"log"
)

var client *graphik.Client

func main() {
	charlie, err := client.CreateNode(context.Background(), &apipb.NodeConstructor{
		Path: &apipb.Path{
			Gtype: "dog",
		},
		Attributes: apipb.NewStruct(map[string]interface{}{
			"name": "Charlie",
		}),
	})
	if err != nil {
		log.Print(err)
		return
	}
	name := charlie.Attributes.Fields["name"].GetStringValue()
	fmt.Println(name)
}
Output:
Charlie

func (*Client) CreateNodes

func (c *Client) CreateNodes(ctx context.Context, in *apipb.NodeConstructors, opts ...grpc.CallOption) (*apipb.Nodes, error)

func (*Client) EdgesFrom

func (c *Client) EdgesFrom(ctx context.Context, in *apipb.EdgeFilter, opts ...grpc.CallOption) (*apipb.Edges, error)

func (*Client) EdgesTo

func (c *Client) EdgesTo(ctx context.Context, in *apipb.EdgeFilter, opts ...grpc.CallOption) (*apipb.Edges, error)

func (*Client) GetEdge

func (c *Client) GetEdge(ctx context.Context, in *apipb.Path, opts ...grpc.CallOption) (*apipb.Edge, error)

func (*Client) GetNode

func (c *Client) GetNode(ctx context.Context, in *apipb.Path, opts ...grpc.CallOption) (*apipb.Node, error)

func (*Client) GetSchema

func (c *Client) GetSchema(ctx context.Context, in *empty.Empty, opts ...grpc.CallOption) (*apipb.Schema, error)
Example
package main

import (
	"context"
	"fmt"
	"github.com/autom8ter/graphik-client-go"
	"github.com/golang/protobuf/ptypes/empty"
	"log"
	"strings"
)

var client *graphik.Client

func main() {
	schema, err := client.GetSchema(context.Background(), &empty.Empty{})
	if err != nil {
		log.Print(err)
		return
	}
	fmt.Printf("node types: %s\n", strings.Join(schema.NodeTypes, ","))
	fmt.Printf("edge types: %s", strings.Join(schema.EdgeTypes, ","))
}
Output:
node types: dog,human,identity
edge types: owner

func (*Client) Me

func (c *Client) Me(ctx context.Context, in *apipb.MeFilter, opts ...grpc.CallOption) (*apipb.NodeDetail, error)
Example
package main

import (
	"context"
	"fmt"
	"github.com/autom8ter/graphik-client-go"

	apipb "github.com/autom8ter/graphik/api"
	"log"
)

var client *graphik.Client

func main() {
	me, err := client.Me(context.Background(), &apipb.MeFilter{
		EdgesFrom: nil,
		EdgesTo:   nil,
	})
	if err != nil {
		log.Print(err)
		return
	}
	issuer := me.GetAttributes().GetFields()["iss"].GetStringValue() // token issuer
	fmt.Println(issuer)
}
Output:
https://accounts.google.com

func (*Client) PatchEdge

func (c *Client) PatchEdge(ctx context.Context, in *apipb.Patch, opts ...grpc.CallOption) (*apipb.Edge, error)

func (*Client) PatchEdges

func (c *Client) PatchEdges(ctx context.Context, in *apipb.PatchFilter, opts ...grpc.CallOption) (*apipb.Edges, error)

func (*Client) PatchNode

func (c *Client) PatchNode(ctx context.Context, in *apipb.Patch, opts ...grpc.CallOption) (*apipb.Node, error)
Example
package main

import (
	"context"
	"fmt"
	"github.com/autom8ter/graphik-client-go"

	apipb "github.com/autom8ter/graphik/api"
	"log"
)

var client *graphik.Client

func main() {
	dogs, err := client.SearchNodes(context.Background(), &apipb.Filter{
		Gtype: "dog",
		Expressions: []string{
			`node.attributes.name.contains("Charl")`,
		},
		Limit: 1,
	})
	if err != nil {
		log.Print(err)
		return
	}
	charlie := dogs.GetNodes()[0]
	charlie, err = client.PatchNode(context.Background(), &apipb.Patch{
		Path: charlie.Path,
		Attributes: apipb.NewStruct(map[string]interface{}{
			"weight": 25,
		}),
	})
	if err != nil {
		log.Print(err)
		return
	}
	fmt.Println(charlie.GetAttributes().GetFields()["weight"].GetNumberValue())
}
Output:
25

func (*Client) PatchNodes

func (c *Client) PatchNodes(ctx context.Context, in *apipb.PatchFilter, opts ...grpc.CallOption) (*apipb.Nodes, error)

func (*Client) Ping

func (c *Client) Ping(ctx context.Context, in *empty.Empty, opts ...grpc.CallOption) (*apipb.Pong, error)

func (*Client) Publish

func (c *Client) Publish(ctx context.Context, in *apipb.OutboundMessage, opts ...grpc.CallOption) (*empty.Empty, error)
Example
package main

import (
	"context"
	"fmt"
	"github.com/autom8ter/graphik-client-go"

	apipb "github.com/autom8ter/graphik/api"
	"log"
)

var client *graphik.Client

func main() {
	res, err := client.Publish(context.Background(), &apipb.OutboundMessage{
		Channel: "testing",
		Data: apipb.NewStruct(map[string]interface{}{
			"text": "hello world",
		}),
	})
	if err != nil {
		log.Print(err)
		return
	}
	fmt.Println(res.String())
}

func (*Client) SearchEdges

func (c *Client) SearchEdges(ctx context.Context, in *apipb.Filter, opts ...grpc.CallOption) (*apipb.Edges, error)
Example
package main

import (
	"context"
	"fmt"
	"github.com/autom8ter/graphik-client-go"

	apipb "github.com/autom8ter/graphik/api"
	"log"
)

var client *graphik.Client

func main() {
	owners, err := client.SearchEdges(context.Background(), &apipb.Filter{
		Gtype: "owner",
		Expressions: []string{
			`edge.attributes.primary_owner`,
		},
		Limit: 1,
	})
	if err != nil {
		log.Print(err)
		return
	}
	coleman := owners.GetEdges()[0]
	primary := coleman.Attributes.Fields["primary_owner"].GetBoolValue()
	fmt.Println(primary)
}
Output:
true

func (*Client) SearchNodes

func (c *Client) SearchNodes(ctx context.Context, in *apipb.Filter, opts ...grpc.CallOption) (*apipb.Nodes, error)
Example
package main

import (
	"context"
	"fmt"
	"github.com/autom8ter/graphik-client-go"

	apipb "github.com/autom8ter/graphik/api"
	"log"
)

var client *graphik.Client

func main() {
	dogs, err := client.SearchNodes(context.Background(), &apipb.Filter{
		Gtype: "dog",
		Expressions: []string{
			`node.attributes.name.contains("Charl")`,
		},
		Limit: 1,
	})
	if err != nil {
		log.Print(err)
		return
	}
	dog := dogs.GetNodes()[0]
	name := dog.Attributes.Fields["name"].GetStringValue()
	fmt.Println(name)
}
Output:
Charlie

func (*Client) Shutdown

func (c *Client) Shutdown(ctx context.Context, in *empty.Empty, opts ...grpc.CallOption) (*empty.Empty, error)

func (*Client) SubGraph

func (c *Client) SubGraph(ctx context.Context, in *apipb.SubGraphFilter) (*apipb.Graph, error)

func (*Client) Subscribe

Example
package main

import (
	"context"
	"fmt"
	"github.com/autom8ter/graphik-client-go"

	apipb "github.com/autom8ter/graphik/api"
	"github.com/autom8ter/machine"
	"log"
	"time"
)

var client *graphik.Client

func main() {
	m := machine.New(context.Background())
	m.Go(func(routine machine.Routine) {
		stream, err := client.Subscribe(context.Background(), &apipb.ChannelFilter{
			Channel:     "testing",
			Expressions: nil,
		})
		if err != nil {
			log.Print(err)
			return
		}
		for {
			msg, err := stream.Recv()
			if err != nil {
				log.Print(err)
				return
			}
			fmt.Println(msg.Data.GetFields()["text"].GetStringValue())
			return
		}
	})
	time.Sleep(1 * time.Second)
	_, err := client.Publish(context.Background(), &apipb.OutboundMessage{
		Channel: "testing",
		Data: apipb.NewStruct(map[string]interface{}{
			"text": "hello world",
		}),
	})
	if err != nil {
		log.Print(err)
		return
	}
	m.Wait()
}
Output:
hello world

type Opt

type Opt func(o *Options)

func WithRetry

func WithRetry(retry uint) Opt

func WithTokenSource

func WithTokenSource(tokenSource oauth2.TokenSource) Opt

type Options

type Options struct {
	// contains filtered or unexported fields
}

type Trigger

type Trigger struct {
	// contains filtered or unexported fields
}

Trigger is an optional/custom external plugin that when added to a Graphik instance, mutates objects at runtime before & after state changes. It should be deployed as a side car to a graphik instance(see Serve())

func NewTrigger

func NewTrigger(fn TriggerFunc, expressions []string) *Trigger

func (Trigger) Filter

func (t Trigger) Filter(ctx context.Context, _ *empty.Empty) (*apipb.ExpressionFilter, error)

func (Trigger) Mutate

func (t Trigger) Mutate(ctx context.Context, interception *apipb.Change) (*apipb.Change, error)

func (Trigger) Ping

func (t Trigger) Ping(ctx context.Context, _ *empty.Empty) (*apipb.Pong, error)

func (Trigger) Serve

func (t Trigger) Serve(ctx context.Context, cfg *flags.PluginFlags)

type TriggerFunc

type TriggerFunc func(ctx context.Context, trigger *apipb.Change) (*apipb.Change, error)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL