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

The power of Serverless GraphQL with AppSync - AWS Community Day Bosnia 2020

The power of Serverless GraphQL with AppSync - AWS Community Day Bosnia 2020

2663a9ac90cc6ed420e0e1560db57782?s=128

Slobodan Stojanović

September 25, 2020
Tweet

Transcript

  1. he!o! AWS Community Day Bosnia

  2. every story n"ds a hero

  3. None
  4. None
  5. Our hero

  6. Works in a sma! team on a mid-sized application

  7. Most of the time enjoys the job

  8. Just sent a test push notification to thousands of customers

  9. We need to build this new complex app.

  10. Ok, no problem

  11. But we have a short deadline.

  12. Ok, how short?

  13. It needs to be ready for yesterday. And it needs

    to be scalable and real-time!
  14. How should I build this?

  15. Don't worry, we have a famous consultant that will help

    you architecture the app.
  16. Great!

  17. multi-cloud guru

  18. Just use Kubernetes! multi-cloud guru

  19. But I don't know Kubernetes.

  20. I don't have time to learn Kubernetes now.

  21. What should I do?

  22. Am I the only one who don't know Kubernetes?

  23. Maybe I am not good enough for this!

  24. None
  25. Serverless GraphQL You should try

  26. Serverless GraphQL?

  27. Serverless GraphQL with AppSync The Power of

  28. @slobodan_ Before we continue…

  29. Slobodan Stojanović CTO @ Cloud Horizon & CTO @ Vacation

    Tracker co-author of Serverless Applications with Node.js book AWS Serverless Hero @slobodan_
  30. Serverless GraphQL with AppSync The Power of

  31. Serverless GraphQL

  32. Serverless GraphQL ?

  33. What's GraphQL?

  34. @slobodan_ "Our biggest mistake was betting too much on HTML5"

    Mark Zuckerberg, 2012
  35. @slobodan_

  36. @slobodan_ HTML HTML

  37. @slobodan_ "Our biggest mistake was betting too much on HTML5"

    Mark Zuckerberg, 2012
  38. @slobodan_ REST? FQL? REST? FQL?

  39. @slobodan_ "We were frustrated with the differences between the data

    we wanted to use in our apps and the server queries they required." L" Byron, 2015 GraphQL: A data query language article
  40. @slobodan_ Data app needs Data app needs

  41. @slobodan_

  42. Nice. But, why should I care about GraphQL?

  43. I don't have the same problems as Facebook.

  44. Ok, but let's s" if you have one of the

    fo!owing problems
  45. Distinct frontend clients for multiple platforms (i.e., web and mobile)

    has different data requirements.
  46. A backend serves data to clients from different sources.

  47. A complex state and caching managements for both frontend and

    backend.
  48. Slow pages especia!y on mobile, caused by multiple dependant HTTP

    requests.
  49. These are just some "symptoms" that GraphQL can cure.

  50. • Defines a data shape • Hierarchical • Strongly typed

    • Protocol, not storage • Introspective • Version fr"
  51. None
  52. None
  53. Types type Author { id: Int name: String posts: [Post]

    } type Post { id: Int title: String text: String author: Author }
  54. Schema type Query { getAuthor(id: Int): Author getPostsByTitle(titleContains: String): [Post]

    } schema { query: Query }
  55. Resolvers getAuthor(_, args){ return sql.raw('SELECT * FROM authors WHERE id

    = %s', args.id); } posts(author){ return request(`https://api.blog.io/by_author/${author.id}`); }
  56. Queries { getAuthor(id: 5){ name posts { title author {

    # this will be the same as the name above name } } } }
  57. 1. Parse 2. Validate 3. Execute

  58. Result { "name": "Slobodan", "posts": [{ "title": "Hello World", "author":

    { "name": "Slobodan" } }, { "title": "GraphQL", "author": { "name": "Slobodan" } }] }
  59. GraphQL supports: • Queries • Mutations • Subscriptions

  60. Interesting!

  61. Serverless GraphQL

  62. Serverless GraphQL Why?

  63. GraphQL using Kubernetes We!, you can deploy

  64. BUT there's an easier way

  65. You sti! n"d to scale these

  66. Unless you make it serverless

  67. How can I make my GraphQL app serverless?

  68. You can make it manua!y

  69. Or you can use AWS AppSync!

  70. What's AWS AppSync?

  71. AppSync is a managed service that uses GraphQL to make

    it easy for applications to get exactly the data they n"d.
  72. AppSync to develop your app faster You can use

  73. I can? How?

  74. • Define GraphQL schema • Automatically provision a DynamoDB data

    source and connect resolvers • Write GraphQL queries and mutations • Use the API in the frontend app
  75. It's easy to start with Let's give it a try

  76. You can start with the guided schema wizard on AWS

    Web Console
  77. But you should use AWS Amplify, CloudFormation or CDK for

    more serious apps
  78. None
  79. > amplify init > amplify add api > amplify push

  80. Wow, that was fast!

  81. I should show it to the consultant.

  82. That will never work! Amplify is not good enough. multi-cloud

    guru
  83. You can always start with Amplify and then move to

    CloudFormation or CDK
  84. AppSync with CDK import * as cdk from '@aws-cdk/core'; import

    * as appsync from '@aws-cdk/aws-appsync'; export class AppsyncCdkAppStack extends cdk.Stack { constructor(scope: cdk.Construct, id: string, props?: cdk.StackProps) { super(scope, id, props); // Creates the AppSync API const api = new appsync.GraphqlApi(this, 'Api', { name: 'my-awesome-app', schema: appsync.Schema.fromAsset('graphql/schema.graphql'), }); // Prints out the AppSync GraphQL endpoint to the terminal new cdk.CfnOutput(this, "GraphQLAPIURL", { value: api.graphqlUrl }); } }
  85. Ok, but the app needs to be real-time multi-cloud guru

  86. AWS AppSync lets you specify which part of your data

    should be available in a real-time manner using GraphQL Subscriptions.
  87. Real-time subscriptions type Subscription { addedPost: Post @aws_subscribe(mutations: ["addPost"]) updatedPost:

    Post @aws_subscribe(mutations: ["updatePost"]) deletedPost: Post @aws_subscribe(mutations: ["deletePost"]) }
  88. And it needs to be scalable! multi-cloud guru

  89. None
  90. But what if you need a search functionality? multi-cloud guru

  91. AWS AppSync supports Amazon Elasticsearch! the GraphQL operations can be

    simple lookups, complex queries & mappings, fu! text searches, fuzzy/keyword searches or geo lookups.
  92. But what if you need to connect to an existing

    app? multi-cloud guru
  93. You can connect AWS AppSync to AWS Lambda!

  94. What about roles and permissions? multi-cloud guru

  95. • API_KEY Authorization • AWS_IAM Authorization • OPENID_CONNECT Authorization •

    AMAZON_COGNITO_USER_POOLS Authorization
  96. Multi-tenant? multi-cloud guru

  97. You can use Cognito Groups.

  98. use Resolver Mapping Template For fine-grained permissions

  99. ? multi-cloud guru

  100. ?

  101. You can use Apache Velocity Template Language (VTL)

  102. What's VTL?

  103. I would say it’s an alien language (we!, its Java-based)

    hard to test in isolation in a serverless application, especia!y in AWS CloudForamation.
  104. Alien language a.k.a. VTL #if($context.result["Owner"] == $context.identity.username) $utils.toJson($context.result) #else $utils.unauthorized()

    #end
  105. Ha! I don't like that! multi-cloud guru

  106. Me neither!

  107. AppSync supports Direct Lambda Resolvers, which skips VTL completely

  108. But what if you want to reuse some logic? multi-cloud

    guru
  109. You can do that in AWS Lambda or use Pipeline

    Resolvers
  110. Ok, ok, but this is still complex for the frontend

    multi-cloud guru
  111. AWS Amplify gives us nice libraries for the frontend

  112. And it can automatica!y generate queries, mutations, subscriptions and types

    for us!
  113. I give up! multi-cloud guru

  114. But this is not a!!

  115. AppSync supports caching and offline data synchronization

  116. Ok, but how do you test it? multi-cloud guru

  117. It depends on your approach. It's good to have end-to-end

    tests, but you can test your Lambda code or VTL templates in isolation.
  118. You can also run the app loca!y using AWS Amplify

  119. What if we need to use more complex architecture, i.e.

    CQRS multi-cloud guru
  120. Mutation Event storage Subscription Query Read-only table EventBridge Business logic

    Business logic
  121. Mutation Event storage Subscription Query Read-only table EventBridge Business logic

    Business logic
  122. Mutation Event storage Subscription Query Read-only table EventBridge Business logic

    Business logic
  123. multi-cloud guru

  124. None
  125. Serverless GraphQL Guru

  126. None
  127. Quick Summary: • GraphQL makes your frontend and backend connection

    effortless • AppSync makes GraphQL management effortless • Serverless GraphQL makes you a super hero
  128. @slobodan_ vacationtracker.io