Fullstack Graph
w/ Neo4j
William Lyon
@lyonwj
lyonwj.com
bit.ly/seattlegraph Graph Tour Seattle
William Lyon
Software Developer @Neo4j
● Neo4j Labs
■ Integrations
■ GraphQL
■ Data Visualization
■ ...
@lyonwj
lyonwj.com
will@neo4j.com
Pig E. Bank
https://neo4j.com/graphacademy/online-training/
Labeled Property Graph
Cypher Query Language
https://neo4jsandbox.com
neo4j.com/developer/get-started/
r.neo4j.com/youtube
medium.com/neo4j
community.neo4j.com
neo4j.com/tag/twin4j
neo4j.com/graphacademy/neo4j-certification/
●
●
●
●
●
●
●
●
○
○
○
John
Table
Mary
von
Groff
Cookie
Macy
Stream
SSN:
X-Y-Z-A
Phone:
555-1234
1.
→
2.
3.
4.
5.
Arrows for data model diagrams:
www.apcjones.com/arrows/
youtube.com/watch?v=SSjlUoFQdos
●
●
●
neo4j.com/labs/
Extending Neo4j
•
•
•
About APOC
• Large standard library of utility
functions and procedures
• Actively developed - many contributors
• "scratch your itch"
• Makes Cypher easier to use
• Enable some specific use-cases
• Plan is it migrate some of the functionality
into the Neo4j product
Browser Guide
:play apoc
• Explore live examples
• Relational / Cassandra
• MongoDB, Couchbase, ElasticSearch
• JSON, XML, CSV, XLS
• Cypher, GraphML
• …
Data integration
Load from a relational database
apoc.load.jdbc
WITH "jdbc:mysql://localhost:3306/customers?user=root" AS url
CALL apoc.load.jdbc(url,"parties")
YIELD row
MERGE (p:Party {id: row.PartyID})
SET p.name = row.name, p.ssn = row.SSN
1)
2)
3)
4)
neo4j.com/labs/etl-tool/
{event} {event}
•
•
•
neo4j.com/labs/kafka/
{event} {event}
Graph Algorithms in Neo4j
• Parallel Breadth First Search &
DFS
• Shortest Path
• Single-Source Shortest Path
• All Pairs Shortest Path
• Minimum Spanning Tree
• A* Shortest Path
• Yen’s K Shortest Path
• K-Spanning Tree (MST)
• Random Walk
• Degree Centrality
• Closeness Centrality
• CC Variations: Harmonic, Dangalchev,
Wasserman & Faust
• Betweenness Centrality
• Approximate Betweenness Centrality
• PageRank
• Personalized PageRank
• ArticleRank
• Eigenvector Centrality
• Triangle Count
• Clustering Coefficients
• Connected Components (Union Find)
• Strongly Connected Components
• Label Propagation
• Louvain Modularity – 1 Step &
Multi-Step
• Balanced Triad (identification)
• Euclidean Distance
• Cosine Similarity
• Jaccard Similarity
• Overlap Similarity
• Pearson Similarity
Pathfinding
& Search
Centrality /
Importance
Community
Detection
Similarity
Updated June 2019
Link
Prediction
• Adamic Adar
• Common Neighbors
• Preferential Attachment
• Resource Allocations
• Same Community
• Total Neighbors
+35
● bolt+routing://
●
●
●
neo4j.com/developer/language-guides/
● Fullstack framework for building applications
Fullstack GraphQL with GRANDstack
grandstack.io
@lyonwj
What is GraphQL?
graphql.org
An API query language and runtime for building APIs
@lyonwj
“Your Application Data Is A Graph”
-- GraphQL
@lyonwj
Movies, Genres,
Directors, Actors
@lyonwj
Movies, Genres, Directors, Actors
GraphQL Type Definitions
● Defined using GraphQL
Schema Definition
Language (SDL)
@lyonwj
Movies, Genres,
Directors, Actors
Introspection
● Schema can be queried
● Schema becomes API specification /
documentation
● Tools like GraphiQL / GraphQL
Playground
@lyonwj
Movies, Genres,
Directors, Actors
GraphQL query
@lyonwj
https://blog.apollographql.com/the-anatomy-of-a-graphql-query-6dffa9e9e747
Operation name and
arguments
@lyonwj
Selection set
@lyonwj https://blog.apollographql.com/the-anatomy-of-a-graphql-query-6dffa9e9e747
GraphQL query
Movies, Genres,
Directors, Actors
@lyonwj
GraphQL query
GraphQL response
Movies, Genres,
Directors, Actors
@lyonwj
1) GraphQL is an API query language, not a database query
language.
2) Limited expressivity (no projections, no aggregations, etc).
3) While GraphQL exposes your application data as a graph, it’s
not (just) for graph databases
What is GraphQL?
@lyonwj
● Overfetching
○ Less data over the wire
● Underfetching
○ Single round trip
● GraphQL Specification
● “Graphs All The Way Down”
○ Relationships vs Resources
○ Unify disparate systems (microservices)
● Simplify data fetching
○ Component based data interactions
GraphQL Advantages
@lyonwj
● Some well understood practices from
REST don’t apply
○ HTTP status codes
○ Errors
○ Caching
● Exposing arbitrary complexity to client
○ Performance considerations
● n+1 query problem
● Query costing / rate limiting
GraphQL Challenges
@lyonwj
Example API & How It’s
Built
Example GraphQL API
Neo4j Community Forum Trending Activity Feeds
community.neo4j.com
@lyonwj
communityapi.neo4jlabs.com@lyonwj
How To Build A GraphQL
Service
hackernoon.com/building-trending-activity-feeds-using-graphql-and-neo4j-e62ee790238e@lyonwj
Community Graph
http://138.197.15.1:7474/browser/
user: community
password: community@lyonwj
@lyonwj
Start With A GraphQL Schema
graphql.org/learn/schema/#type-language
@lyonwj
Start With A GraphQL Schema
Type
Definitions
@lyonwj
Start With A GraphQL Schema
API Entry Point(s)
Query & Mutation Types
@lyonwj
Start With A GraphQL Schema
API Entry Point(s)
Query & Mutation Types
graphql.org/learn/schema/#type-language
@lyonwj
● Functions that define how to “resolve” data for GraphQL request
GraphQL Resolvers
graphql.org/learn/execution/#root-fields-resolvers@lyonwj
Cypher Query
https://hackernoon.com/building-trending-activity-feeds-using-graphql-and-neo4j-e62ee790238e
@lyonwj
Resolvers
@lyonwj
Apollo Server
https://www.apollographql.com/docs/apollo-server/@lyonwj
Apollo Server
@lyonwj
Apollo Server
@lyonwj
Apollo Server
@lyonwj
Apollo Server
@lyonwj
communityapi.neo4jlabs.com@lyonwj
1) Schema Duplication
2) Mapping / translation layer from graph ←→ (???)
3) Boilerplate code
4) n+1 query problem
Common Problems With This Approach
https://blog.grandstack.io/five-common-graphql-problems-and-how-neo4j-graphql-aims-to-solve-them-e9a8999c8d43
@lyonwj
GraphQL “Engines”
● Tools for auto-generating GraphQL schema, generating
database queries from GraphQL requests
GraphQL “Engines” Overview
prisma.io aws.amazon.com/appsync hasura.io graphile.org grandstack.io
Neo4j-GraphQL
@lyonwj
Neo4j-GraphQL
● GraphQL First Development
○ GraphQL schema drives the database data model
● Generate Cypher from GraphQL
○ Single query / single round trip to database
● Generate GraphQL CRUD API from type definitions
● Auto-generated resolvers (no boilerplate!)
● Extend GraphQL functionality with Cypher
○ @cypher schema directive
Goals for Neo4j-GraphQL Integration
@lyonwj
GraphQL First Development
@lyonwj
Auto-generate GraphQL CRUD API
@lyonwj
Query & Mutation types
○ Entry point for each type
Ordering
Pagination
Complex Filter
DateTime types
https://blog.grandstack.io/complex-graphql-filtering-with-neo4j-graphql-js-aef19ad06c3e
Generate Cypher From GraphQL
@lyonwj
Extend GraphQL w/ Cypher
grandstack.io/docs/neo4j-graphql-js.html#cypher-directive
@cypher GraphQL schema directive
@lyonwj
www.npmjs.com/package/neo4j-graphql-js@lyonwj
neo4j-graphql-js
neo4j-graphql-js
apollo-server
@lyonwj
Demo
GRANDstack starter
project
grandstack.io
https://github.com/grand-stack/grand-stack-starter/tree/master/api/src@lyonwj
GRANDstack.io
@lyonwj
● Declarative database integrations for
GraphQL
● GraphQL type definitions define
database model
● Provision CRUD GraphQL API
○ Auto-generated GraphQL API
○ Schema enrichments
● Generate database queries
○ auto-generated resolvers
○ reduce boilerplate
GraphQL ”Engines”
@lyonwj
How Do GraphQL Engines Generate
Database Queries From GraphQL Requests?
resolveInfo resolver
argument
● GraphQL query AST
● GraphQL schema
● Selection set
● Variables
● ...
@lyonwj
Deploying A GraphQL
Service
GraphQL Deployment
“Traditional” Approaches Serverless Options
● PaaS
○ Heroku, AWS Elastic
Beanstalk
● Docker container
● VPS
● ...
● AWS Lambda, Google Cloud
Functions, Azure
● Serverless Framework
○ serverless.com
● Zeit Now
○ zeit.co/now
● Netlify Functions
○ netlify.com
@lyonwj
neo4j-graphql-js
apollo-server
Neo4j Cloud
AWS Lambda
GCP Functions
Static CDN
GraphQL Deployment
@lyonwj
https://blog.apollographql.com/deploy-a-fullstack-apollo-app-with-netlify-45a7dfd51b0b
@lyonwj
grandstack.io
GRANDstack
https://github.com/grand-stack/grandstack.io/issues/1
@lyonwj
GraphQL Clients
Apollo Client Alternatives
● Most popular
● Frontend integrations for:
○ React, Angular, vue.js,
scala.js,iOS, Android,
etc
● Relay
● urql
● graphql-request
● GraphiQL
● GraphQL Playground
● fetch
● <any http client>
@lyonwj
Apollo Client (for React)
@lyonwj
Apollo Client (for React)
@lyonwj
Apollo Client (for React)
@lyonwj
Query GraphQL endpoint
Render our table
@lyonwj
<Query> component
@lyonwj
Handle GraphQL response
and render table
@lyonwj
@lyonwj
●
●
●
●
●
●
○
○
○
●
●
Fraud
Flagger
Party Interaction Stream
Fraud Suspect Stream
Fraud Confirmation Stream
neo4j-streams
Investigation
Account Action Improved Insight
1.
a.
2.
3.
Graph Algorithm Categories in Neo4j
neo4j.com/
graph-algorithms-
book/
Pathfinding
& Search
Centrality /
Importance
Community
Detection
Link
Prediction
Finds optimal paths
or evaluates route
availability and quality
Determines the
importance of distinct
nodes in the network
Detects group
clustering or partition
options
Evaluates how
alike nodes are
Estimates the likelihood
of nodes forming a
future relationship
Similarity
Graph Algorithms in Neo4j
• Parallel Breadth First Search &
DFS
• Shortest Path
• Single-Source Shortest Path
• All Pairs Shortest Path
• Minimum Spanning Tree
• A* Shortest Path
• Yen’s K Shortest Path
• K-Spanning Tree (MST)
• Random Walk
• Degree Centrality
• Closeness Centrality
• CC Variations: Harmonic, Dangalchev,
Wasserman & Faust
• Betweenness Centrality
• Approximate Betweenness Centrality
• PageRank
• Personalized PageRank
• ArticleRank
• Eigenvector Centrality
• Triangle Count
• Clustering Coefficients
• Connected Components (Union Find)
• Strongly Connected Components
• Label Propagation
• Louvain Modularity – 1 Step &
Multi-Step
• Balanced Triad (identification)
• Euclidean Distance
• Cosine Similarity
• Jaccard Similarity
• Overlap Similarity
• Pearson Similarity
Pathfinding
& Search
Centrality /
Importance
Community
Detection
Similarity
Updated June 2019
Link
Prediction
• Adamic Adar
• Common Neighbors
• Preferential Attachment
• Resource Allocations
• Same Community
• Total Neighbors
+35
●
●
○
○
●
○
○
○
○
●
○
○
■
○
●
●
●
●
●
●
●
●
●
○ https://github.com/neo4j-contrib/neo4j-streams
○ https://www.confluent.io/hub/neo4j/kafka-connect-neo4j
●
https://grandstack.io/
●
https://www.freecodecamp.org/news/how-to-leverage-neo4j-streams-and-build-a-just-in-time-data-warehouse-64adf290f093/
● https://neo4j.com/docs/graph-algorithms/current/
Building Fullstack Graph Applications With Neo4j
Building Fullstack Graph Applications With Neo4j
Building Fullstack Graph Applications With Neo4j
Building Fullstack Graph Applications With Neo4j
Building Fullstack Graph Applications With Neo4j
Building Fullstack Graph Applications With Neo4j

Building Fullstack Graph Applications With Neo4j