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 #23

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
2 changes: 1 addition & 1 deletion code/csharp/Example.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
namespace dotnet {
class Example {
static async Task Main() {
var driver = GraphDatabase.Driver("bolt://<HOST>:<BOLTPORT>",
var driver = GraphDatabase.Driver("neo4j://<HOST>:<BOLTPORT>",
AuthTokens.Basic("<USERNAME>", "<PASSWORD>"));

var cypherQuery =
Expand Down
59 changes: 25 additions & 34 deletions code/go/example.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
package main

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

func main() {
results, err := runQuery("bolt://<HOST>:<BOLTPORT>", "neo4j", "<USERNAME>", "<PASSWORD>")
results, err := runQuery("neo4j://<HOST>:<BOLTPORT>", "neo4j", "<USERNAME>", "<PASSWORD>")
if err != nil {
panic(err)
}
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 (u:User {screen_name: $screenName})<-[r:MENTIONS]-(t:Tweet)-[r2:TAGS]->(h:Hashtag)
RETURN h.name as hashtag
`, map[string]interface{}{
"screenName": "NASA",
})
defer func() { err = handleClose(ctx, driver, err) }()
query := " MATCH (u:User {screen_name: $screenName})<-[r:MENTIONS]-(t:Tweet)-[r2:TAGS]->(h:Hashtag)
RETURN h.name as hashtag
params := map[string]any{"screenName": "NASA"}
result, err := neo4j.ExecuteQuery(ctx, driver, query, params,
neo4j.EagerResultTransformer,
neo4j.ExecuteQueryWithDatabase(database),
neo4j.ExecuteQueryWithReadersRouting())
if err != nil {
return nil, err
}
hashtags := 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, "hashtag")
if err != nil {
return nil, err
}
var arr []string
for result.Next() {
value, found := result.Record().Get("hashtag")
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
hashtags[i] = name
}
result = results.([]string)
return result, err
return hashtags, 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
267 changes: 195 additions & 72 deletions code/graphql/example.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,87 +8,210 @@ const driver = neo4j.driver(
);

const typeDefs = /* GraphQL */ `
type Tweet {
created_at: DateTime
favorites: Int
id: ID!
id_str: String
import_method: String
text: String
using: [Source] @relationship(type: "USING", direction: OUT)
tags: [Hashtag] @relationship(type: "TAGS", direction: OUT)
retweets: [Tweet] @relationship(type: "RETWEETS", direction: OUT)
reply_to: [Tweet] @relationship(type: "REPLY_TO", direction: OUT)
contains: [Link] @relationship(type: "CONTAINS", direction: OUT)
posted_by: User @relationship(type: "POSTS", direction: IN)
}

type Me {
followers: Int!
following: Int!
location: String!
name: String!
profile_image_url: String!
screen_name: ID!
posts: [Tweet] @relationship(type: "POSTS", direction: OUT)
users: [User] @relationship(type: "FOLLOWS", direction: IN)
tweets: [Tweet] @relationship(type: "MENTIONS", direction: IN)
}

type Hashtag {
name: String!
tweets: [Tweet] @relationship(type: "TAGS", direction: IN)
num_tweets: Int
@cypher(statement: "RETURN SIZE( (this)<-[:TAGS]-(:Tweet) )")
}

type Link {
url: String!
tweets: [Tweet] @relationship(type: "CONTAINS", direction: IN)
}

type Source {
name: String!
tweets: [Tweet] @relationship(type: "USING", direction: IN)
}

type User {
followers: Int
following: Int
location: String
name: String!
profile_image_url: String
screen_name: String!
statuses: Int
url: String
posts: [Tweet] @relationship(type: "POSTS", direction: OUT)
tweets: [Tweet] @relationship(type: "MENTIONS", direction: IN)
}

type UserCount {
type Tweet {
created_at: DateTime
favorites: Int
id: ID!
id_str: String
import_method: String
text: String
using: [Source] @relationship(type: "USING", direction: OUT)
tags: [Hashtag] @relationship(type: "TAGS", direction: OUT)
retweets: [Tweet] @relationship(type: "RETWEETS", direction: OUT)
reply_to: [Tweet] @relationship(type: "REPLY_TO", direction: OUT)
contains: [Link] @relationship(type: "CONTAINS", direction: OUT)
posted_by: User @relationship(type: "POSTS", direction: IN)
}

type Me {
followers: Int!
following: Int!
location: String!
name: String!
profile_image_url: String!
screen_name: ID!
posts: [Tweet] @relationship(type: "POSTS", direction: OUT)
users: [User] @relationship(type: "FOLLOWS", direction: IN)
tweets: [Tweet] @relationship(type: "MENTIONS", direction: IN)
}

type Hashtag {
name: String!
tweets: [Tweet] @relationship(type: "TAGS", direction: IN)
num_tweets: Int @cypher(statement: "RETURN SIZE( (this)<-[:TAGS]-(:Tweet) )")
}

type Link {
url: String!
tweets: [Tweet] @relationship(type: "CONTAINS", direction: IN)
}

type Source {
name: String!
tweets: [Tweet] @relationship(type: "USING", direction: IN)
}

type User {
followers: Int
following: Int
location: String
name: String!
profile_image_url: String
screen_name: String!
statuses: Int
url: String
posts: [Tweet] @relationship(type: "POSTS", direction: OUT)
tweets: [Tweet] @relationship(type: "MENTIONS", direction: IN)
}

type UserCount {
count: Int
user: User
}

type HashtagCount {
count: Int
user: User
}
name: String
}

extend type User {
topMentions: UserCount
type UserTweet {
screen_name: String
name: String
profile_pic: String
created_at: DateTime
text: String
}

extend type Me {
topMentions(first: Int = 5): [UserCount]
@cypher(
statement: """
MATCH (this)-[:POSTS]->(t:Tweet)-[:MENTIONS]->(m:User)
WITH m, COUNT(m.screen_name) AS count
ORDER BY count DESC
LIMIT $first
RETURN {
user: m { .* },
count: count
}
"""
)
topHashtags(first: Int = 5): [HashtagCount]
@cypher(
statement: """
MATCH (this:Me)-[:POSTS]->(t:Tweet)-[:TAGS]->(h:Hashtag)
WITH h, COUNT(h) AS count
ORDER BY count DESC
LIMIT $first
RETURN {
name: h.name,
count: count
}
"""
)
followbackCount: Int
@cypher(
statement: """
MATCH (me:Me)-[:FOLLOWS]->(f:User)
WHERE (f)-[:FOLLOWS]->(me)
RETURN count(f)
"""
)
recommended(first: Int = 10): [UserCount]
@cypher(
statement: """
MATCH (u:User)-[:POSTS]->(t:Tweet)-[:MENTIONS]->(me:Me)
WITH DISTINCT u, me, count(t) as count
WHERE (u)-[:FOLLOWS]->(me)
AND NOT (me)-[:FOLLOWS]->(u)
RETURN {
user: u { .* },
count: count
}
ORDER BY count DESC
LIMIT $first
"""
)
priorityFeed: [UserTweet]
@cypher(
statement: """
MATCH (t:Tweet) WHERE t.created_at IS NOT NULL
WITH max(t.created_at) - duration('P3D') as recentDate
CALL {
WITH recentDate
MATCH (this:Me)-[r:SIMILAR_TO]-(u:User)
WITH u, recentDate
ORDER BY r.score DESC
LIMIT 10
MATCH (u)-[:POSTS]->(t:Tweet)
WHERE t.created_at >= recentDate
WITH u, t
ORDER BY t.created_at DESC
LIMIT 50
RETURN {
screen_name: u.screen_name,
name: u.name,
profile_pic: u.profile_image_url,
created_at: t.created_at,
text: t.text
} as tweets
UNION
WITH recentDate
MATCH (u:User)-[r:POSTS]->(t:Tweet)
WHERE t.created_at >= recentDate AND NOT u:Me
WITH u, t
ORDER BY t.created_at DESC
LIMIT 50
RETURN {
screen_name: u.screen_name,
name: u.name,
profile_pic: u.profile_image_url,
created_at: t.created_at,
text: t.text
} as tweets
ORDER BY t.created_at DESC
LIMIT 50
}
RETURN tweets
"""
)
}

extend type User {
topMentions: UserCount
@cypher(
statement: """
MATCH (this)-[:POSTS]->(t:Tweet)-[:MENTIONS]->(m:User)
WITH m, COUNT(m.screen_name) AS count
ORDER BY count DESC
LIMIT 1
RETURN {
user: m {.*},
count: count
}
"""
)
}

extend type Tweet {
mentions: [User] @relationship(type: "MENTIONS", direction: OUT)
}

extend type Hashtag {
trendingTags(first: Int = 5): [HashtagCount]
@cypher(
statement: """
MATCH (this)-[:POSTS]->(t:Tweet)-[:MENTIONS]->(m:User)
WITH m, COUNT(m.screen_name) AS count
MATCH (t:Tweet)-[:TAGS]->(h:Hashtag)
WITH h, count(h) as count
ORDER BY count DESC
LIMIT 1
LIMIT $first
RETURN {
user: m {.*},
count: count
name: h.name,
count: count
}
"""
)
}

extend type Tweet {
mentions: [User] @relationship(type: "MENTIONS", direction: OUT)
}
}
`;

// Create executable GraphQL schema from GraphQL type definitions,
Expand Down
2 changes: 1 addition & 1 deletion code/java/Example.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class Example {

public static void main(String...args) {

Driver driver = GraphDatabase.driver("bolt://<HOST>:<BOLTPORT>",
Driver driver = GraphDatabase.driver("neo4j://<HOST>:<BOLTPORT>",
AuthTokens.basic("<USERNAME>","<PASSWORD>"));

try (Session session = driver.session(SessionConfig.forDatabase("neo4j"))) {
Expand Down
4 changes: 2 additions & 2 deletions code/javascript/example.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
// npm install --save neo4j-driver
// node example.js
const neo4j = require('neo4j-driver');
const driver = neo4j.driver('bolt://<HOST>:<BOLTPORT>',
const driver = neo4j.driver('neo4j://<HOST>:<BOLTPORT>',
neo4j.auth.basic('<USERNAME>', '<PASSWORD>'),
{/* encrypted: 'ENCRYPTION_OFF' */});
{});

const query =
`
Expand Down
Loading