MCPcopy Index your code
hub / github.com/RedisGraph/redisgraph-go

github.com/RedisGraph/redisgraph-go @v2.1.1

Chat with this repo
repository ↗ · DeepWiki ↗ · release v2.1.1 ↗ · + Follow
120 symbols 450 edges 10 files 34 documented · 28%
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

license CircleCI GitHub issues Codecov Go Report Card GoDoc

redisgraph-go

Forum Discord

redisgraph-go is a Golang client for the RedisGraph module. It relies on redigo for Redis connection management and provides support for RedisGraph's QUERY, EXPLAIN, and DELETE commands.

Installation

Simply do:

$ go get github.com/redislabs/redisgraph-go

Usage

The complete redisgraph-go API is documented on GoDoc.

package main

import (
    "fmt"
    "os"

    "github.com/gomodule/redigo/redis"
    rg "github.com/redislabs/redisgraph-go"
)

func main() {
    conn, _ := redis.Dial("tcp", "127.0.0.1:6379")
    defer conn.Close()

    graph := rg.GraphNew("social", conn)

    graph.Delete()

    john := rg.Node{
        Label: "person",
        Properties: map[string]interface{}{
            "name":   "John Doe",
            "age":    33,
            "gender": "male",
            "status": "single",
        },
    }
    graph.AddNode(&john)

    japan := rg.Node{
        Label: "country",
        Properties: map[string]interface{}{
            "name": "Japan",
        },
    }
    graph.AddNode(&japan)

    edge := rg.Edge{
        Source:      &john,
        Relation:    "visited",
        Destination: &japan,
    }
    graph.AddEdge(&edge)

    graph.Commit()

    query := `MATCH (p:person)-[v:visited]->(c:country)
           RETURN p.name, p.age, c.name`

    // result is a QueryResult struct containing the query's generated records and statistics.
    result, _ := graph.Query(query)

    // Pretty-print the full result set as a table.
    result.PrettyPrint()

    // Iterate over each individual Record in the result.
    fmt.Println("Visited countries by person:")
    for result.Next() { // Next returns true until the iterator is depleted.
        // Get the current Record.
        r := result.Record()

        // Entries in the Record can be accessed by index or key.
        pName := r.GetByIndex(0)
        fmt.Printf("\nName: %s\n", pName)
        pAge, _ := r.Get("p.age")
        fmt.Printf("\nAge: %d\n", pAge)
    }

    // Path matching example.
    query = "MATCH p = (:person)-[:visited]->(:country) RETURN p"
    result, err := graph.Query(query)
    if err != nil {
        fmt.Println(err)
        os.Exit(1)
    }
    fmt.Println("Pathes of persons visiting countries:")
    for result.Next() {
        r := result.Record()
        p, ok := r.GetByIndex(0).(rg.Path)
        fmt.Printf("%s %v\n", p, ok)
    }
}

Running the above produces the output:

+----------+-------+--------+
|  p.name  | p.age | c.name |
+----------+-------+--------+
| John Doe |    33 | Japan  |
+----------+-------+--------+

Query internal execution time 1.623063

Name: John Doe

Age: 33

Running queries with timeouts

Queries can be run with a millisecond-level timeout as described in the module documentation. To take advantage of this feature, the QueryOptions struct should be used:

options := NewQueryOptions().SetTimeout(10) // 10-millisecond timeout
res, err := graph.QueryWithOptions("MATCH (src {name: 'John Doe'})-[*]->(dest) RETURN dest", options)

ParameterizedQueryWithOptions and ROQueryWithOptions endpoints are also exposed by the client.

Running tests

A simple test suite is provided, and can be run with:

$ go test

The tests expect a Redis server with the RedisGraph module loaded to be available at localhost:6379

License

redisgraph-go is distributed under the BSD3 license - see LICENSE

Core symbols most depended-on inside this repo

Query
called by 32
graph.go
GetProperty
called by 27
node.go
SetProperty
called by 20
node.go
GetByIndex
called by 18
record.go
ToString
called by 15
utils.go
Next
called by 13
query_result.go
Record
called by 13
query_result.go
Values
called by 12
record.go

Shape

Method 71
Function 39
Struct 8
TypeAlias 2

Languages

Go100%

Modules by API surface

query_result.go31 symbols
graph.go26 symbols
client_test.go19 symbols
path.go11 symbols
edge.go8 symbols
utils.go6 symbols
record.go6 symbols
node.go6 symbols
example_graph_test.go5 symbols
examples/redisgraph_tls_client/redisgraph_tls_client.go2 symbols

For agents

$ claude mcp add redisgraph-go \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact