Ce diaporama a bien été signalé.
Nous utilisons votre profil LinkedIn et vos données d’activité pour vous proposer des publicités personnalisées et pertinentes. Vous pouvez changer vos préférences de publicités à tout moment.

Building Community APIs using GraphQL, Neo4j, and Kotlin

1 323 vues

Publié le

Neo4j is a high performance, open-source graph database written in Java and Scala.

But of course, you can use and extend it from other JVM Languages, like Kotlin.
I've been using Kotlin on and off since 2012, that's when I wrote my first article about this pragmatic and clean programming language.

Today I want to demonstrate in 4 examples how you can use Neo4j with Kotlin.
In preparation for KotlinConf 2017 we started to gather community activity of the Kotlin community in a Neo4j graph.
You can see tweets, GitHub projects, StackOverflow questions and answers and meetup events in this database of developer activity.

After showing some examples we will explore how to query that database using a Kotlin driver for Neo4j.
Then we'll look at the Kotlin + Spring (Data) demo app, that shows community members who get a lot of praise on a Twitter wall.

My main Kotlin project over the last year has been the GraphQL extension for Neo4j.
So we'll have a look at how we can extend Neo4j with a custom HTTP API and how we integrate the GraphQL-Java library using Kotlin.
This extension manages a GraphQL schema to translate GraphQL queries to Neo4js native query language Cypher.

Another way to extend Neo4j is with user-defined procedures and functions which (with a small trick) are really easy to implement with Kotlin too.
We'll take a look at the procedures and functions we expose as part of the neo4j-graphql extension.

Publié dans : Logiciels
  • Soyez le premier à commenter

Building Community APIs using GraphQL, Neo4j, and Kotlin

  1. 1. Building Community APIs using GraphQL, Neo4j, and Kotlin Michael Hunger Kotlin Meetup Jan 2018
  2. 2. (Michael Hunger)-[:WORKS_FOR]->(Neo4j) michael@neo4j.com | @mesirii | github.com/jexp | jexp.de/blog Michael Hunger - Developer Relations Engineering @Neo4j
  3. 3. My History With
  4. 4. Graphs & Neo4j Community Graph Kotlin App Dev GraphQL Extending Neo4j
  5. 5. Why Graphs? Because the World is a Graph!
  6. 6. Unraveling the Cancer Code with Graphs
  7. 7. Real-Time Recommendation s Fraud Detection Network & IT Operations Master Data Management Graph-Based Search Identity & Access Management Using Neo4j Graphs
  8. 8. Machine Learning is Based on Graphs
  9. 9. Neo4j Open Source Database for Highly Connected Data
  10. 10. The Whiteboard Model Is the Physical Model Eliminates Graph-to- Relational Mapping In your data model Bridge the gap between business and IT models In your application Greatly reduce need for application code
  11. 11. 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 WITH PERSON PERSON
  12. 12. Relational Versus Graph Models Relational Model Graph Model KNOWS ANDREAS TOBIAS MICA DELIA Person FriendPerson-Friend ANDREAS DELIA TOBIAS MICA
  13. 13. Cypher: Powerful and Expressive Query Language MATCH (:Person { name:“Dan”} ) -[:LOVES]-> (:Person { name:“Ann”} ) LOVES Dan Ann LABEL PROPERTY NODE NODE LABEL PROPERTY
  14. 14. 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 Python Community Drivers Drivers Bolt
  15. 15. 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
  16. 16. Kotlin Wrapper – Connect & Insert https://github.com/erictsangx/kotlin-neo4j
  17. 17. Kotlin Wrapper – Query & Map https://github.com/erictsangx/kotlin-neo4j
  18. 18. Object Graph Mapping Spring Data Neo4j Neo4j - OGM
  19. 19. 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
  20. 20. Movie.kt
  21. 21. MovieRepository.kt
  22. 22. Community Graph Making Sense of Developer Activity github.com/community-graph
  23. 23. Developer Channels
  24. 24. User Event Message Tweet Location Project Topic Tag Question Answer Topic Tag There is much more to it
  25. 25. 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
  26. 26. Community Graphs • Neo4j • GraphQL • Kotlin • Angular • ... more planned ...
  27. 27. Demo Kotlin Community Graph
  28. 28. Community Radar Kotlin Conf 2017 Twitter Appreciation Wall App github.com/community-graph/community-radar
  29. 29. Community Radar • Kotlin Backend – finds people that have been mentioned a lot or tagged with a hashtag – and recent tweets • Angular Front-End • Statistics Service with React UI (WIP)
  30. 30. Stats Repository
  31. 31. Neo4j-GraphQL Neo4j Extension in Kotlin neo4j.com/developer/graphql & grandstack.io
  32. 32. 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
  33. 33. GraphQL query { Movie(title:"The Matrix") { title released tagline actors { name born } } } type Movie { title: String! released: Int tagline: String actors: [Person] } type Person { name: String! born: Int movies: [Movie] }
  34. 34. 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
  35. 35. Code!
  36. 36. 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
  37. 37. 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 = {...}
  38. 38. Extending Neo4j User Defined Procedures
  39. 39. Any JVM Language will do • Nullable Inputs / Outputs • Some Annotations needed (@JvmField) • Example Project – github.com/mfalcier/neo4j-kotlin-procedure-example
  40. 40. Example
  41. 41. Other Projects Looking for more pointers
  42. 42. 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
  43. 43. Thank you! Questions ? neo4j.com/slack @neo4j

×