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

GraphQL on Rails @ RailsConf 2018

GraphQL on Rails @ RailsConf 2018

exAspArk

April 17, 2018
Tweet

More Decks by exAspArk

Other Decks in Programming

Transcript

  1. • Everything you need to know about GraphQL • Simple

    application with Ruby on Rails + React + Webpacker • GraphQL schema definition on the server-side • Consuming GraphQL API, automatic code generation, integration • Extra: error handling, authorization, monitoring, N+1 queries What are we doing today?
  2. Server describes what data is available User Query Name Post

    Title Content Comment Text Mutation Subscription
  3. Clients ask what data they need User Query Name Post

    Title Content Comment Text Mutation Subscription
  4. • What is GraphQL and why a graph, GraphQL specification

    • New blog application with Rails v5.2 • What is Webpack, Webpacker vs Sprockets • One-way data flow, React vs Turbolinks • Static HTML template Part 1: Bootstrapping simple application
  5. • Hot Module Replacement – faster build time without a

    full reload • Tons of loaders – ES6, CSS post-processors, linters • Tree shaking – exclude unused code • Code splitting – separate static and dynamic parts of your code • Compression, source-maps, minification • WebAssembly support Webpack
  6. • Smooth integration with Webpack – The Rails Way™ •

    React, Angular, Elm and Vue support out-of-the-box • Rails view helpers Webpacker https://github.com/rails/webpacker
  7. Webpacker Once you become familiar with Webpack, you probably don’t

    need to use Webpacker – an extra dependency, doesn’t support the latest versions. https://medium.com/webpack/webpack-4-released-today-6cdb994702d4
  8. What is React? ____ https://reactjs.org/ • JavaScript library for building

    user interfaces • Update and render just the right components when your data changes • Declarative views make your code more predictable and easier to debug • Encapsulated components that manage their own state • Learn once, write anywhere (React Native), not opinionated • Great ecosystem, a lot of tools and libraries
  9. One-way data flow Browser DOM implementations are generally really slow.

    One-way data-binding is the process of completely regenerating the HTML whenever the state from a single source changes.
  10. The jQuery way™ value = ‘Input’ Input value = ‘Input123’

    Input123 value = ‘Input321’ Input321 Source of truth Source of truth
  11. One-way data flow value = ‘Input’ Input value = ‘Input123’

    Input123 Input321 function(‘Input321’) value = ‘Input321’ Input321 Single source of truth
  12. Why one-way flow is simpler? Server: – I have OpenSSL

    1.0.1c __ – Can we upgrade to 1.1.0h?
  13. Why one-way flow is simpler? Server: – I have OpenSSL

    1.0.1c __ – Can we upgrade to 1.1.0h? – Maybe we should upgrade to 1.0.1u first?
  14. Why one-way flow is simpler? Server: – I have OpenSSL

    1.0.1c __ – Can we upgrade to 1.1.0h? – Maybe we should upgrade to 1.0.1u first? – Or can we upgrade to 1.0.2o, right?
  15. Why one-way flow is simpler? Server: – I have OpenSSL

    1.0.1c __ – Can we upgrade to 1.1.0h? – Maybe we should upgrade to 1.0.1u first? – Or can we upgrade to 1.0.2o, right? – Well, it probably depends on our current version version
  16. Why one-way flow is simpler? Server: – I have OpenSSL

    1.0.1c __ – Can we upgrade to 1.1.0h? – Maybe we should upgrade to 1.0.1u first? – Or can we upgrade to 1.0.2o, right? – Well, it probably depends on our current version version – What did we update last time?
  17. Why one-way flow is simpler? Server: – I have OpenSSL

    1.0.1c __ – Can we upgrade to 1.1.0h? – Maybe we should upgrade to 1.0.1u first? – Or can we upgrade to 1.0.2o, right? – Well, it probably depends on our current version version – What did we update last time? Docker: – I have OpenSSL 1.0.1c __ – Can we upgrade to 1.1.0h? – Yes! Simply setup your environment from scratch.
  18. Why one-way flow is simpler? Server: Docker: Init State1 State2

    Init State1 Init State2 No intermediate state
  19. We are a small startup, not Facebook! I used React

    for demonstration purposes only. Yes, using SPA is complicated. But use appropriate tools to solve your problems.
  20. Turbolinks vs React Turbolinks: – Received an event. – Let’s

    re-render the whole page. – Done, just replaced the whole HTML DOM body
  21. Turbolinks vs React Turbolinks: – Received an event. – Let’s

    re-render the whole page. – Done, just replaced the whole HTML DOM body React: – Received an event. – Let’s calculate the final state in Virtual DOM (reconciliation). – Let’s quickly re-render only necessary elements in DOM.
  22. Turbolinks vs React Turbolinks: – Received an event. – Let’s

    re-render the whole page. – Done, just replaced the whole HTML DOM body React: – Received an event. – Let’s calculate the final state in Virtual DOM (reconciliation). – Let’s quickly re-render only necessary elements in DOM. – We could pause until the new data is fetched in 3G (I/O). – We could re-render in smaller chunks during idle time only (CPU)
  23. • What is GraphQL and why a graph, GraphQL specification

    • New blog application with Rails v5.2 • What is Webpack, Webpacker vs Sprockets • One-way data flow, React vs Turbolinks • Static HTML template Part 1: Bootstrapping simple application
  24. • What is GraphQL and why a graph, GraphQL specification

    • New blog application with Rails v5.2 • What is Webpack, Webpacker vs Sprockets • One-way data flow, React vs Turbolinks • Static HTML template Part 1: Bootstrapping simple application
  25. • GraphiQL – exploring automatically generated documentation • Adding models

    • GraphQL gem v1.8 • GraphQL API endpoint, schema, types, enums, arguments • How GraphQL is different from REST API and what are the benefits Part 2: Describing GraphQL schema
  26. GraphQL Types: Active Model Serializer on steroids • Declarative specification

    for available fields • Custom field implementation with Ruby instance methods • Strong type definitions, description for documentation • Type composition (relations) • Don’t worry about response root key names (GraphQL specification) • API consumers specify fields they need
  27. • GraphiQL – exploring automatically generated documentation • Adding models

    • GraphQL gem v1.8 • GraphQL API endpoint, schema, types, enums arguments • How GraphQL is different from REST API and what are the benefits Part 2: Describing GraphQL schema
  28. • GraphiQL – exploring automatically generated documentation • Adding models

    • GraphQL gem v1.8 • GraphQL API endpoint, schema, types, enums arguments • How GraphQL is different from REST API and what are the benefits Part 2: Describing GraphQL schema
  29. • GraphQL clients • Consuming GraphQL API with modern JavaScript

    tools • Client-side code generation • Seemless integration – contract testing Part 3: Consuming GraphQL
  30. Why use GraphQL clients? • Data fetching • Managing state

    and caching data (Redux is not required) • UI framework integrations (React, Angular, Vue) • Managing websocket connections for GraphQL subscriptions • Query batching • Helpers for pagination • Dev tools for debugging and introspecting
  31. • Framework agnostic • Very flexible, not opinionated • Incrementally

    adoptable • Big community Apollo Client ___ https://www.apollographql.com/client
  32. • GraphQL clients • Consuming GraphQL API with modern JavaScript

    tools • Client-side code generation • Seemless integration – contract testing Part 3: Consuming GraphQL
  33. • GraphQL clients • Consuming GraphQL API with modern JavaScript

    tools • Client-side code generation • Seemless integration – contract testing Part 3: Consuming GraphQL
  34. With great flexibility on the client-side comes great responsibility on

    the server-side. Let’s make our GraphQL API more robust
  35. • Error handling • Authorization • Monitoring • Solving N+1

    queries Part 4: Making GraphQL API more robust
  36. Apollo Engine https://www.apollographql.com/engine • Query execution tracing per field •

    GraphQL error tracking • Caching • Alerts • Free for up to 1 million requests per month
  37. • Error handling • Authorization • Monitoring • Solving N+1

    queries Part 4: Making GraphQL API more robust
  38. • Error handling • Authorization • Monitoring • Solving N+1

    queries Part 4: Making GraphQL API more robust
  39. Takeaways • GraphQL is a specification • Getting the data

    you need, no under-fetching / over-fetching • Strong type system for documentation and seamless integration • Incrementally adoptable on client-side / server-side (7y old Rails app __) • Simpler to understand for junior developers: • Server-side: no routes, controllers, serializers, better composition • Client-side: no manual data fetching, mapping, caching (Redux)
  40. Takeaways • GraphQL is a specification • Getting the data

    you need, no under-fetching / over-fetching • Strong type system for documentation and seamless integration • Incrementally adoptable on client-side / server-side (7y old Rails app __) • Simpler to understand for junior developers: • Server-side: no routes, controllers, serializers, better composition • Client-side: no manual data fetching, mapping, caching (Redux) Conceptual compression!
  41. When to use GraphQL? • You have more than one

    API client (e.g. web + mobile app) • You care about latency and bandwidth (e.g. mobile networks) • You need a large API which you’re planning to change over time • You have multiple data sources (services, databases) and you need one API gateway
  42. What we didn’t cover • Testing GraphQL • Pagination: limit

    / offset vs. cursor-based • Versioning, schema masking, stitching, delegation • Caching • Persisted queries • Query depth / complexity • Mutations for changing state on client-side / server-side • Subscriptions