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

Beyond REST API

Lin Min Phyo
December 22, 2019

Beyond REST API

Lin Min Phyo

December 22, 2019
Tweet

More Decks by Lin Min Phyo

Other Decks in Technology

Transcript

  1. V1

  2. V2

  3. V2 Changes • New tab for Activity Log is added

    • Create new API for Activity Log
  4. API Schema • V2 • GET https://www.better.hr/api/web/profile/{id} • GET https://www.better.hr/api/web/profiles

    • GET https://www.better.hr/api/web/activiylog/{id} • GET https://www.better.hr/api/web/activiylogs
  5. V3

  6. V3 Changes • Added Payroll tab • Move payroll related

    fields from profile to payroll tab • Clean up old API
  7. API Schema • V3 • GET https://www.better.hr/api/web/profile/{id} • GET https://www.better.hr/api/web/profiles

    • GET https://www.better.hr/api/web/activiylog/{id} • GET https://www.better.hr/api/web/activiylogs • GET https://www.better.hr/api/web/payroll/{id} • GET https://www.better.hr/api/web/payrolls
  8. Things to consider for Mobile Apps • JSON should be

    as lightweight as possible • Backward compatible APIs
  9. Mobile endpoints • GET https://www.better.hr/api/mobile/profile/{id} • GET https://www.better.hr/api/mobile/profiles • GET

    https://www.better.hr/api/mobile/activiylog/{id} • GET https://www.better.hr/api/mobile/activiylogs • GET https://www.better.hr/api/mobile/payroll/{id} • GET https://www.better.hr/api/mobile/payrolls
  10. What we realize • More frequent changes = More problems

    • More releases = More problems • More feature changes = More problems • More UI changes = More problems
  11. Explore a new thing. • Reduce loads of API developers

    • Want Single Source of Truth • Lightweight network calls • Easy to evolve over time • Want to experiment something new
  12. Backend Dev Frontend Dev Mobile Dev - Build API -

    Make changes Consumes API Consumes API REST
  13. Backend Dev Frontend Dev Mobile Dev Build Schema, which information

    is opened for clients Consumes API Consumes API GraphQL Ask required information Ask required information
  14. NO

  15. REST curl -X POST -H "Content-Type: application/json" --data '{ "query":

    "{ users { name } }" }' https://www.example.com/graphql curl -X GET -H "Content-Type: application/json" https://www.example.com/users GraphQL
  16. curl -X GET -H "Content-Type: application/json" https://www.example.com/users curl -X POST

    -H "Content-Type: application/json" --data '{ "query": "{ users { name } }" }' https://www.example.com/graphql REST GraphQL
  17. Server Side I can give you this User info :

    ID, Name, Nick Name, Job Title, Salary, Phone, Address, Gender
  18. type User { id : String name: String nickName: String

    jobTitle : String salary : Float phone : String address : String gender : Gender } I can give you this User info : ID, Name, Nick Name, Job Title, Salary, Phone, Address, Gender GraphQL Schema
  19. GraphQL Schema • Provided by Backend • Documentation that shows

    information provided for clients https://graphql.org/learn/schema/
  20. type User { id : String name: String nickName: String

    jobTitle : String salary : Float phone : String address : String gender : Gender } I can give you this User info : ID, Name, Nick Name, Job Title, Salary, Phone, Address, Gender GraphQL Schema
  21. Result { "data": { “user": { "name": "Lin Phyo", "phone":

    “+959950001234" } } } query { user { name phone } } API returns
  22. Result { "data": { “user": { "name": "Lin Phyo", "phone":

    “+959950001234”, "email": “[email protected]”, } } } query { user { name phone email } } API returns Request Parameter
  23. Result { "data": { “user": { "id": “u0001”, "name": "Lin

    Phyo", "phone": “+959950001234”, "email": “[email protected]”, } } } query { user { id name phone email } } API returns Request Parameter
  24. query { users { name phone } } select name,

    phone from user where id = 1
  25. query { users( id = 1 ) { name phone

    } } select name, phone from user where id = 1
  26. query { users( id = 1 ) { name phone

    } } select name, phone from user where id = 1
  27. You can update this User information : Nick Name, Phone

    & Address Server Side Client wants to update some information
  28. Okay. I want to update Nick name & Phone Client

    Side Client wants to update some information
  29. React import gql from 'graphql-tag'; import { useQuery } from

    '@apollo/react-hooks'; const GET_DOGS = gql` { dogs { id breed } } `; const { loading, error, data } = useQuery(GET_DOGS);
  30. React import gql from 'graphql-tag'; import { useQuery } from

    '@apollo/react-hooks'; const GET_DOGS = gql` { dogs { id breed } } `; const { loading, error, data } = useQuery(GET_DOGS);
  31. Android / iOS > apollo client:download-schema [OUTPUT] Put it inside

    project query GetMyProfile { me{ id name email } } Write the query you want : GetMyProfile.graphql
  32. Android / iOS > apollo client:download-schema [OUTPUT] Put it inside

    project Write the query you want : GetMyProfile.graphql Build Project which will automatically run > apollo client:codegen [OUTPUT] API.swift for iOS or GetMyProfileQuery.java
  33. Android / iOS > apollo client:download-schema [OUTPUT] Put it inside

    project Write the query you want : GetMyProfile.graphql Build Project which will automatically run > apollo client:codegen [OUTPUT] Use generated method or class to call API
  34. With Variables query GetUser($id : Int) { users( id =

    $id ) { name phone } } { “id” : 1 }
  35. Without Aliasing query GetUser($id : Int) { users( id =

    $id ) { name phone } } { “id” : 1 }
  36. With Aliasing query GetUser($id : Int) { employees : users(

    id = $id ) { name phone } } { “id” : 1 }
  37. Enums enum EmployeeStatus { ACTIVE, DEACTIVE, DELETE } type User

    { name : String, status : EmployeeStatus }
  38. Enums query GetEmployeeStatuses { users { name status } }

    type User { name : String, status : EmployeeStatus }
  39. Enums query GetEmployeeStatuses { users { name status } }

    { "data": { "users": [ { "name": “John Doe", "status": "ACTIVE" }, ... ] } }
  40. Enums { "data": { "users": [ { "name": “John Doe",

    "status": "ACTIVE" }, ... ] } } enum EmployeeStatus { ACTIVE, DEACTIVE, DELETE }
  41. Interfaces type NotiTypeA implements Notification { id: ID! title: String!

    description: String! } interface Notification{ id: ID! title: String! description: String! }
  42. Interfaces interface Notification{ id: ID! title: String! description: String! }

    type NotiTypeB implements Notification { id: ID! title: String! description: String! attachmentsUrls: [String!]! } type NotiTypeA implements Notification { id: ID! title: String! description: String! }
  43. Unions union User = DashboardUser | MobileUser | NonMobileUser {

    users { ... on DashboardUser { role } ... on MobileUser { deviceID } ... on NonMobileUser { officeID } } }
  44. Unions union User = DashboardUser | MobileUser | NonMobileUser {

    users { ... on DashboardUser { role } ... on MobileUser { deviceID } ... on NonMobileUser { officeID } } }
  45. Unions union User = DashboardUser | MobileUser | NonMobileUser {

    users { ... on DashboardUser { role } ... on MobileUser { deviceID } ... on NonMobileUser { officeID } } }
  46. Unions union User = DashboardUser | MobileUser | NonMobileUser {

    users { ... on DashboardUser { role } ... on MobileUser { deviceID } ... on NonMobileUser { officeID } } }
  47. Tooling Altair GraphQL Client https://altair.sirmuel.design/ IntelliJ Plugin https://github.com/jimkyndemeyer/js-graphql-intellij-plugin Apollo Platform

    https://www.apollographql.com/docs/intro/platform Apollo CLI https://github.com/apollographql/apollo-tooling
  48. Apollo Platform • Apollo Server • Apollo Client • iOS

    and Android • Apollo CLI https://www.apollographql.com/docs/intro/platform#open- source
  49. No

  50. Drawbacks • Multipart request is not out of the box

    • Server side caching is harder compared to REST • Rate limiting for public APIs • Careless developers problem • Project handover
  51. But, we gain • Write once, query anywhere • Single

    source of truth • Faster frontend, mobile development • Able to call different data nodes in one API call • Easier to analyze which fields are requested by whom • Easier to grow overtime • Performant API
  52. @Linminphyoe1 https://lin.phyo.work Take a ride through a Burmese tech scene

    made by Burmese, for Burmese https://anchor.fm/techshaw @vincentpaing