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

GraphQL for web devs who don't need it

GraphQL for web devs who don't need it

Chew Choon Keat

January 17, 2020
Tweet

More Decks by Chew Choon Keat

Other Decks in Programming

Transcript

  1. Your app has many APIs POST /things { "json": input1

    } POST /things/two { "json": input2 } POST /other { "json": input3 } GET /other/things
  2. Today: Focus on 1 API POST /things { "json": input1

    } POST /things/two { "json": input2 } POST /other { "json": input3 } GET /other/things
  3. POST /things { "json": input1 } POST /things/two { "json":

    input2 } POST /other { "json": input3 } GET /other/things Same Authentication, Same Middleware, Same HTTP client, Same HTTP server,… Changing small part
  4. Your app has many APIs POST /things { "json": input1

    } POST /things/two { "json": input2 } POST /other { "json": input3 } GET /other/things
  5. Your app has many APIs POST /things { "json": input1

    } POST /things/two { "json": input2 } POST /other { "json": input3 } GET /other/things If only we could document and describe our APIs
  6. What’s the catch? • Succinct API document • Tools that

    speak my API (“repositories”) not request path, query string...
  7. Impact for Front-end POST /things { "json": input1 } POST

    /things/two { "json": input2 } POST /other { "json": input3 } GET /other/things
  8. Impact for Front-end POST /q {"query": "allThings()", input1 } POST

    /q {“query":"createThing(x:$x)", input2 } POST /q {"query": "other()..", input3 } POST /q {"query": "otherThings().." }
  9. Scope of HTTP API [client] [server] construct HTTP request send

    HTTP request receive HTTP request parse HTTP headers invoke correct HTTP handler parse request payload process construct response send response receive response parses response process response
  10. Scope of HTTP API [client] [server] construct HTTP request send

    HTTP request receive HTTP request parse HTTP headers invoke correct HTTP handler parse request payload process construct response send response receive response parses response process response List Comment -> String String -> List User
  11. Scope of GraphQL? [client] [server] construct HTTP request send HTTP

    request receive HTTP request parse HTTP headers invoke correct HTTP handler parse request payload process construct response send response receive response parses response process response
  12. Scope of GraphQL [client] [server] construct HTTP request send HTTP

    request receive HTTP request parse HTTP headers invoke correct HTTP handler parse request payload process construct response send response receive response parses response process response List Comment -> String String -> List User
  13. GraphQL is • Request JSON format • decoder of request

    into strongly typed variables • free validation; optional, required fields • JSON encoder for your response
  14. GraphQL is • A specification • Implementations are standard middleware

    • like Ruby’s Rack or Python’s WSGI • http.Handler wrapper
  15. GraphQL is NOT • a framework • “what if it

    doesn’t do everything i need!” • mutually exclusive with REST • “what about email confirmation link, download csv, …?”
  16. GraphQL is NOT • a framework • “what if it

    doesn’t do everything i need!” • mutually exclusive with REST • “what about email confirmation link, download csv, …?”
  17. Schema & Handler 
 This routing is clear
 - what

    api
 - what input
 - what output 
 and checked by compiler

  18. Schema & Handler 
 This routing is clear
 - what

    api
 - what input
 - what output 
 and checked by compiler

  19. Schema & Handler 
 This routing is clear
 - what

    api
 - what input
 - what output 
 and checked by compiler

  20. [client] [server] construct HTTP request send HTTP request receive HTTP

    request parse HTTP headers invoke correct HTTP handler parse request payload function(args) return construct response send response receive response parses response process response Focus on Logic
  21. [client] [server] construct HTTP request send HTTP request receive HTTP

    request parse HTTP headers invoke correct HTTP handler parse request payload function(args) return construct response send response receive response parses response process response Focus on Logic Same Authentication, Same Middleware, Same HTTP client, Same HTTP server,…
  22. [client] [server] construct HTTP request send HTTP request receive HTTP

    request parse HTTP headers invoke correct HTTP handler parse request payload function(args) return construct response send response receive response parses response process response Focus on Logic 
 TDD Friendly 

  23. Summary • Front-end remain familiar with option to go further

    • GraphiQL — developer console to your web API • Focus on function processing data • TDD friendly, no views, and no templates! • Add regular HTTP endpoints as needed • Just middleware; wrap JWT, CORS, Instrumentation
  24. Summary • Front-end remain familiar with option to go further

    • GraphiQL — developer console to your web API • Focus on function processing data • TDD friendly, no views, and no templates! • Add regular HTTP endpoints as needed • Just middleware; wrap JWT, CORS, Instrumentation