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

Building Community APIs using GraphQL, Neo4j, and Kotlin

Jan 21, 2018

Download

Software

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Building Community APIs using GraphQL, Neo4j, and Kotlin

Building Community APIs usingGraphQL, Neo4j, and Kotlin

Michael Hunger

Kotlin MeetupJan 2018

Page 2: Building Community APIs using GraphQL, Neo4j, and Kotlin
Page 3: 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

Page 4: Building Community APIs using GraphQL, Neo4j, and Kotlin

My History With

Page 5: Building Community APIs using GraphQL, Neo4j, and Kotlin

Graphs &

Neo4j

Community Graph

KotlinAppDev

GraphQL

ExtendingNeo4j

Page 6: Building Community APIs using GraphQL, Neo4j, and Kotlin

Why Graphs?

Because the World is a Graph!

Page 7: Building Community APIs using GraphQL, Neo4j, and Kotlin
Page 8: Building Community APIs using GraphQL, Neo4j, and Kotlin
Page 9: Building Community APIs using GraphQL, Neo4j, and Kotlin

Unraveling the Cancer Code with

Graphs

Page 10: Building Community APIs using GraphQL, Neo4j, and Kotlin
Page 11: Building Community APIs using GraphQL, Neo4j, and Kotlin

Real-Time Recommendation

s

Fraud Detection

Network &IT

Operations

Master Data Management

Graph-Based Search

Identity & Access Management

Using Neo4j Graphs

Page 12: Building Community APIs using GraphQL, Neo4j, and Kotlin

Machine Learning is Based on Graphs

Page 13: Building Community APIs using GraphQL, Neo4j, and Kotlin

Neo4j

Open Source Database for HighlyConnected Data

Page 14: Building Community APIs using GraphQL, Neo4j, and Kotlin

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

Page 15: Building Community APIs using GraphQL, Neo4j, and Kotlin

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

Page 16: Building Community APIs using GraphQL, Neo4j, and Kotlin

Relational Versus Graph Models

Relational Model Graph Model

KNOWSANDREAS

TOBIAS

MICA

DELIA

Person FriendPerson-Friend

ANDREASDELIA

TOBIAS

MICA

Page 17: Building Community APIs using GraphQL, Neo4j, and Kotlin

Cypher: Powerful and Expressive Query Language

MATCH (:Person { name:“Dan”} ) -[:LOVES]-> (:Person { name:“Ann”} )

LOVES

Dan Ann

LABEL PROPERTY

NODE NODE

LABEL PROPERTY

Page 18: Building Community APIs using GraphQL, Neo4j, and Kotlin

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

Page 19: Building Community APIs using GraphQL, Neo4j, and Kotlin

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

Page 20: Building Community APIs using GraphQL, Neo4j, and Kotlin

Kotlin Wrapper – Connect & Insert

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

Page 21: Building Community APIs using GraphQL, Neo4j, and Kotlin

Kotlin Wrapper – Query & Map

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

Page 22: Building Community APIs using GraphQL, Neo4j, and Kotlin

Object Graph Mapping

Spring Data Neo4jNeo4j - OGM

Page 23: Building Community APIs using GraphQL, Neo4j, and Kotlin

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

Page 24: Building Community APIs using GraphQL, Neo4j, and Kotlin

Movie.kt

Page 25: Building Community APIs using GraphQL, Neo4j, and Kotlin

MovieRepository.kt

Page 26: Building Community APIs using GraphQL, Neo4j, and Kotlin

Community Graph

Making Sense of Developer Activity

github.com/community-graph

Page 27: Building Community APIs using GraphQL, Neo4j, and Kotlin

Developer Channels

Page 28: Building Community APIs using GraphQL, Neo4j, and Kotlin

User

Event

MessageTweet

LocationProject

TopicTag

QuestionAnswer

TopicTag

There is much more to it

Page 29: Building Community APIs using GraphQL, Neo4j, and Kotlin

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

Page 30: Building Community APIs using GraphQL, Neo4j, and Kotlin

Community Graphs

• Neo4j

• GraphQL

• Kotlin

• Angular

• ... more planned ...

Page 31: Building Community APIs using GraphQL, Neo4j, and Kotlin

Demo

Kotlin Community Graph

Page 32: Building Community APIs using GraphQL, Neo4j, and Kotlin
Page 33: Building Community APIs using GraphQL, Neo4j, and Kotlin

Community RadarKotlin Conf 2017

Twitter Appreciation Wall App

github.com/community-graph/community-radar

Page 34: Building Community APIs using GraphQL, Neo4j, and Kotlin
Page 35: Building Community APIs using GraphQL, Neo4j, and Kotlin

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)

Page 36: Building Community APIs using GraphQL, Neo4j, and Kotlin

Stats Repository

Page 37: Building Community APIs using GraphQL, Neo4j, and Kotlin

Neo4j-GraphQL

Neo4j Extension in Kotlin

neo4j.com/developer/graphql & grandstack.io

Page 38: Building Community APIs using GraphQL, Neo4j, and Kotlin

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

Page 39: Building Community APIs using GraphQL, Neo4j, and Kotlin

GraphQLquery {

Movie(title:"The Matrix") {titlereleasedtaglineactors {

nameborn

}}

}

type Movie {title: String!released: Inttagline: Stringactors: [Person]

}

type Person {name: String!born: Intmovies: [Movie]

}

Page 40: Building Community APIs using GraphQL, Neo4j, and Kotlin

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

Page 41: Building Community APIs using GraphQL, Neo4j, and Kotlin
Page 42: Building Community APIs using GraphQL, Neo4j, and Kotlin

Code!

Page 43: Building Community APIs using GraphQL, Neo4j, and Kotlin
Page 44: Building Community APIs using GraphQL, Neo4j, and Kotlin

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

Page 45: Building Community APIs using GraphQL, Neo4j, and Kotlin

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

Page 46: Building Community APIs using GraphQL, Neo4j, and Kotlin

Extending Neo4j

User Defined Procedures

Page 47: Building Community APIs using GraphQL, Neo4j, and Kotlin

Any JVM Language will do

• Nullable Inputs / Outputs

• Some Annotations needed (@JvmField)

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

Page 48: Building Community APIs using GraphQL, Neo4j, and Kotlin

Example

Page 49: Building Community APIs using GraphQL, Neo4j, and Kotlin

Other Projects

Looking for morepointers

Page 50: Building Community APIs using GraphQL, Neo4j, and Kotlin

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

Page 51: Building Community APIs using GraphQL, Neo4j, and Kotlin

Thank you!Questions ?

neo4j.com/slack@neo4j