Advertisement
Advertisement

More Related Content

Advertisement
Advertisement

React & GraphQL

  1. @nikolasburk GraphQL The Why, What and How
  2. Nikolas Burk 👋 Developer at Graphcool $ whoami @nikolasburk
  3. Agenda 1. GraphQL Introduction 2. React + GraphQL = ❤ 3. Demo
  4. GraphQL Introduction REST vs GraphQL @nikolasburk
  5. What’s GraphQL? • new API standard by Facebook • query language for APIs • declarative way of fetching & updating data @nikolasburk
  6. A rapidly growing Community
  7. Mary Mary’s posts: Learn GraphQL Today Why GraphQL is better than REST React & GraphQL - A declarative love story Relay vs Apollo - GraphQL clients Last three followers: John, Alice, Sarah Example: Blogging App
  8. Example: Blogging App with REST /users/<id> /users/<id>/posts /users/<id>/followers 3 API endpoints
  9. 1 Fetch user data /users/<id>/users/<id> /users/<id>/posts /users/<id>/followers { “user”: { “id”: “er3tg439frjw” “name”: “Mary”, “address”: { … }, “birthday”: “July 26, 1982” } } HTTP GET Mary Mary’s posts: Last three followers:
  10. 2 /users/<id> /users/<id>/posts /users/<id>/followers Fetch posts HTTP GET { “posts”: [{ “id”: “ncwon3ce89hs” “title”: “Learn GraphQL today”, “content”: “Lorem ipsum … ”, “comments”: [ … ], }, { “id”: “dsifr3as0vds” “title”: “React & GraphQL - A declarative love story”, “content”: “Lorem ipsum … ”, “comments”: [ … ], }, { “id”: “die5odnvls1o” “title”: “Why GraphQL is better than REST”, “content”: “Lorem ipsum … ”, “comments”: [ … ], }, { “id”: “dovmdr3nvl8f” “title”: “Relay vs Apollo - GraphQL clients”, “content”: “Lorem ipsum … ”, “comments”: [ … ], }] } Mary Mary’s posts: Learn GraphQL Today Why GraphQL is better than REST React & GraphQL - A declarative love story Relay vs Apollo - GraphQL clients Last three followers:
  11. /users/<id> /users/<id>/posts /users/<id>/followers HTTP GET { “followers”: [{ “id”: “leo83h2dojsu” “name”: “John”, “address”: { … }, “birthday”: “January 6, 1970” },{ “id”: “die5odnvls1o” “name”: “Alice”, “address”: { … }, “birthday”: “May 1, 1989” }{ “id”: “xsifr3as0vds” “name”: “Sarah”, “address”: { … }, “birthday”: “November 20, 1986” } … ] } Mary Mary’s posts: Learn GraphQL Today Why GraphQL is better than REST React & GraphQL - A declarative love story Relay vs Apollo - GraphQL clients Last three followers: John, Alice, Sarah Fetch followers3
  12. 1 API endpoint Example: Blogging App with GraphQL
  13. Mary’s posts: Last three followers: Fetch everything with a single request1 HTTP POST query { User(id: “er3tg439frjw”) { name posts { title } followers(last: 3) { name } } }
  14. Mary’s posts: Last three followers: Mary Learn GraphQL Today Why GraphQL is better than REST React & GraphQL - A declarative love story Relay vs Apollo - GraphQL clients John, Alice, Sarah Fetch everything with a single request1 HTTP POST { “data”: { “User”: { “name”: “Mary”, “posts”: [ { title: “Learn GraphQL today” }, { title: “React & GraphQL - A declarative love story” } { title: “Why GraphQL is better than REST” } { title: “Relay vs Apollo - GraphQL Clients” } ], “followers”: [ { name: “John” }, { name: “Alice” }, { name: “Sarah” }, ] } } }
  15. Core Concepts Queries & Mutations @nikolasburk
  16. Queries … only read data query MessageQuery { Message(id: “1”) { text sentBy { name } } } @nikolasburk
  17. Queries … only read data query MessageQuery { Message(id: “1”) { text sentBy { name } } } @nikolasburk Operation Type 3 Operation Types: - query (default) - mutation - subscription
  18. Queries … only read data @nikolasburk Operation Name query MessageQuery { Message(id: “1”) { text sentBy { name } } }
  19. Queries … only read data @nikolasburk Fields query MessageQuery { Message(id: “1”) { text sentBy { name } } }
  20. Queries … only read data @nikolasburk Root Field query MessageQuery { Message(id: “1”) { text sentBy { name } } }
  21. Queries … only read data @nikolasburk Selection Set query MessageQuery { Message(id: “1”) { text sentBy { name } } }
  22. Queries … only read data query MessageQuery { Message(id: “1”) { text sentBy { name } } } { “data”: { “Message”: { “text”: “Hello 😎”, “sentBy”: { “name”: “Sarah” } } } } @nikolasburk
  23. Queries … only read data query MessageQuery { Message(id: “1”) { text sentBy { name } } } { “data”: { “Message”: { “text”: “Hello 😎”, “sentBy”: { “name”: “Sarah” } } } } @nikolasburk
  24. Queries … only read data { “data”: { “Message”: { “text”: “Hello 😎”, “sentBy”: { “name”: “Sarah” } } } } @nikolasburk query MessageQuery { Message(id: “1”) { text sentBy { name } } }
  25. Mutations … write and read data mutation CreateMessageMutation { createMessage(text:“Greetings 👋”) { id } } { “data”: { “createMessage”: { “id”: “3”, } } } @nikolasburk
  26. Mutations … write and read data mutation CreateMessageMutation { createMessage(text:“Greetings 👋”) { id } } { “data”: { “createMessage”: { “id”: “3”, } } } @nikolasburk
  27. Mutations … write and read data mutation CreateMessageMutation { createMessage(text:“Greetings 👋”) { id } } { “data”: { “createMessage”: { “id”: “3”, } } } @nikolasburk
  28. @nikolasburk • strongly typed & written in Schema Definition Language (SDL) • defines API capabilities ( = contract for client-server communication) • root types: Query, Mutation, Subscription The GraphQL Schema
  29. @nikolasburk type Person { name: String! messages: [Message!]! } type Message { text: String! sentBy: Person } Types in the SDL
  30. 💡 Want to know more? 👉 GraphQL Server Basics: The Schema Graphcool Blog: https://blog.graph.cool/ac5e2950214e
  31. …with GraphQL Playgrounds ▷ Let’s play
  32. React + GraphQL = ❤ React & GraphQL - A declarative love story @nikolasburk
  33. Declarative Programming https://en.wikipedia.org/wiki/Declarative_programming In computer science, declarative programming is a programming paradigm […] that expresses the logic of a computation without describing its control flow.
  34. Declarative Programming https://en.wikipedia.org/wiki/Declarative_programming In computer science, declarative programming is a programming paradigm […] that expresses the logic of a computation without describing its control flow.
  35. Declarative explain eludicate make clear disclose reveal announce
  36. Tell the machine what to do - not how to do it
  37. What are the abstractions & primitives you’re programming with?
  38. Why is GraphQL declarative? • describing data operations with dedicated language • core primitives: Queries & Mutations • DSL for working with data @nikolasburk
  39. Declarative programs… @nikolasburk … are using dedicated abstractions to express goals … lead to more expressive and concise code … make your programs easier to reason about
  40. GraphQL Clients • provide convenience API for sending queries & mutations • take caring of managing the cache • more handy features like optimistic UI, lower- level networking, realtime subscriptions… @nikolasburk
  41. Relay vs Apollo Client • open-sourced alongside GraphQL in 2015 • optimized for performance and reducing network traffic • notable learning curve • community-driven • easy-to-understand with intuitive abstractions • available on several platforms
  42. From imperative to declarative data fetching 1. construct and send HTTP request (e.g. with fetch) 2. receive and parse server response 3. store data locally (e.g. Redux) 4. display information in the UI Imperative data fetching
  43. From imperative to declarative data fetching 1. pass data requirements to GraphQL client 2. display information in the UI Declarative data fetching
  44. Demo 🔨 Simple Instagram Clone w/ GraphQL https://github.com/graphcool-examples/react-graphql/tree/master/quickstart-with-apollo
  45. Resources 📚 • How to GraphQL - The Fullstack Tutorial for GraphQL • GraphQL Weekly Newsletter • GraphQL Radio Podcast • GraphQL Summit 2017 Talks (Youtube)
  46. Community 🙌 • slack.graph.cool (> 6k members) • GraphQL Berlin Meetup • GraphQL Europe Conference (June 15, 2018)
  47. Thank you! 🙇 … any questions?
  48. GraphQL Subscriptions ⚡ • event-based realtime updates • clients subscribe to specific events • usually implemented with websockets @nikolasburk
  49. Realtime Updates with Subscriptions subscription { Message { node { text } } }
  50. Realtime Updates with Subscriptions { "data": { "Message": { "node": { "text": "Hello" } } } } subscription { Message { node { text } } } { "data": { "Message": { "node": { "text": "Hey" } } } } { "data": { "Message": { "node": { "text": "Greetings" } } } }
Advertisement