Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🤖 Sandbox code update #53

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 24 additions & 33 deletions code/go/example.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
package main

import (
"context"
"fmt"
"github.com/neo4j/neo4j-go-driver/v4/neo4j"
"io"
"github.com/neo4j/neo4j-go-driver/v5/neo4j"
"reflect"
)

Expand All @@ -19,46 +19,37 @@ func main() {
}
}

func runQuery(uri, database, username, password string) (result []string, err error) {
driver, err := neo4j.NewDriver(uri, neo4j.BasicAuth(username, password, ""))
func runQuery(uri, database, username, password string) (_ []string, err error) {
ctx := context.Background()
driver, err := neo4j.NewDriverWithContext(uri, neo4j.BasicAuth(username, password, ""))
if err != nil {
return nil, err
}
defer func() {err = handleClose(driver, err)}()
session := driver.NewSession(neo4j.SessionConfig{AccessMode: neo4j.AccessModeRead, DatabaseName: database})
defer func() {err = handleClose(session, err)}()
results, err := session.ReadTransaction(func(transaction neo4j.Transaction) (interface{}, error) {
result, err := transaction.Run(
`
MATCH (m:Movie {title:$movie})<-[:RATED]-(u:User)-[:RATED]->(rec:Movie)
RETURN distinct rec.title AS recommendation LIMIT 20
`, map[string]interface{}{
"movie": "Crimson Tide",
})
defer func() { err = handleClose(ctx, driver, err) }()
query := " MATCH (m:Movie {title:$movie})<-[:RATED]-(u:User)-[:RATED]->(rec:Movie)
RETURN distinct rec.title AS recommendation LIMIT 20
params := map[string]any{"movie": "Crimson Tide"}
result, err := neo4j.ExecuteQuery(ctx, driver, query, params,
neo4j.EagerResultTransformer,
neo4j.ExecuteQueryWithDatabase(database),
neo4j.ExecuteQueryWithReadersRouting())
if err != nil {
return nil, err
}
recommendations := make([]string, len(result.Records))
for i, record := range result.Records {
// this assumes all actors have names, hence ignoring the 2nd returned value
name, _, err := neo4j.GetRecordValue[string](record, "recommendation")
if err != nil {
return nil, err
}
var arr []string
for result.Next() {
value, found := result.Record().Get("recommendation")
if found {
arr = append(arr, value.(string))
}
}
if err = result.Err(); err != nil {
return nil, err
}
return arr, nil
})
if err != nil {
return nil, err
recommendations[i] = name
}
result = results.([]string)
return result, err
return recommendations, nil
}

func handleClose(closer io.Closer, previousError error) error {
err := closer.Close()
func handleClose(ctx context.Context, closer interface{ Close(context.Context) error }, previousError error) error {
err := closer.Close(ctx)
if err == nil {
return previousError
}
Expand Down
24 changes: 12 additions & 12 deletions code/graphql/example.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const driver = neo4j.driver(
);

const typeDefs = /* GraphQL */ `
type Movie {
type Movie @exclude(operations: [CREATE, UPDATE, DELETE]) {
budget: Int
countries: [String]
imdbId: ID
Expand All @@ -25,9 +25,9 @@ type Movie {
tmdbId: String
url: String
year: Int
genres: [Genre] @relationship(type: "IN_GENRE", direction: OUT)
actors: [Actor] @relationship(type: "ACTED_IN", direction: IN)
directors: [Director] @relationship(type: "DIRECTED", direction: IN)
genres: [Genre!]! @relationship(type: "IN_GENRE", direction: OUT)
actors: [Actor!]! @relationship(type: "ACTED_IN", direction: IN)
directors: [Director!]! @relationship(type: "DIRECTED", direction: IN)
similar(first: Int = 3): [Movie]
@cypher(
statement: """
Expand All @@ -38,18 +38,18 @@ type Movie {
)
}

type Genre {
type Genre @exclude(operations: [CREATE, UPDATE, DELETE]) {
name: String
movies: [Movie] @relationship(type: "IN_GENRE", direction: IN)
movies: [Movie!]! @relationship(type: "IN_GENRE", direction: IN)
}

type User {
type User @exclude(operations: [CREATE, UPDATE, DELETE]) {
userId: ID!
name: String
rated: [Movie] @relationship(type: "RATED", direction: OUT)
rated: [Movie!]! @relationship(type: "RATED", direction: OUT)
}

type Actor {
type Actor @exclude(operations: [CREATE, UPDATE, DELETE]) {
bio: String
born: Date
bornIn: String
Expand All @@ -58,18 +58,18 @@ type Actor {
poster: String
tmdbId: String
url: String
acted_in: [Movie] @relationship(type: "ACTED_IN", direction: OUT)
acted_in: [Movie!]! @relationship(type: "ACTED_IN", direction: OUT)
}

type Director {
type Director @exclude(operations: [CREATE, UPDATE, DELETE]) {
bio: String
bornIn: String
imdbIb: String
name: String
poster: String
tmdbId: String
url: String
directed: [Movie] @relationship(type: "DIRECTED", direction: OUT)
directed: [Movie!]! @relationship(type: "DIRECTED", direction: OUT)
}

`;
Expand Down
2 changes: 1 addition & 1 deletion code/javascript/example.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
const neo4j = require('neo4j-driver');
const driver = neo4j.driver('neo4j://<HOST>:<BOLTPORT>',
neo4j.auth.basic('<USERNAME>', '<PASSWORD>'),
{/* encrypted: 'ENCRYPTION_OFF' */});
{});

const query =
`
Expand Down
24 changes: 11 additions & 13 deletions code/python/example.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
# pip3 install neo4j-driver
# pip3 install neo4j
# python3 example.py

from neo4j import GraphDatabase, basic_auth

driver = GraphDatabase.driver(
"neo4j://<HOST>:<BOLTPORT>",
auth=basic_auth("<USERNAME>", "<PASSWORD>"))

cypher_query = '''
MATCH (m:Movie {title:$movie})<-[:RATED]-(u:User)-[:RATED]->(rec:Movie)
RETURN distinct rec.title AS recommendation LIMIT 20
'''

with driver.session(database="neo4j") as session:
results = session.read_transaction(
lambda tx: tx.run(cypher_query,
movie="Crimson Tide").data())
for record in results:
print(record['recommendation'])

driver.close()
with GraphDatabase.driver(
"neo4j://<HOST>:<BOLTPORT>",
auth=("<USERNAME>", "<PASSWORD>")
) as driver:
result = driver.execute_query(
cypher_query,
movie="Crimson Tide",
database_="neo4j")
for record in result.records:
print(record['recommendation'])