From a6d3331f5dac9e9ac2ae9484361251ed90030003 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sandbox=20=F0=9F=A4=96?= Date: Mon, 27 Feb 2023 18:52:02 +0000 Subject: [PATCH] Triggered by direct commit. Origin: https://github.com/neo4j-contrib/sandbox-code-updater/commit/2362543b65f6e3905c6566d321d12d2f9823c89c --- code/csharp/Example.cs | 2 +- code/go/example.go | 2 +- code/graphql/example.js | 267 +++++++++++++++++++++++++++---------- code/java/Example.java | 2 +- code/javascript/example.js | 4 +- code/python/example.py | 4 +- 6 files changed, 202 insertions(+), 79 deletions(-) diff --git a/code/csharp/Example.cs b/code/csharp/Example.cs index 18b0e6d..e091a21 100644 --- a/code/csharp/Example.cs +++ b/code/csharp/Example.cs @@ -13,7 +13,7 @@ namespace dotnet { class Example { static async Task Main() { - var driver = GraphDatabase.Driver("bolt://:", + var driver = GraphDatabase.Driver("neo4j://:", AuthTokens.Basic("", "")); var cypherQuery = diff --git a/code/go/example.go b/code/go/example.go index 16153b6..af43a0b 100644 --- a/code/go/example.go +++ b/code/go/example.go @@ -10,7 +10,7 @@ import ( ) func main() { - results, err := runQuery("bolt://:", "neo4j", "", "") + results, err := runQuery("neo4j://:", "neo4j", "", "") if err != nil { panic(err) } diff --git a/code/graphql/example.js b/code/graphql/example.js index 2dc655e..09040f4 100644 --- a/code/graphql/example.js +++ b/code/graphql/example.js @@ -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, diff --git a/code/java/Example.java b/code/java/Example.java index 8b2b047..39088ca 100644 --- a/code/java/Example.java +++ b/code/java/Example.java @@ -11,7 +11,7 @@ public class Example { public static void main(String...args) { - Driver driver = GraphDatabase.driver("bolt://:", + Driver driver = GraphDatabase.driver("neo4j://:", AuthTokens.basic("","")); try (Session session = driver.session(SessionConfig.forDatabase("neo4j"))) { diff --git a/code/javascript/example.js b/code/javascript/example.js index 531c468..5f6c412 100644 --- a/code/javascript/example.js +++ b/code/javascript/example.js @@ -1,9 +1,9 @@ // npm install --save neo4j-driver // node example.js const neo4j = require('neo4j-driver'); -const driver = neo4j.driver('bolt://:', +const driver = neo4j.driver('neo4j://:', neo4j.auth.basic('', ''), - {/* encrypted: 'ENCRYPTION_OFF' */}); + {}); const query = ` diff --git a/code/python/example.py b/code/python/example.py index f77ad06..31bca0c 100644 --- a/code/python/example.py +++ b/code/python/example.py @@ -1,10 +1,10 @@ -# pip3 install neo4j-driver +# pip3 install neo4j # python3 example.py from neo4j import GraphDatabase, basic_auth driver = GraphDatabase.driver( - "bolt://:", + "neo4j://:", auth=basic_auth("", "")) cypher_query = '''