47
GRAPHQL Sebastian Siemssen @thefubhy

Zensations Drupal 8 GraphQL Presentation 2015

Embed Size (px)

Citation preview

Page 1: Zensations Drupal 8 GraphQL Presentation 2015

GRAPHQLSebastian Siemssen @thefubhy

Page 2: Zensations Drupal 8 GraphQL Presentation 2015
Page 3: Zensations Drupal 8 GraphQL Presentation 2015

A LITTLE STORY …

Page 4: Zensations Drupal 8 GraphQL Presentation 2015

‣ Requires multiple round trips for fetching complex object graphs

‣ Over fetching — As the model grows, so does the payload

‣ High potential for breaking API changes

‣ Structure of the response dictated by the server

‣ Potentially a huge amount of different endpoints and thus complexity

‣ No formal specification resulting in various other shortcomings

PROBLEMS WITH REST

Page 5: Zensations Drupal 8 GraphQL Presentation 2015

DEAR REST, I STILL LOVE YOU !

Page 6: Zensations Drupal 8 GraphQL Presentation 2015

Multiple round trips

When fetching complex, relational data structures: In order to descent further into the object graph, multiple round trips to the server are required.

Page 7: Zensations Drupal 8 GraphQL Presentation 2015
Page 8: Zensations Drupal 8 GraphQL Presentation 2015

Over fetching

Unless specifically designed for a given purpose, you often have to deal with needlessly large and bloated

responses.

Page 9: Zensations Drupal 8 GraphQL Presentation 2015
Page 10: Zensations Drupal 8 GraphQL Presentation 2015

Compatibility and versioning

By changing your model you are very likely to break your APIs. API versioning can mitigate that potential

damage at the cost of exponentially increasing complexity.

Page 11: Zensations Drupal 8 GraphQL Presentation 2015

Endpoints galore

Attempting to circumvent the aforementioned problems often leads to a huge amount of bespoke/ad-hoc endpoints. This, in turn, inevitably increases

the complexity of your application.

Page 12: Zensations Drupal 8 GraphQL Presentation 2015

Your API is usually composed of a

spectrum of different interpretations of REST

Page 13: Zensations Drupal 8 GraphQL Presentation 2015

No formal specification

No prescribed pattern for deprecation. No standardized introspection functionality.

Page 14: Zensations Drupal 8 GraphQL Presentation 2015

REST IS WHAT YOU MAKE IT

Page 15: Zensations Drupal 8 GraphQL Presentation 2015

„This is not not how we as product developers think about data. Product developers think of data in

terms of graphs.“

Nick Schrock

Page 16: Zensations Drupal 8 GraphQL Presentation 2015

Code cartoons by Lin Clark

Page 17: Zensations Drupal 8 GraphQL Presentation 2015
Page 18: Zensations Drupal 8 GraphQL Presentation 2015

WHAT IS GRAPHQL?

Page 19: Zensations Drupal 8 GraphQL Presentation 2015

What is GraphQL?

GraphQL is a query language designed to build client applications by providing an intuitive and flexible

syntax and system for describing their data requirements and interactions.

Page 20: Zensations Drupal 8 GraphQL Presentation 2015

IT IS NOT A QUERY LANGUAGE FOR A GRAPH DATABASE

Page 21: Zensations Drupal 8 GraphQL Presentation 2015

GRAPHQL IS AGNOSTIC TO YOUR

STORAGE LAYER

Page 22: Zensations Drupal 8 GraphQL Presentation 2015

{      me  {          name      }  }

Page 23: Zensations Drupal 8 GraphQL Presentation 2015

{      'me':  {          'name':  'Sebastian  Siemssen'      }  }

{      me  {          name      }  }

Page 24: Zensations Drupal 8 GraphQL Presentation 2015

{      user(id:  123)  {          name      }  }

Page 25: Zensations Drupal 8 GraphQL Presentation 2015

{      user(id:  123)  {          name      }  }

{      'user':  {          'name':  'Sebastian  Siemssen'      }  }

Page 26: Zensations Drupal 8 GraphQL Presentation 2015

{      'me':  {          'name':  'Sebastian  Siemssen',          'company':  {              'name':  'Zensations',              'website':  'http://zensations.at'          }      }  }

{      me  {          name,       company  {              name,              website          }      }  }

Page 27: Zensations Drupal 8 GraphQL Presentation 2015

{      'me':  {          'name':  'Sebastian  Siemssen',          'profilePic':  {              'width':  100,              'height':  200,              'url':  'http://...'          }      }  }

{      me  {          name,       profilePic(size:  100)  {              width,              height,              url          }      }  }

Page 28: Zensations Drupal 8 GraphQL Presentation 2015

{      'me':  {          'name':  'Sebastian  Siemssen',          'small':  {              'width':  100,              'height':  200,              'url':  'http://...'          },          'large':  {              'width':  300,              'height':  600,              'url':  'http://...'          }      }  }

{      me  {          name,       small:profilePic(size:  100)  {              width,              height,              url          },       large:profilePic(size:  300)  {              width,              height,              url          }      }  }

Page 29: Zensations Drupal 8 GraphQL Presentation 2015

{      'me':  {          'name':  'Sebastian  Siemssen',          'articles':  [         {                  'title':  'GraphQL  rocks'              },         {                  'title':  'Ruben  sucks'              },         {                  'title':  '...'              },         {                  'title':  '...'              },          ]      }  }

{      me  {          name,       articles  {              title          }      }  }

Page 30: Zensations Drupal 8 GraphQL Presentation 2015

{      'me':  {          'name':  'Sebastian  Siemssen',          'articles':  [              {                  'title':  'GraphQL  rocks',                  'comments':  [                      {                          'author':  {                              'name':  'Ruben  Teijeiro',                              'label':  'This  is  sorcercy!'                          }                      },                      {                          'author':  {                              'name':  'Dries  Buytaert',                              'label':  'We  need  that  in  core!'                          }                      }                  ]              }          ]      }  }

{      me  {          name,       articles  {              title,              comments(first:  10)  {                  author  {                      name,                      label                  }              }          }      }  }

Page 31: Zensations Drupal 8 GraphQL Presentation 2015

Code cartoons by Lin Clark

Page 32: Zensations Drupal 8 GraphQL Presentation 2015

Code cartoons by Lin Clark

Page 33: Zensations Drupal 8 GraphQL Presentation 2015

Code cartoons by Lin Clark

Page 34: Zensations Drupal 8 GraphQL Presentation 2015

GRAPHQL CHANGES THE SERVER — CLIENT

RELATIONSHIP

Page 35: Zensations Drupal 8 GraphQL Presentation 2015

THE SERVER PUBLISHES ITS POSSIBILITIES

THE CLIENT SPECIFIES ITS REQUIREMENTS

Page 36: Zensations Drupal 8 GraphQL Presentation 2015

type  Query  {      me:  User,      user:  (id:  Int!):  User  }

{      me  {          name      },      user(id:  123)  {          name      }  }

Page 37: Zensations Drupal 8 GraphQL Presentation 2015

type  User  {      name:  String,      articles:  (first:  Int,  orderBy:  ArticleOrderEnum):  [Article]  }

{      me  {          name          articles(first:  10,  orderBy:  CREATION_DATE)      },      user(id:  123)  {          name,          articles(first:  10,  orderBy:  CREATION_DATE)      }  }

enum  ArticleOrderEnum  [      CREATION_DATE,      CHANGED_DATE,      IMPORTANCE  ]

Page 38: Zensations Drupal 8 GraphQL Presentation 2015

TYPE INTROSPECTION

Page 39: Zensations Drupal 8 GraphQL Presentation 2015

{      __schema  {          queryType  {              name          },          types  {              name,              fields  {                  name,                  type  {                      kind                      name,                      ofType  {                          name                      }                  }              }          }      }  }

Page 40: Zensations Drupal 8 GraphQL Presentation 2015

BUILDING A GRAPHQL SERVER

Page 41: Zensations Drupal 8 GraphQL Presentation 2015

GraphQL

Your application code

Database External APIs Others

Page 42: Zensations Drupal 8 GraphQL Presentation 2015

type  User  {      name(user)  {          return  user.name;      },      articles(user,  arguments)  {          return  storage-­‐>load(…);      }  }

Page 43: Zensations Drupal 8 GraphQL Presentation 2015

There is even more …Conditionals, Fragments, Mutations, Subscriptions, …

Page 44: Zensations Drupal 8 GraphQL Presentation 2015

DEMO

Page 45: Zensations Drupal 8 GraphQL Presentation 2015

QUESTIONS

Page 46: Zensations Drupal 8 GraphQL Presentation 2015

‣ RFC Working Draft: https://facebook.github.io/graphql/

‣ Reference implemenetation: https://github.com/graphql/graphql-js

‣ GraphiQL: https://github.com/graphql/graphiql

‣ StarWars API Playground: http://graphql-swapi.parseapp.com/

‣ GraphQL Relay: https://facebook.github.io/relay/docs/graphql-relay-specification.html

‣ Learn GraphQL: https://learngraphql.com/

‣ …

RESOURCES

Page 47: Zensations Drupal 8 GraphQL Presentation 2015

A Wiedner Hauptstraße 64 1040 Wien

T 01 89 00 179

M [email protected]

W www.zensations.at