Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Learning GraphQL for Mobile

Learning GraphQL for Mobile

A talk at the Omaha Mobile Meetup about the future of GraphQL on Mobile.

Avatar for John Shelley

John Shelley

July 06, 2016
Tweet

More Decks by John Shelley

Other Decks in Programming

Transcript

  1. Learning GraphQL for Mobile John Shelley - @jpshells Mobile (Android)

    Developer @ Hudl http://public.hudl.com/jobs/
  2. ◦ GraphQL = “Graph Query Language” ◦ Data query language

    and runtime ◦ Alternative paradigm to REST i.e. client-side apps talking to backend servers CRUD: ◦ Retrieve -> GQL “Query” ◦ Create/Update/Delete -> GQL “Mutation” What even is it?
  3. ◦ Graph a set of connected nodes “Graph Query”? Huh?

    ◦ Query power of GQL is in querying
  4. ◦ User data type REST comparison: Intro { id: <int>

    name: <string> favouriteQuote: <string> thumbnailUrl: <string> }
  5. ◦ REST request (GET): ◦ User data type REST comparison:

    Intro { id: <int>, name: <string> favouriteQuote: <string> thumbnailUrl: <string> } blah.com/api/user/123
  6. ◦ REST request (GET): ◦ GraphQL request (POST): ◦ User

    data type REST comparison: Intro blah.com/api/user/123 blah.com/api/graphql query { user(id: 123) { id name favouriteQuote thumbnailUrl } } { data: { user : { id: 123, name: “Billy Big Braces” favouriteQuote: “blah blah blah…” thumbnailUrl: http://someUrl.com/kjh234/zxc0v8z0.jpg } } } { id: <int>, name: <string> favouriteQuote: <srting> thumbnailUrl: <string> }
  7. ◦ favouriteQuote is huge REST comparison: Unwanted fields ◦ thumbnailUrl

    is heavy to compute (e.g. requires long running process)
  8. ◦ favouriteQuote is huge REST comparison: Unwanted fields ◦ thumbnailUrl

    is heavy to compute (e.g. requires long running process) ◦ REST solution: blah.com/api/userlightweight/123
  9. ◦ favouriteQuote is huge REST comparison: Unwanted fields ◦ thumbnailUrl

    is heavy to compute (e.g. requires long running process) ◦ REST solution: ◦ GraphQL solution: blah.com/api/userlightweight/123 query { user(id: 123) { id name } } { data: { user : { id: 123, name: “Billy Big Braces” } } }
  10. ◦ A user has friends i.e. a user has connections

    to other users REST comparison: Connections
  11. ◦ A user has friends i.e. a user has connections

    to other users REST comparison: Connections ◦ REST solution: ajax.get('blah.com/api/user/123') .done((data) => { const friendsString = data.friends.join('-'); ajax.get(`blah.com/api/users/${friendsString}`) .done((data) => { // process friends data }); });
  12. ◦ A user has friends i.e. a user has connections

    to other users REST comparison: Connections ◦ REST solution: ajax.get('blah.com/api/user/123') .done((data) => { const friendsString = data.friends.join('-'); ajax.get(`blah.com/api/users/${friendsString}`) .done((data) => { // process friends data }); }); ◦ GraphQL solution: query { user(id: 123) { thumbnailUrl friends { name } } } { data: { user : { thumbnailUrl: “http://someUrl.com/kjh234/zxc0v8z0.jpg” friends: [ { name: “Sammy Sausage”, }, { name: “Sir Lancel Worthington-Hensley”, }, ... }
  13. ◦ AutoComplete ◦ Full featured docs ◦ Use the chrome

    extension ChromeiQL Note: make sure you’re logged in to the server GraphiQL
  14. ◦ Choose your own HTTP Stack
 (HttpUrlConnection, OkHttp, Retrofit, Volley,

    etc) ◦ One single endpoint ◦ POST a 2 field content body ◦ Only ever receive a 200 or 500s ◦ ETags/Caching?. . . Setup
  15. Why? ◦ Okay… so its not refined…but ◦ Ability to

    send less data over the wire. ◦ Ability to chain multiple calls into one. ◦ Ability to create complex queries dynamically. ◦ Clients don’t need to worry about API versioning.
 (A query should never become invalid)
  16. Why? “Rather than creating multiple simultaneous connections to download data,

    or chaining multiple consecutive GET requests, where possible you should bundle those requests into a single GET.” - Google https://developer.android.com/training/efficient-downloads/efficient-network-access.html
  17. ◦ .graphql files with attached schema ◦ Introspection to provide

    schema requirements ◦ Compile time code generation of ViewModels ◦ Strict parsing of Response based on Request query ◦ Local DB hooks for Sqlite and Core Data ◦ All the benefits without any of the pain Future