Create a gist now

Instantly share code, notes, and snippets.

Basic usage of Neo4j from F# (http://geekswithblogs.net/cskardon/archive/2013/11/27/using-neo4j-with-f-ndash-cypher-2.0.aspx) Check the URL for your local Neo4j server!
open System
open Neo4jClient
open System.Linq
[<CLIMutable>]
type Person = { Name:string; Twitter:string }
[<CLIMutable>]
type Knows = { How:string }
[<EntryPoint>]
let main argv =
let client = new GraphClient(new Uri("http://localhost.:7474/db/data"));
client.Connect();
let createPerson person =
client.Cypher
.Create("(p:Person {param})")
.WithParam("param", person)
.Return<Person>("p")
.Results
.Single()
let pA = createPerson { Name = "PersonA"; Twitter = "tA" }
let pB = createPerson { Name = "PersonB"; Twitter = "tB" }
let pC = createPerson { Name = "PersonC"; Twitter = "tC" }
let pD = createPerson { Name = "PersonD"; Twitter = "tD" }
let follows target source =
client.Cypher
.Match("(s:Person)", "(t:Person)")
.Where(fun s -> s.Twitter = source.Twitter)
.AndWhere(fun t -> t.Twitter = target.Twitter)
.CreateUnique("s-[:follows]->t")
.ExecuteWithoutResults()
pB |> follows pA
pC |> follows pA
pD |> follows pB
pD |> follows pC
let knows target (details : Knows) source =
client.Cypher
.Match("(s:Person)", "(t:Person)")
.Where(fun s -> s.Twitter = source.Twitter)
.AndWhere(fun t -> t.Twitter = target.Twitter)
.CreateUnique("s-[:knows {knowsData}]->t")
.WithParam("knowsData", details)
.ExecuteWithoutResults()
pB |> knows pC {How = "colleagues"}
let pAfollowers =
client.Cypher
.Match("n<-[:follows]-e")
.Where(fun n -> n.Twitter = "tA")
.Return<Person>("e")
.Results
.Select(fun x -> x.Name)
for follower in pAfollowers do
printfn "%s" follower
0 // return an integer exit code
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment