graphs all the way down
TRANSCRIPT
![Page 1: Graphs All The Way Down](https://reader031.vdocuments.net/reader031/viewer/2022020916/61b41753c1a0ff539350023f/html5/thumbnails/1.jpg)
Graphs All The Way DownBuilding A GraphQL API
Backed By A Graph Database
William Lyon@lyonwj
lyonwj.com
![Page 3: Graphs All The Way Down](https://reader031.vdocuments.net/reader031/viewer/2022020916/61b41753c1a0ff539350023f/html5/thumbnails/3.jpg)
Agenda
• Graph databases - Neo4j• Intro to GraphQL• Why I’m excited about GraphQL + Graph Databases• neo4j-graphql
![Page 4: Graphs All The Way Down](https://reader031.vdocuments.net/reader031/viewer/2022020916/61b41753c1a0ff539350023f/html5/thumbnails/4.jpg)
neo4j.com/developer
![Page 5: Graphs All The Way Down](https://reader031.vdocuments.net/reader031/viewer/2022020916/61b41753c1a0ff539350023f/html5/thumbnails/5.jpg)
Neo4j Graph Database
![Page 6: Graphs All The Way Down](https://reader031.vdocuments.net/reader031/viewer/2022020916/61b41753c1a0ff539350023f/html5/thumbnails/6.jpg)
![Page 7: Graphs All The Way Down](https://reader031.vdocuments.net/reader031/viewer/2022020916/61b41753c1a0ff539350023f/html5/thumbnails/7.jpg)
![Page 8: Graphs All The Way Down](https://reader031.vdocuments.net/reader031/viewer/2022020916/61b41753c1a0ff539350023f/html5/thumbnails/8.jpg)
![Page 10: Graphs All The Way Down](https://reader031.vdocuments.net/reader031/viewer/2022020916/61b41753c1a0ff539350023f/html5/thumbnails/10.jpg)
![Page 11: Graphs All The Way Down](https://reader031.vdocuments.net/reader031/viewer/2022020916/61b41753c1a0ff539350023f/html5/thumbnails/11.jpg)
![Page 12: Graphs All The Way Down](https://reader031.vdocuments.net/reader031/viewer/2022020916/61b41753c1a0ff539350023f/html5/thumbnails/12.jpg)
![Page 13: Graphs All The Way Down](https://reader031.vdocuments.net/reader031/viewer/2022020916/61b41753c1a0ff539350023f/html5/thumbnails/13.jpg)
![Page 15: Graphs All The Way Down](https://reader031.vdocuments.net/reader031/viewer/2022020916/61b41753c1a0ff539350023f/html5/thumbnails/15.jpg)
https://arxiv.org/pdf/1004.1001.pdf
![Page 18: Graphs All The Way Down](https://reader031.vdocuments.net/reader031/viewer/2022020916/61b41753c1a0ff539350023f/html5/thumbnails/18.jpg)
GraphQL
• “A query language for your API”
• Developed by Facebook iOS team for iOS app • Reduce number of round trip requests in face of low latency
• Declarative, state what fields you want • Alternative to REST • Self documenting (schema and types) • Limited support for “queries”
• Logic is implemented in server
![Page 19: Graphs All The Way Down](https://reader031.vdocuments.net/reader031/viewer/2022020916/61b41753c1a0ff539350023f/html5/thumbnails/19.jpg)
GraphQL
• “A query language for your API, and a server-side runtime for executing queries by using a type system you define for your data”
• “GraphQL isn't tied to any specific database or storage engine”
• “A GraphQL service is created by defining types and fields on those types, then providing functions for each field on each type”
http://graphql.org/learn/
![Page 20: Graphs All The Way Down](https://reader031.vdocuments.net/reader031/viewer/2022020916/61b41753c1a0ff539350023f/html5/thumbnails/20.jpg)
GraphQL Adoption
![Page 21: Graphs All The Way Down](https://reader031.vdocuments.net/reader031/viewer/2022020916/61b41753c1a0ff539350023f/html5/thumbnails/21.jpg)
GraphQL
GraphQL Query Result
https://github.com/johnymontana/neo4j-datasets/tree/master/yelp/src/graphql
![Page 22: Graphs All The Way Down](https://reader031.vdocuments.net/reader031/viewer/2022020916/61b41753c1a0ff539350023f/html5/thumbnails/22.jpg)
Ecosystem
• GraphQL Clients • Most popular is Apollo-client • Also Relay (Relay Modern recently released at F8) • Apollo-client also has iOS, Android clients • Lokka (js)
• Frontend frameworks • Dominated by React (Fiber is react rewrite, coming soon
to public) • Vue.js, Angular, Preact
• GraphQL-as-a-service • Graphcool, Scaphold, Reindex
• Language (client) • JavaScript dominates, but Swift (iOS) and Java (Android)
emerging • Language (server)
• JavaScript, Ruby, Java, Scala, Go, Python, … • Server
• Express.js dominates, but many other projects exist
• Tools • Graphiql • Apollo optics • Dataloader
![Page 23: Graphs All The Way Down](https://reader031.vdocuments.net/reader031/viewer/2022020916/61b41753c1a0ff539350023f/html5/thumbnails/23.jpg)
Building A Full Stack Graph Application
![Page 24: Graphs All The Way Down](https://reader031.vdocuments.net/reader031/viewer/2022020916/61b41753c1a0ff539350023f/html5/thumbnails/24.jpg)
GraphQL First Development
1.Design API schema 2.Build UI and backend 3.Run in production
• Schema is your friend • GraphQL Schema is the API spec
• Allows for simultaneous frontend and backend development
• Enables introspection • Build other tools (graphiql)
https://dev-blog.apollodata.com/navigating-your-transition-to-graphql-28a4dfa3acfb
![Page 25: Graphs All The Way Down](https://reader031.vdocuments.net/reader031/viewer/2022020916/61b41753c1a0ff539350023f/html5/thumbnails/25.jpg)
IDL Schema Syntax
Shorthand schema definition
• Language agnostic • Tooling to scaffold server
![Page 26: Graphs All The Way Down](https://reader031.vdocuments.net/reader031/viewer/2022020916/61b41753c1a0ff539350023f/html5/thumbnails/26.jpg)
Demo
![Page 29: Graphs All The Way Down](https://reader031.vdocuments.net/reader031/viewer/2022020916/61b41753c1a0ff539350023f/html5/thumbnails/29.jpg)
![Page 30: Graphs All The Way Down](https://reader031.vdocuments.net/reader031/viewer/2022020916/61b41753c1a0ff539350023f/html5/thumbnails/30.jpg)
Building A GraphQL Server
![Page 31: Graphs All The Way Down](https://reader031.vdocuments.net/reader031/viewer/2022020916/61b41753c1a0ff539350023f/html5/thumbnails/31.jpg)
GraphQL First Development
1.Design API schema 2.Build UI and backend 3.Run in production
• Schema is your friend • GraphQL Schema is the API spec
• Allows for simultaneous frontend and backend development
• Enables introspection • Build other tools (graphiql)
https://dev-blog.apollodata.com/navigating-your-transition-to-graphql-28a4dfa3acfb
![Page 32: Graphs All The Way Down](https://reader031.vdocuments.net/reader031/viewer/2022020916/61b41753c1a0ff539350023f/html5/thumbnails/32.jpg)
Building A GraphQL Server
• Define GraphQL schema• Define resolvers
• Fetch data for a GraphQL field
![Page 33: Graphs All The Way Down](https://reader031.vdocuments.net/reader031/viewer/2022020916/61b41753c1a0ff539350023f/html5/thumbnails/33.jpg)
?
![Page 35: Graphs All The Way Down](https://reader031.vdocuments.net/reader031/viewer/2022020916/61b41753c1a0ff539350023f/html5/thumbnails/35.jpg)
![Page 36: Graphs All The Way Down](https://reader031.vdocuments.net/reader031/viewer/2022020916/61b41753c1a0ff539350023f/html5/thumbnails/36.jpg)
![Page 37: Graphs All The Way Down](https://reader031.vdocuments.net/reader031/viewer/2022020916/61b41753c1a0ff539350023f/html5/thumbnails/37.jpg)
Developer Story
• GraphQL is not a query language for graph databases • Your application data is a graph, GraphQL enables you to define
and query it as such (by extracting trees from the graph) • Implies mapping / translation layer
• GraphQL was designed to be bolted onto an existing data layer (API, database, ORM)
• Write your own database queries, ORM, or API calls yourself for each GraphQL field
• One (or more) database query for each field
https://dev-blog.apollodata.com/the-concepts-of-graphql-bc68bd819be3
![Page 38: Graphs All The Way Down](https://reader031.vdocuments.net/reader031/viewer/2022020916/61b41753c1a0ff539350023f/html5/thumbnails/38.jpg)
Can we build a better integration?
?
![Page 39: Graphs All The Way Down](https://reader031.vdocuments.net/reader031/viewer/2022020916/61b41753c1a0ff539350023f/html5/thumbnails/39.jpg)
Developer Story - A GraphQL-Neo4j integration
• If you are assuming your application data is a graph, why not model / store it as a graph?
• GraphQL + Neo4j removes mapping / translation • Why write SQL and GraphQL? • We translate GraphQL to Cypher
• Performance benefit of • storing your application data graph as a graph • sending a single query to the database
• Cypher with GraphQL takes the power of querying your application data to the next level • Optionally embed Cypher within GraphQL fields to move beyond just
pulling trees out of your application data graph...
![Page 40: Graphs All The Way Down](https://reader031.vdocuments.net/reader031/viewer/2022020916/61b41753c1a0ff539350023f/html5/thumbnails/40.jpg)
Neo4j scaffold tool
npm install neo4j-graphql-cli neo4j-graphql myschema.schema
Neo4j Sandbox
Neo4jneo4j-graphql extension
POST myschema.schema
Client app scaffold
GET/POST /graphql
scaffolds
neo4j-graphql
OR
Bring your own instance
Neo4jneo4j-graphql extension
Serves /graphql endpoint
Client app scaffold (apollo-client, React)
![Page 41: Graphs All The Way Down](https://reader031.vdocuments.net/reader031/viewer/2022020916/61b41753c1a0ff539350023f/html5/thumbnails/41.jpg)
Your Application Data Is A Graph
• Storing data in a graph database removes mapping / translation layer found in GraphQL resolvers
http://graphql.org/learn/thinking-in-graphs/
![Page 42: Graphs All The Way Down](https://reader031.vdocuments.net/reader031/viewer/2022020916/61b41753c1a0ff539350023f/html5/thumbnails/42.jpg)
Performance
• By translating GraphQL queries to a single Cypher query removes performance hit of N+1 database queries (1 per field)
![Page 43: Graphs All The Way Down](https://reader031.vdocuments.net/reader031/viewer/2022020916/61b41753c1a0ff539350023f/html5/thumbnails/43.jpg)
Cypher
• Expose Cypher through GraphQL schema directives
type User { name: ID! address: String seen: [Movie] @relation(name: "RATED") recommended(first:Int = 5): [Movie] @cypher(statement:"WITH $this as u MATCH (u)-->(:Movie)<--(:User)-->(reco:Movie) WHERE NOT (u)-[:RATED]->(reco) RETURN reco, count(*) as score ORDER BY score DESC LIMIT $first") }
![Page 45: Graphs All The Way Down](https://reader031.vdocuments.net/reader031/viewer/2022020916/61b41753c1a0ff539350023f/html5/thumbnails/45.jpg)
![Page 46: Graphs All The Way Down](https://reader031.vdocuments.net/reader031/viewer/2022020916/61b41753c1a0ff539350023f/html5/thumbnails/46.jpg)
https://neo4j.com/developer/graphql/
npm install neo4j-graphql-clineo4j-graphql
![Page 47: Graphs All The Way Down](https://reader031.vdocuments.net/reader031/viewer/2022020916/61b41753c1a0ff539350023f/html5/thumbnails/47.jpg)
(you)-[:HAVE]->(?)(?)<-[:ANSWERS]-(will)
![Page 48: Graphs All The Way Down](https://reader031.vdocuments.net/reader031/viewer/2022020916/61b41753c1a0ff539350023f/html5/thumbnails/48.jpg)