AWS Summit Berlin.key - Amazon AWS

Traditional Backend. Monolith/. Microservices. Database. API + DB + LOCIC LAYER. Page 25. Serverless GraphQL Backend. GraphQL Backend. API LAYER.
1MB Sizes 7 Downloads 270 Views
Serverless GraphQL Applications with Lambda

Johannes Schickling



Based in Berlin/San Francisco



@graphcool (Serverless GraphQL Platform)

@_schickling @schickling



Love cutting-edge technology 🤖



Introduction to GraphQL



Architecture & Concepts



Live Demo



Resources

Introduction to GraphQL

Use Case: Webshop •

Each Item has the following fields: •

id: ID



title: String



price: Float



ratingInfo: RatingInfo

{ "id": "cj1s966c8hudb0139bg5qjmna", "price": 2500, "title": "Macbook Pro 2016", "ratingInfo": null }

Status Quo: RESTful APIs

GET https://api.shop.com/items

[{ "id": "cj1s966c8hudb0139bg5qjmna", "createdAt": "2017-04-21T19:52:37.000Z", "updatedAt": "2017-04-21T19:52:50.000Z", "price": 2500, "title": "Macbook Pro 2016" }, { "id": "cj1s96cpohufg0139keoewq22", "createdAt": "2017-04-21T19:52:46.000Z", "updatedAt": "2017-04-21T19:53:28.000Z", "price": 800, "title": "iPhone" }, { "id": "cj1s97mpwhuo10139salvx5wn", "createdAt": "2017-04-21T19:53:45.000Z", "updatedAt": "2017-04-21T19:53:45.000Z", "price": 1700, "title": "Macbook Air 2015" }]

Related data?

Problem: Related data?



Strategy 1: Nested endpoints

Problem: Related data?



Strategy 1: Nested endpoints Data overfetching

Problem: Related data?



Strategy 1: Nested endpoints Data overfetching



Strategy 2: Extra endpoint

Problem: Related data?



Strategy 1: Nested endpoints Data overfetching



Strategy 2: Extra endpoint N+1 query problem

Solution: GraphQL “A query language for your API”

GraphQL Query { "data": { "allItems": [ { "title": "iPhone 6", "ratingInfo": { "count": 115, "averageRating": 4.7 } }, { "title": "Macbook 2016", "ratingInfo": { "count": 29, "averageRating": 4.9 } }, { "title": "Macbook 2015", "ratingInfo": { "count": 235, "averageRating": 4.8 } } ] }

query { allItems { title ratingInfo { count averageRating } } }

}

GraphQL Mutation mutation { createItem( title: "iPhone 7" price: 900 ){ id } }

{ "data": { "createItem": { "id": "cj1sbo2phrmpq0124t63uyo5f" } } }

Demo



GraphQL IDL •

Describes data model



Syntax: GraphQL IDL



Typesystem

type Item { id: ID! @isUnique createdAt: DateTime! updatedAt: DateTime! price: Float! title: String! ratingInfo: RatingInfo! @relation(name: "ItemRatingInfo") } type RatingInfo { id: ID! @isUnique createdAt: DateTime! updatedAt: DateTime! count: Int! averageRating: Float! item: Item! @relation(name: "ItemRatingInfo") }

Architecture & Concepts

History

History

History

History

History

Traditional Backend API + DB + LOCIC LAYER

Monolith/ Microservices

Database

Serverless GraphQL Backend API LAYER

LOGIC LAYER

Events

GraphQL Backend

Business Logic

Serverless GraphQL Backend Architecture 1. GraphQL API for frontend & backend applications 2. Event-driven business logic 1.

Asynchronous events

2.

Synchronous data transformation

3. Global type safety based on GraphQL

Graphcool •

Serverless GraphQL backend



Turns GraphQL schema into a production-ready backend



Event-driven

Request Pipeline Schema Validation

TRANSFORM _REQUEST

Data Validation

TRANSFORM _ARGUMENTS

PRE_WRITE

Data Write

TRANSFORM _PAYLOAD

TRANSFORM _RESPONSE

Demo-App Overview

Webshop

Graphcool

Serverless

3rd-party services

Live Demo

Goal: Ordering process

Order validation: * Check availability * Check totals match

Charge credit card

Save to database

What we’ll do 1. Setup GraphQL API 2. Create some test data 3. Deploy functions 4. Configure events 5. Test run

What we’ll do 1. Setup GraphQL API 2. Create some test data 3. Deploy functions 4. Configure events 5. Test run

Resources

GraphQL & Serverless •

https://github.com/graphcool-examples/serverless-webshop



https://serverless.com/blog/2017-04-5-build-webshop-with-graphqland-serverless/

slack.graph.cool

Thank you