Building Community APIs using GraphQL, Neo4j, and Kotlin

51
Building Community APIs using GraphQL, Neo4j, and Kotlin Michael Hunger Kotlin Meetup Jan 2018

Transcript of Building Community APIs using GraphQL, Neo4j, and Kotlin

Building Community APIs usingGraphQL, Neo4j, and Kotlin

Michael Hunger

Kotlin MeetupJan 2018

(Michael Hunger)-[:WORKS_FOR]->(Neo4j)

[email protected] | @mesirii | github.com/jexp | jexp.de/blog

Michael Hunger - Developer Relations Engineering @Neo4j

My History With

Graphs &

Neo4j

Community Graph

KotlinAppDev

GraphQL

ExtendingNeo4j

Why Graphs?

Because the World is a Graph!

Unraveling the Cancer Code with

Graphs

Real-Time Recommendation

s

Fraud Detection

Network &IT

Operations

Master Data Management

Graph-Based Search

Identity & Access Management

Using Neo4j Graphs

Machine Learning is Based on Graphs

Neo4j

Open Source Database for HighlyConnected Data

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

Kotlin Wrapper – Connect & Insert

https://github.com/erictsangx/kotlin-neo4j

Kotlin Wrapper – Query & Map

https://github.com/erictsangx/kotlin-neo4j

Object Graph Mapping

Spring Data Neo4jNeo4j - OGM

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

Movie.kt

MovieRepository.kt

Community Graph

Making Sense of Developer Activity

github.com/community-graph

Developer Channels

User

Event

MessageTweet

LocationProject

TopicTag

QuestionAnswer

TopicTag

There is much more to it

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 Graphs

• Neo4j

• GraphQL

• Kotlin

• Angular

• ... more planned ...

Demo

Kotlin Community Graph

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)

Stats Repository

Neo4j-GraphQL

Neo4j Extension in Kotlin

neo4j.com/developer/graphql & grandstack.io

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

Code!

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 = {...}

Extending Neo4j

User Defined Procedures

Any JVM Language will do

• Nullable Inputs / Outputs

• Some Annotations needed (@JvmField)

• Example Project– github.com/mfalcier/neo4j-kotlin-procedure-example

Example

Other Projects

Looking for morepointers

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

Thank you!Questions ?

neo4j.com/slack@neo4j