Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Introduction to GraphQL: Mobile Week SF

107 views

Published on

AWS Mobile Week at the San Francisco Loft

Introduction to GraphQL
GraphQL is a query language for APIs and a runtime for fulfilling those queries. It gives clients the power to ask for exactly what they need, which makes it a great fit for modern web and mobile apps. In this talk, we explain why GraphQL was created, introduce you to the syntax and behavior, and then show how to use it to build powerful APIs for your data. We will also introduce you to AWS AppSync, a GraphQL-powered serverless backend for apps, which you can use to host GraphQL APIs and also add real-time and offline capabilities to your web and mobile apps. You can follow along if you have an AWS account – no GraphQL experience required!
Level: Beginner
Speaker: Rohan Deshpande - Sr. Software Developer Engineer, AWS Mobile Applications

  • Be the first to comment

Introduction to GraphQL: Mobile Week SF

  1. 1. Introduction to GraphQL Rohan Deshpande ( @appwiz) Principal Engineer, Amazon Web Services © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
  2. 2. Agenda graphql { what why how } © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
  3. 3. What is GraphQL © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
  4. 4. “GraphQL is a query language for APIs and a runtime for fulfilling those queries with your existing data.” https://graphql.org © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
  5. 5. GraphQL type system • Object types • Interfaces • Unions • Enumerations • Fields • Lists • Scalars • String • Float • Int • Boolean • ID • Custom scalars e.g. Date © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
  6. 6. GraphQL operations Queries read data Mutations write data Subscriptions listen for data query { search(q: "name") { title author } } mutation { create(title: "book") { id } } subscription { onCreate { id title } } © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
  7. 7. 2012 Development started 2015 Open sourced 2016 2017 2018 Evolving specification Timeline not to scale © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
  8. 8. Mobile usage in 2017 •! 2+ billion smart phones1 • 1.6+ trillion hours2 2 https://www.appannie.com/en/insights/market-data/trillion-dollar-app-economy-only-beginning/ 1https://www.statista.com/statistics/330695/number-of-smartphone-users-worldwide/ © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
  9. 9. © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved. logo book title book author 55 paperback hardcover $1015 $1425 only 2 left in stock only 16 left in stock logo book title book author 55 paperback hardcover $1015 $1425 only 2 left in stock only 16 left in stock
  10. 10. http:// Desktop app Alexa skill Mobile app http:// © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved. logo book title book author 55 paperback hardcover $1015 $1425 only 2 left in stock only 16 left in stock logo book title book author 55 paperback hardcover $1015 $1425 only 2 left in stock only 16 left in stock
  11. 11. A new philosophy for APIs © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
  12. 12. App data challenges Data requirements vary across devices and become harder when multiple users share data Users want instant access to data Building scalable data- driven apps without learning distributed systems concepts is hard Users want to continue using their apps even with low or no connectivity © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
  13. 13. Why is GraphQL compelling? © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
  14. 14. Traditional data fetching Data ü Trivial to set up ü Standard HTTP Calls Relationships Lists with reduced information Query support Ordering and pagination Notifications /posts /postInfo /postJustTitle /postsByAuthor /postNameStartsX /postByTag /commentsOnPost REST Endpoints © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
  15. 15. logo book title book author 55 paperback hardcover $1015 $1425 only 2 left in stock only 16 left in stock logo book title book author 55 paperback hardcover $1015 $1425 only 2 left in stock only 16 left in stock http:// Books Reviews Inventory © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
  16. 16. http:// type Book { uuid: ID title: String in_stock: Int price: Float ... } getBook(uuid: "123") © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved. logo book title book author 55 paperback hardcover $1015 $1425 only 2 left in stock only 16 left in stock
  17. 17. Type system © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
  18. 18. only 6 left in stock get it today only 3 left in stock only 2 left in stock book title book title logo logo ! search(q="book") return ("123", "456", "789"...) getBook("123") return ("123", "book title", "author", ["123a", "123b"]) getInventory("123a") return ("123", 2) getReviews("123") return ("4.4", 55) getInventory("123b") return ("123", 16) getBook("456") return ("456", "book title", "author", ["456a" ,"456b"]) getInventory("456a") return ("456", -1) getReviews("456") return ("456", "4.3", 54) getInventory("456b") return ("456", 11) only 6 left in stock get it today only 3 left in stock only 2 left in stock book title book title © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved. logo logo
  19. 19. underfetching © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
  20. 20. ! search(q="book") results: [ book: { uuid: "123", title: "book title", author: "author", review: { rating: 4.4, count: 55 }, items: [ { uuid: "123a", remaining: 2 }, { uuid: "123b", remaining: 16 } ]}, book: { uuid: "456", title: "book title", author: "author", review: { rating: 4.3, count: 54 }, items: [ { uuid: "456a", remaining: -1 }, { uuid: "456b", remaining: 11 } ]} ] © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
  21. 21. ! search(q="book") results: [ book: { uuid: "123", title: "book title", author: "author", review: { rating: 4.4, count: 55 }, items: [ { uuid: "123a", remaining: 2 }, { uuid: "123b", remaining: 16 } ]}, book: { uuid: "456", title: "book title", author: "author", review: { rating: 4.3, count: 54 }, items: [ { uuid: "456a", remaining: -1 }, { uuid: "456b", remaining: 11 } ]} ] © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
  22. 22. ! mobile_search(q="book") results: [ book: { uuid: "123", title: "book title", author: "author", review: { rating: 4.4, count: 55 }, uuids: [ "123a", "123b" ] }, book: { uuid: "456", title: "book title", author: "author", review: { rating: 4.3, count: 54 }, uuids: [ "456a", "456b" ] } ] © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
  23. 23. overfetching © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
  24. 24. What we want •Single endpoint to access data •One request •Only the fields that are asked "exactfetching" © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
  25. 25. Type system GraphQL Queries © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
  26. 26. title of review lorem ipsum content of the review. this is a great book! Helpful Not helpful 6 people found this helpful Reviews query { reviews { helpful } } POST /reviews/r1/helpful { "helpful": 7 } © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
  27. 27. title of review lorem ipsum content of the review. this is a great book! Helpful Not helpful 6 people found this helpful Reviews mutation { markReview(isHelpful: true) { helpful } } { "helpful": 7 } © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
  28. 28. Type system GraphQL Queries GraphQL Mutations © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
  29. 29. Reviews reviews { count rating } © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
  30. 30. Reviews reviews { count rating } reviews { count rating } reviews { count rating } reviews { count rating } reviews { count rating } © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
  31. 31. Reviews subscribe { reviews(uuid:"123") { count rating } } persistent connection { count: 55, rating: 4.4 } { count: 56, rating: 4.5 } © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
  32. 32. Type system GraphQL Queries GraphQL Mutations GraphQL Subscriptions © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
  33. 33. Open specification http://facebook.github.io/graphql/draft/ © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
  34. 34. Features of GraphQL • Strongly typed • Code generation and introspection • Queries, mutations, subscriptions • Transport agnostic • Client-specified shape of response • Efficient © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
  35. 35. Build a GraphQL API © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
  36. 36. uuid title author created A123 101 Dalmatians D. Smith 20050101 B456 A Tale of Two Kitties S. Kelly 20060101 http:// [ { id: "r1", stars: 5, uuid: "A123", txt: "Spots everywhere!" }, { id: "r2", stars: 4, uuid: "B456", txt: "Purr-fect book!" }, ] Desktop app Alexa skill Mobile app uuid qty A123 10 B456 25 Books Reviews Inventory © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
  37. 37. Alexa skill uuid qty A123 10 B456 25 Books Inventory "Alexa, is <book> available?" uuid title author created A123 101 Dalmatians D. Smith 20050101 B456 A Tale of Two Kitties S. Kelly 20060101 © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
  38. 38. http:// [ { id: "r1", stars: 5, uuid: "A123", txt: "Spots everywhere!" }, { id: "r2", stars: 4, uuid: "B456", txt: "Purr-fect book!" }, ] Desktop app uuid qty A123 10 B456 25 Books Reviews Inventory Search books View reviews Manage inventory Manage reviews uuid title author created A123 101 Dalmatians D. Smith 20050101 B456 A Tale of Two Kitties S. Kelly 20060101 © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
  39. 39. http:// [ { id: "r1", stars: 5, uuid: "A123", txt: "Spots everywhere!" }, { id: "r2", stars: 4, uuid: "B456", txt: "Purr-fect book!" }, ] Mobile app Books Reviews Search books View reviews uuid title author created A123 101 Dalmatians D. Smith 20050101 B456 A Tale of Two Kitties S. Kelly 20060101 © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
  40. 40. uuid title author created A123 101 Dalmatians D. Smith 20150101 type Book { } © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
  41. 41. type Book { } uuid title author created A123 101 Dalmatians D. Smith 20150101 © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
  42. 42. type Book { } uuid title author created A123 101 Dalmatians D. Smith 20150101 © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
  43. 43. type Book { uuid: ID } uuid title author created A123 101 Dalmatians D. Smith 20150101 © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
  44. 44. type Book { uuid: ID } uuid title author created A123 101 Dalmatians D. Smith 20150101 © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
  45. 45. uuid title author created A123 101 Dalmatians D. Smith 20150101 type Book { uuid: ID } © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
  46. 46. uuid title author created A123 101 Dalmatians D. Smith 20150101 type Book { uuid: ID title: String author: String } © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
  47. 47. type Review { id: ID uuid: String stars: Int txt: String } [ { id: "r1", stars: 5, uuid: "A123", txt: "Spots everywhere!" }, { id: "r2", stars: 4, uuid: "B456", txt: "Purr-fect book!" }, ] http:// © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
  48. 48. type Inventory { uuid: ID qty: Int } uuid qty A123 10 B456 25 © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
  49. 49. http:// [ { id: "r1", stars: 5, uuid: "A123", txt: "Spots everywhere!" }, { id: "r2", stars: 4, uuid: "B456", txt: "Purr-fect book!" }, ] uuid qty A123 10 B456 25 Books Reviews Inventory type Book { uuid: ID title: String author: String } type Review { id: ID uuid: String stars: Int txt: String } type Inventory { uuid: ID qty: Int } uuid title author created A123 101 Dalmatians D. Smith 20050101 B456 A Tale of Two Kitties S. Kelly 20060101 © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
  50. 50. http:// [ { id: "r1", stars: 5, uuid: "A123", txt: "Spots everywhere!" }, { id: "r2", stars: 4, uuid: "B456", txt: "Purr-fect book!" }, ] uuid qty A123 10 B456 25 Books Reviews Inventory type Review { id: ID uuid: String stars: Int txt: String } type Inventory { uuid: ID qty: Int } uuid title author created A123 101 Dalmatians D. Smith 20050101 B456 A Tale of Two Kitties S. Kelly 20060101 type Book { uuid: ID title: String author: String reviews: [Review] } © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
  51. 51. http:// [ { id: "r1", stars: 5, uuid: "A123", txt: "Spots everywhere!" }, { id: "r2", stars: 4, uuid: "B456", txt: "Purr-fect book!" }, ] uuid qty A123 10 B456 25 Books Reviews Inventory type Review { id: ID uuid: String stars: Int txt: String } type Inventory { uuid: ID qty: Int } type Book { uuid: ID title: String author: String reviews: [Review] inventory: Inventory } uuid title author created A123 101 Dalmatians D. Smith 20050101 B456 A Tale of Two Kitties S. Kelly 20060101 © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
  52. 52. Reading data ! © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
  53. 53. type MyQuery { search(q: String): [Book] getBook(uuid: ID): Book } type Book { uuid: ID title: String author: String reviews: [Review] inventory: Inventory } type Review { id: ID uuid: String stars: Int txt: String } type Inventory { uuid: ID qty: Int } © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
  54. 54. search(q: "tale") { uuid title } type Book { uuid: ID title: String author: String reviews: [Review] inventory: Inventory } type Review { id: ID uuid: String stars: Int txt: String } type Inventory { uuid: ID qty: Int } type MyQuery { search(q: String): [Book] getBook(uuid: ID): Book } © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
  55. 55. search(q: "tale") { uuid title } type Book { uuid: ID title: String author: String reviews: [Review] inventory: Inventory } type Review { id: ID uuid: String stars: Int txt: String } type Inventory { uuid: ID qty: Int } type MyQuery { search(q: String): [Book] getBook(uuid: ID): Book } [ { "uuid": "B456" "title": "A Tale of Two Kitties" } ] © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
  56. 56. getBook(uuid: "A123") { uuid title reviews { id txt stars } inventory { qty } } type Book { uuid: ID title: String author: String reviews: [Review] inventory: Inventory } type Review { id: ID uuid: String stars: Int txt: String } type Inventory { uuid: ID qty: Int } type MyQuery { search(q: String): [Book] getBook(uuid: ID): Book } © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
  57. 57. { "uuid": "A123", "title": "101 Dalmatians", "reviews": [ id: "r1", txt: "Spots everywhere!", stars: 5 ], "inventory" { "qty": 10 } } type Book { uuid: ID title: String author: String reviews: [Review] inventory: Inventory } type Review { id: ID uuid: String stars: Int txt: String } type Inventory { uuid: ID qty: Int } type MyQuery { search(q: String): [Book] getBook(uuid: ID): Book } © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
  58. 58. Writing data ! © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
  59. 59. type MyMutation { addStar(num: Int): Review } type Book { uuid: ID title: String author: String reviews: [Review] inventory: Inventory } type Review { id: ID uuid: String stars: Int txt: String } type Inventory { uuid: ID qty: Int } type MyQuery { search(q: String): [Book] getBook(uuid: ID): Book } © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
  60. 60. addStar(num: 1) { stars } type Book { uuid: ID title: String author: String reviews: [Review] inventory: Inventory } type Review { id: ID uuid: String stars: Int txt: String } type Inventory { uuid: ID qty: Int } type MyQuery { search(q: String): [Book] getBook(uuid: ID): Book } type MyMutation { addStar(num: Int): Review } © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
  61. 61. addStar(num: 1) { stars } type Book { uuid: ID title: String author: String reviews: [Review] inventory: Inventory } type Review { id: ID uuid: String stars: Int txt: String } type Inventory { uuid: ID qty: Int } type MyQuery { search(q: String): [Book] getBook(uuid: ID): Book } type MyMutation { addStar(num: Int): Review } { stars: 55 } © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
  62. 62. Listening for data ! © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
  63. 63. type MySubscription { onReview(uuid: ID): Review } type Book { uuid: ID title: String author: String reviews: [Review] inventory: Inventory } type Review { id: ID uuid: String stars: Int txt: String } type Inventory { uuid: ID qty: Int } type MyQuery { search(q: String): [Book] getBook(uuid: ID): Book } type MyMutation { addStar(num: Int): Review } © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
  64. 64. onReview(uuid: "A123") { uuid id stars } type Book { uuid: ID title: String author: String reviews: [Review] inventory: Inventory } type Review { id: ID uuid: String stars: Int txt: String } type Inventory { uuid: ID qty: Int } type MyQuery { search(q: String): [Book] getBook(uuid: ID): Book } type MyMutation { addStar(num: Int): Review } type MySubscription { onReview(uuid: ID): Review } © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
  65. 65. onReview(uuid: "A123") { uuid id stars } <listen> type Book { uuid: ID title: String author: String reviews: [Review] inventory: Inventory } type Review { id: ID uuid: String stars: Int txt: String } type Inventory { uuid: ID qty: Int } type MyQuery { search(q: String): [Book] getBook(uuid: ID): Book } type MyMutation { addStar(num: Int): Review } type MySubscription { onReview(uuid: ID): Review } © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
  66. 66. onReview(uuid: "A123") { uuid id stars } { "uuid": "A123", "id": "r576", "stars": 54 } { "uuid": "A123", "id": "r9845", "stars": 55 } type Book { uuid: ID title: String author: String reviews: [Review] inventory: Inventory } type Review { id: ID uuid: String stars: Int txt: String } type Inventory { uuid: ID qty: Int } type MyQuery { search(q: String): [Book] getBook(uuid: ID): Book } type MyMutation { addStar(num: Int): Review } type MySubscription { onReview(uuid: ID): Review } © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
  67. 67. type Book { uuid: ID title: String author: String reviews: [Review] inventory: Inventory } type Review { id: ID uuid: String stars: Int txt: String } type Inventory { uuid: ID qty: Int } type MyQuery { search(q: String): [Book] getBook(uuid: ID): Book } type MyMutation { addStar(num: Int): Review } type MySubscription { onReview(uuid: ID): Review } © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
  68. 68. type Book { uuid: ID title: String author: String reviews: [Review] inventory: Inventory } type Review { id: ID uuid: String stars: Int txt: String } type Inventory { uuid: ID qty: Int } schema { query: MyQuery mutation: MyMutation subscription: MySubscription } type MyQuery { search(q: String): [Book] getBook(uuid: ID): Book } type MyMutation { addStar(num: Int): Review } type MySubscription { onReview(uuid: ID): Review } © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
  69. 69. http:// [ { id: "r1", stars: 5, uuid: "A123", txt: "Spots everywhere!" }, { id: "r2", stars: 4, uuid: "B456", txt: "Purr-fect book!" }, ] Desktop app Alexa skill Mobile app uuid qty A123 10 B456 25 Books Reviews Inventory schema uuid title author created A123 101 Dalmatians D. Smith 20050101 B456 A Tale of Two Kitties S. Kelly 20060101 © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
  70. 70. How are queries executed? © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
  71. 71. type MyQuery { getBook(uuid: ID): Book } Books ? © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
  72. 72. type MyQuery { getBook(uuid: ID): Book } Books resolver f(x) © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
  73. 73. type MyQuery { getBook(uuid: ID): Book } Books var client = new DocumentClient(); var params = { TableName: "Books" }; client.getItem(params, onItem); function onItem(err, data) { if (data) { //... } } resolver f(x) © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
  74. 74. http:// Books Reviews Inventory type MyQuery { getBook(uuid: ID): Book } GetBookResolver © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
  75. 75. type MyQuery { getBook(uuid: ID): Book } http:// Books Reviews Inventory GetBookResolver type Book { uuid: ID title: String author: String reviews: [Review] inventory: Inventory } © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
  76. 76. type Book { uuid: ID title: String author: String reviews: [Review] inventory: Inventory } type MyQuery { getBook(uuid: ID): Book } http:// Books Reviews Inventory GetBookResolver GetReviewsResolver © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
  77. 77. type MyQuery { getBook(uuid: ID): Book } http:// Books Reviews Inventory GetBookResolver GetReviewsResolver GetInventoryResolver type Book { uuid: ID title: String author: String reviews: [Review] inventory: Inventory } © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
  78. 78. getBook(uuid: "A123") { uuid title reviews { id txt stars } inventory { qty } } getBook uuid title inventory qty reviews id txt stars © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
  79. 79. getBook uuid title inventory qty reviews id txt stars GetBookResolver © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
  80. 80. getBook uuid title inventory qty reviews id txt stars GetBookResolver GetReviewsResolver GetInventoryResolver © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
  81. 81. getBook uuid title inventory qty reviews id txt stars GetBookResolver GetReviewsResolver GetInventoryResolver © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
  82. 82. getBook(uuid: "A123") { uuid title inventory reviews } getBook uuid title © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
  83. 83. http:// [ { id: "r1", stars: 5, uuid: "A123", txt: "Spots everywhere!" }, { id: "r2", stars: 4, uuid: "B456", txt: "Purr-fect book!" }, ] Desktop app Alexa skill Mobile app uuid qty A123 10 B456 25 Books Reviews Inventory schema f(x) f(x) f(x) f(x) uuid title author created A123 101 Dalmatians D. Smith 20050101 B456 A Tale of Two Kitties S. Kelly 20060101 © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
  84. 84. How do I Run GraphQL in production © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
  85. 85. © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved. GraphQL sounds legit! I want to run my own GraphQL server!!
  86. 86. endpoint auth Security f(x) © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
  87. 87. resolver auth Security f(x) © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
  88. 88. getBook(uuid: "A123") { uuid title qty } Security { uuid: "A123" title: "101 Dalmatians" qty: null } © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
  89. 89. Nested queries query { friends(id: "123") { id name friends { id name friends { id name friends { ... } } } } } © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
  90. 90. Query Execution •! Bounding • Throttling •" Batching getBook(uuid: "A123") { uuid title reviews { id txt stars } price { currency } inventory { qty } } http:// © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
  91. 91. f(x) Scaling subscriptions … © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
  92. 92. © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Focus on Apps Not Infrastructure
  93. 93. AWS AppSync – managed GraphQL http:// https://aws.amazon.com/appsync © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
  94. 94. AWS AppSync © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Managed Serverless GraphQL service Connect to data sources in your account Add data sync, real-time and offline capabilities for any data source or API GraphQL façade for any AWS service Conflict detection and resolution in the cloud Enterprise security features: IAM, Cognito, OIDC, API keys
  95. 95. Mix/Match Data Sources on GraphQL Types Amazon DynamoDB type Query { listPosts: [Post] searchPosts: [Post] } type Mutation { addPost: Post } type Post { id: ID! content: String description: String ups: Int downs: Int } Amazon Elasticsearch listPosts searchPosts addPost
  96. 96. AWS AppSync benefits © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Clients receive the data they ask for. Nothing more, nothing less Get many resources from many data sources with a single request Self-documenting APIs with Introspection React Native, Android, iOS, and Web (JS) using the Apollo GraphQL client Data persistence across application restarts Write-through mutations with Optimistic UI
  97. 97. How does AppSync work? © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Amazon Elasticsearch Amazon DynamoDB AWS Lambda Connect Data Sources AppSync Updates Data in Real Time and Manages Data when Offline __________ ______________ ___________ _________ ___________ ____________ __________ NEW Create, Import or Upload GraphQL Schema
  98. 98. Data flow and security © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
  99. 99. Request Template Response Template Data Sources 1 2 3 4 5 GraphQL data flow in AWS AppSync
  100. 100. Fine-grained access control
  101. 101. Simple Only users in certain groups can perform actions #if($hasPermission || $ctx.result.public == 'yes') $utils.toJson($ctx.result) #else $utils.unauthorized() #end Only users in certain groups can perform actions #if($ctx.result["Owner"] == $ctx.identity.username) $utils.toJson($context.result); #else $utils.unauthorized() #end Advanced Only users in certain groups can perform actions #foreach($group in $ctx.identity.claims.get("cognito:groups")) #if($group == "Admin") #set($inCognitoGroup = true) #end #end #if($inCognitoGroup) { …write/read from datasource } #else $utils.unauthorized() #end Access control checks
  102. 102. Real-time data © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
  103. 103. Amazon Elasticsearch Amazon DynamoDB Browser Mobile device WebSocket servers Web servers PubSub servers Subscription /search /taps /taps/:id /m_search AWS Lambda Third-party service … Real-time app before AppSync
  104. 104. /graphql AWS AppSync Subscription Amazon Elasticsearch Amazon DynamoDB AWS Lambda Third-party service Real-time app with AppSync
  105. 105. • GraphQL subscriptions • Event stream of mutation results • Data synchronisation with MQTT + WebSockets • Client managed WebSocket connection • Automatic catch-up snapshots Real-time updates
  106. 106. Offline data and conflicts © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
  107. 107. Jane Version : 2 Updated Document Jane Version : 2 Updated Document Version : 3 Updated Document Version : 1 New Document Time John John Jane goes offline Jane comes back online Version : 4 Updated Document John Jane Data conflicts
  108. 108. • Conflict detection with optimistic version check • Conflict resolution strategies: • Client wins • Server wins • Silent reject • Custom logic in AWS Lambda • Client callback for conflict resolution is available "condition" : { "expression" : "someExpression" "conditionalCheckFailedHandler" : { "strategy" : "Custom", "lambdaArn" : "arn:..." } } Handle data conflicts in the cloud
  109. 109. • Offline is a write-through "Store" • Persistent storage mediums back the Apollo normalised cache • Local Storage for web • AsyncStorage for React Native • SQLite on native platforms • Database can be preloaded • Offline client can be configured • WiFi only • WiFi and cellular Offline capabilities – AppSync offline storage
  110. 110. AWS Amplify CLI
  111. 111. Amazon DynamoDB type Post @model @auth(rules: [{allow: owner}]) @searchable{ id: ID! content: String description: String ups: Int downs: Int } Amazon Elasticsearch searchPosts Amazon Cognito User Pools mutations queries createPost readPost updatePost deletePost AWS AppSync GraphQL transformer
  112. 112. Let's build a GraphQL API with AWS AppSync © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
  113. 113. Use GraphQL Use REST When data drives UI • Structured Data • Complex Data • Query-driven • Real-time/Offline Client-driven development Pros: Contract-driven, Introspection, Relations, Types Conns: Not as ubiquitous as REST When you leverage HTTP • Caching • Content Types • Hypermedia (HATEOAS) For Resources (e.g. Kinesis) Pros: HTTP Client, Golden Standard, HTTP/2 Performance gains Conns: Over fetching/Under fetching GraphQL or REST?
  114. 114. Bottom Line It depends on the use case and, most importantly… Good API Design! GraphQL or REST?
  115. 115. Recap • What is GraphQL • Why was GraphQL created • How to build a GraphQL API • How to operate a GraphQL API in production • We built a GraphQL API © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
  116. 116. Thank you https://aws.amazon.com/appsync follow me on twitter @appwiz !"# © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.

×