GraphQL ♥ Graph Database
15/03/2018
Marco Liberati
@dej611
Enrico Risa
@wolf4ood
Agenda
- GraphQL Introduction
- GraphQL Schema Definition (Meetup)
- Why GraphQL ♥ Graph Database?
- Schema Impl with a GraphDB
WHY GRAPHQL?
Let’s Talk About Rest first
● Resource Based
● HTTP Verbs
○ GET
○ PUT
○ PATCH
○ DELETE
○ POST
Actions -> Verbs + Uris
Meetup model with REST
● https://api.meetup.com/:urlname
● https://api.meetup.com/:urlname/members
● https://api.meetup.com/:urlname/events
● https://api.meetup.com/:urlname/events/:event_id/rsvp
Sounds Cool, Let’s Use it
Multiple Round Trips Problem
How Do we Handle That?
GET /GraphRM?fields=topics
…And What about Over-fetching?
GET /GraphRM?only=id,name
I need only those fields
You will probably end up with some custom
endpoints
API Documentation
● How can i use those APIs ?
● What parameters this endpoint support?
● Is this parameter required?
● Swagger
● JSON Schema
● JSON:API
● API Blueprint
Documentation should be good in order to let
people use our APIs without headache
Potential Problems
● Multiple Round trips
● Over/Under fetching
● Documentation
What GraphQL IS NOT..
● A query language for Graph Databases
● Database / Storage Engine
● A library
● It’s not language specific
What Is IT ?
GraphQL is a query language for APIs and a runtime for fulfilling those queries
with your existing data. GraphQL provides a complete and understandable
description of the data in your API, gives clients the power to ask for exactly what
they need and nothing more, makes it easier to evolve APIs over time, and
enables powerful developer tools.
Just a spec
● A declarative query language
● A type system
● A runtime that fetch and mutate data
Hello GraphQL
Hello GraphQL (with nesting)
How does it work ?
● The server describe what is possible (a type system)
● The client can asks exactly what data it requires
● The server then gives back results with the same shape as the query
Power to the client
Powerful Type System
● Hierarchical
● Strongly-Typed
● Self Documenting
● Enable Introspection
● Client-specified queries
Types
● Objects
● Scalar
○ Boolean
○ Int
○ Float
○ String
○ Custom Types
● Enums
● Lists
● Unions
● Interfaces
GraphQL Schema Language
● GraphQL type system
● Describes what data can be queried
● Language-agnostic
GraphQL Schema Language Example
Mutation
Introspection
… Enables Tools
● GraphiQL
● Documentation
● IDE plugins
● Code generation
● Client Side Validation
● …
GraphQL Schema Definition (Meetup)
● Entity
○ User
○ Meetup
○ Tag
○ Event
● Query
○ meetupsByName
○ usersByName
GraphQL Schema Definition (Meetup)
Demo time!
https://github.com/GraphRM/graphql-graphdb
Why GraphQL ♥ Graph Database?
A GraphQL query represents a description of a relationship:
Why GraphQL ♥ Graph Database?
Person “me”
:id
:name
Person
:name
Person
:name
A GraphQL query represents a description of a relationship:
Why GraphQL ♥ Graph Database?
Person “me”
:id
:name
Person
:name
Person
:name
Problem:
● For each Person the GraphQL
reducer will go to the database and
ask for data.
● This is known as the N+1 query
problem.
● Which in turns mean poor
performance
What if we could translate one GraphQL query to a single GraphDB query?
Why GraphQL ♥ Graph Database?
● This could prevent the N+1 queries problem
● A GraphDB is designed to run traversals
● Bonus: if the schema is the same for both GraphQL and DB that is a
huge win!
What if we could translate one GraphQL query to a single GraphDB query?
Why GraphQL ♥ Graph Database?
What if we could translate one GraphQL query to a single GraphDB query?
Why GraphQL ♥ Graph Database?
What if we could translate one GraphQL query to a single GraphDB query?
Why GraphQL ♥ Graph Database?
● This is still a PoC but the idea is:
○ Parse the Schema and the query
○ Traverse the query and build a database query
What if we could translate one GraphQL query to a single GraphDB query?
Why GraphQL ♥ Graph Database?
This is not fully compatible yet but there are some PoC:
● GraphQL -> Neo4J Cypher:
https://github.com/neo4j-graphql/neo4j-graphql-js
● GraphQL -> OrientDB Gremlin + MATCH
https://github.com/kensho-technologies/graphql-compiler
What if we could translate one GraphQL query to a single GraphDB query?
Why GraphQL ♥ Graph Database?
Demo time!
https://github.com/GraphRM/graphql-graphdb
Grazie :)
Resources
● GraphQL: http://graphql.org/learn/
● AST Explorer: https://astexplorer.net/
● GRANDstack: https://grandstack.io/
● GraphQL + OrientDB:
https://blog.kensho.com/compiled-graphql-as-a-database-query-language-72e1
06844282
● GraphQL + Neo4J: https://neo4j.com/developer/graphql/
● Five common pitfalls with GraphQL and how Neo4J-GraphQL aims to solve
them:
https://blog.grandstack.io/five-common-graphql-problems-and-how-neo4j-graph
ql-aims-to-solve-them-e9a8999c8d43

GraphQL ♥︎ GraphDB

  • 1.
    GraphQL ♥ GraphDatabase 15/03/2018
  • 2.
  • 3.
    Agenda - GraphQL Introduction -GraphQL Schema Definition (Meetup) - Why GraphQL ♥ Graph Database? - Schema Impl with a GraphDB
  • 4.
  • 5.
    Let’s Talk AboutRest first ● Resource Based ● HTTP Verbs ○ GET ○ PUT ○ PATCH ○ DELETE ○ POST Actions -> Verbs + Uris
  • 6.
    Meetup model withREST ● https://api.meetup.com/:urlname ● https://api.meetup.com/:urlname/members ● https://api.meetup.com/:urlname/events ● https://api.meetup.com/:urlname/events/:event_id/rsvp
  • 7.
  • 8.
  • 9.
    How Do weHandle That? GET /GraphRM?fields=topics
  • 10.
    …And What aboutOver-fetching? GET /GraphRM?only=id,name I need only those fields
  • 11.
    You will probablyend up with some custom endpoints
  • 12.
    API Documentation ● Howcan i use those APIs ? ● What parameters this endpoint support? ● Is this parameter required? ● Swagger ● JSON Schema ● JSON:API ● API Blueprint Documentation should be good in order to let people use our APIs without headache
  • 13.
    Potential Problems ● MultipleRound trips ● Over/Under fetching ● Documentation
  • 15.
    What GraphQL ISNOT.. ● A query language for Graph Databases ● Database / Storage Engine ● A library ● It’s not language specific
  • 16.
    What Is IT? GraphQL is a query language for APIs and a runtime for fulfilling those queries with your existing data. GraphQL provides a complete and understandable description of the data in your API, gives clients the power to ask for exactly what they need and nothing more, makes it easier to evolve APIs over time, and enables powerful developer tools.
  • 17.
    Just a spec ●A declarative query language ● A type system ● A runtime that fetch and mutate data
  • 18.
  • 19.
  • 20.
    How does itwork ? ● The server describe what is possible (a type system) ● The client can asks exactly what data it requires ● The server then gives back results with the same shape as the query Power to the client
  • 21.
    Powerful Type System ●Hierarchical ● Strongly-Typed ● Self Documenting ● Enable Introspection ● Client-specified queries
  • 22.
    Types ● Objects ● Scalar ○Boolean ○ Int ○ Float ○ String ○ Custom Types ● Enums ● Lists ● Unions ● Interfaces
  • 23.
    GraphQL Schema Language ●GraphQL type system ● Describes what data can be queried ● Language-agnostic
  • 24.
  • 25.
  • 26.
  • 27.
    … Enables Tools ●GraphiQL ● Documentation ● IDE plugins ● Code generation ● Client Side Validation ● …
  • 28.
    GraphQL Schema Definition(Meetup) ● Entity ○ User ○ Meetup ○ Tag ○ Event ● Query ○ meetupsByName ○ usersByName
  • 29.
    GraphQL Schema Definition(Meetup) Demo time! https://github.com/GraphRM/graphql-graphdb
  • 30.
    Why GraphQL ♥Graph Database?
  • 31.
    A GraphQL queryrepresents a description of a relationship: Why GraphQL ♥ Graph Database? Person “me” :id :name Person :name Person :name
  • 32.
    A GraphQL queryrepresents a description of a relationship: Why GraphQL ♥ Graph Database? Person “me” :id :name Person :name Person :name Problem: ● For each Person the GraphQL reducer will go to the database and ask for data. ● This is known as the N+1 query problem. ● Which in turns mean poor performance
  • 33.
    What if wecould translate one GraphQL query to a single GraphDB query? Why GraphQL ♥ Graph Database? ● This could prevent the N+1 queries problem ● A GraphDB is designed to run traversals ● Bonus: if the schema is the same for both GraphQL and DB that is a huge win!
  • 34.
    What if wecould translate one GraphQL query to a single GraphDB query? Why GraphQL ♥ Graph Database?
  • 35.
    What if wecould translate one GraphQL query to a single GraphDB query? Why GraphQL ♥ Graph Database?
  • 36.
    What if wecould translate one GraphQL query to a single GraphDB query? Why GraphQL ♥ Graph Database? ● This is still a PoC but the idea is: ○ Parse the Schema and the query ○ Traverse the query and build a database query
  • 37.
    What if wecould translate one GraphQL query to a single GraphDB query? Why GraphQL ♥ Graph Database? This is not fully compatible yet but there are some PoC: ● GraphQL -> Neo4J Cypher: https://github.com/neo4j-graphql/neo4j-graphql-js ● GraphQL -> OrientDB Gremlin + MATCH https://github.com/kensho-technologies/graphql-compiler
  • 38.
    What if wecould translate one GraphQL query to a single GraphDB query? Why GraphQL ♥ Graph Database? Demo time! https://github.com/GraphRM/graphql-graphdb
  • 39.
  • 40.
    Resources ● GraphQL: http://graphql.org/learn/ ●AST Explorer: https://astexplorer.net/ ● GRANDstack: https://grandstack.io/ ● GraphQL + OrientDB: https://blog.kensho.com/compiled-graphql-as-a-database-query-language-72e1 06844282 ● GraphQL + Neo4J: https://neo4j.com/developer/graphql/ ● Five common pitfalls with GraphQL and how Neo4J-GraphQL aims to solve them: https://blog.grandstack.io/five-common-graphql-problems-and-how-neo4j-graph ql-aims-to-solve-them-e9a8999c8d43