Building Community APIs using GraphQL, Neo4j, and Kotlin
-
Upload
neo4j-the-fastest-and-most-scalable-native-graph-database -
Category
Software
-
view
95 -
download
2
Transcript of Building Community APIs using GraphQL, Neo4j, and Kotlin
(Michael Hunger)-[:WORKS_FOR]->(Neo4j)
[email protected] | @mesirii | github.com/jexp | jexp.de/blog
Michael Hunger - Developer Relations Engineering @Neo4j
Real-Time Recommendation
s
Fraud Detection
Network &IT
Operations
Master Data Management
Graph-Based Search
Identity & Access Management
Using Neo4j Graphs
The Whiteboard Model Is the Physical Model
Eliminates Graph-to-Relational Mapping
In your data modelBridge the gap
between business and IT models
In your applicationGreatly reduce need for application code
CAR
name: “Dan”born: May 29, 1970
twitter: “@dan”name: “Ann”
born: Dec 5, 1975
since: Jan 10, 2011
brand: “Volvo”model: “V70”
Property Graph Model Components
Nodes
• The objects in the graph
• Can have name-value properties
• Can be labeled
Relationships
• Relate nodes by type and direction
• Can have name-value properties
LOVES
LOVES
LIVES WITHPERSON PERSON
Relational Versus Graph Models
Relational Model Graph Model
KNOWSANDREAS
TOBIAS
MICA
DELIA
Person FriendPerson-Friend
ANDREASDELIA
TOBIAS
MICA
Cypher: Powerful and Expressive Query Language
MATCH (:Person { name:“Dan”} ) -[:LOVES]-> (:Person { name:“Ann”} )
LOVES
Dan Ann
LABEL PROPERTY
NODE NODE
LABEL PROPERTY
Official Language Drivers• Foundational drivers for
most programming languages
• Bolt: streaming binary wire protocol
• Authoritative mapping to native type system, uniform across drivers
• Pluggable into richer frameworks
JavaScript Java .NET PythonCommunity
Drivers
Drivers
Bolt
Kotlin Wrapper• Wraps Neo4j Java Driver
• Idiomatic API
• Use Data Classes as– Parameters
– Results
– Destruct data class to Map
• Serializer (DateTime, Enum)
Bolt
https://github.com/erictsangx/kotlin-neo4j
Object Graph Mapping
• Neo4j OGM
• Spring Data Neo4j
– Dedicated support for Kotlin classes in Spring / Spring Data
– Persistence Constructors (PR 405)
– Example Project:
– github.com/neo4j-examples/movies-kotlin-spring-data-neo4j
Approach
• Use Jupyter Notebooks– Read Data from APIs
– Import into Neo4j Graph
• Evolve into AWS script– spin instance up for any community
– based on search tags
• Query for analytics, newsletter, active people
Community RadarKotlin Conf 2017
Twitter Appreciation Wall App
github.com/community-graph/community-radar
Community Radar
• Kotlin Backend
– finds people that have been mentioned a lot ortagged with a hashtag
– and recent tweets
• Angular Front-End
• Statistics Service with React UI (WIP)
GraphQL
• GraphQL is an API query language
• Based on a typesafe schema
• Contract between front & backend
• Flexible queries, nested „shape of data“
• Updates via Mutations
GraphQLquery {
Movie(title:"The Matrix") {titlereleasedtaglineactors {
nameborn
}}
}
type Movie {title: String!released: Inttagline: Stringactors: [Person]
}
type Person {name: String!born: Intmovies: [Movie]
}
Neo4j-GraphQL
• Add support to Neo4j via extension
• Translate GraphQL query to Cypher based on schema
• Execute & transform results
• Integrate graphql-java
• Add custom directives & generated mutations
Lessons Learned• Started in Java switched to Kotlin = < 3• Convince other devs• Interoperability is impressive• Language & Query transformations work well• Extension functions are great for glue code• Updates were painless• I like: ?: / ?. / let / use
Lessons Learned
• Watch out for Nullability in Java libraries/APIs
• Too deep list comprehensions
• Too many „it“
• „?“ operator would be great
• Local return from closures
• Watch out for fun foo(): Any = {...}
Any JVM Language will do
• Nullable Inputs / Outputs
• Some Annotations needed (@JvmField)
• Example Project– github.com/mfalcier/neo4j-kotlin-procedure-example
Other Projects
• Open Data (Healthcare) Import, Queries, Object-Mapping– slideshare.net/neo4j/open-data-with-neo4j-and-kotlin
• Neo4j Cloudfoundry Service Broker
• Extension Functions for Neo4j‘s Java API
– github.com/pisolutions/neo4j-kotlin