Slide 1

Slide 1 text

Putting REST to rest with GraphQL RubyConf MY - 26th October, 2018

Slide 2

Slide 2 text

Using and optimising GraphQL

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

No content

Slide 5

Slide 5 text

We started out in early 2015

Slide 6

Slide 6 text

… and our API journey began with REST

Slide 7

Slide 7 text

A typical REST endpoint

Slide 8

Slide 8 text

… and it’s serializer

Slide 9

Slide 9 text

As we grew, so did our APIs and their clients

Slide 10

Slide 10 text

API Server The same resource served a different purpose

Slide 11

Slide 11 text

So we started creating client-specific serializers

Slide 12

Slide 12 text

...and allowing clients to specify parameters

Slide 13

Slide 13 text

/products?params=details /products?params=name¶ms=price API Server This allowed clients to ask for certain attributes /products /products/1/receipt

Slide 14

Slide 14 text

Clients Server … but neither were happy

Slide 15

Slide 15 text

Clients often did not have the fields they needed

Slide 16

Slide 16 text

Server was being overloaded with fields no client was using

Slide 17

Slide 17 text

So we started exploring GraphQL

Slide 18

Slide 18 text

Setting up GraphQL was easy honestbee/graphqlapp

Slide 19

Slide 19 text

We created a Rails-GraphQL app Table Customer Item RESTAURANT APP

Slide 20

Slide 20 text

… and experimented with our code organization Stores a collection of related values e.g. statuses Fields are similar to functions that can accept inputs, resolvers, and return values Reusable container for field logic that can be re-used across many fields Key-value inputs for fields Collection of types which implement the some of the same fields GraphQL::Batch classes to batch database operations in a single query GraphQL middleware Commands that change the state of a system Definitions to expose fields on an object

Slide 21

Slide 21 text

GraphQL did make the clients happy by... having a strongly typed schema (living documentation) clients could specify exactly what they needed clients could fetch all the data with a single request

Slide 22

Slide 22 text

Clients Server … but the server was still unhappy

Slide 23

Slide 23 text

GraphQL is not a silver bullet

Slide 24

Slide 24 text

GraphQL’s additional flexibility comes at a cost - Queries can be large, especially when compared to REST - Queries can be constructed maliciously - Queries can be resource intensive

Slide 25

Slide 25 text

These issues can be improved by practicing caution

Slide 26

Slide 26 text

Queries can be limited Limit complexity Limit nesting Limit amount

Slide 27

Slide 27 text

… and whitelisted - APIs cannot be opened to public - Queries for outdated clients need to be kept

Slide 28

Slide 28 text

Sometimes in life, you need to pick your battles

Slide 29

Slide 29 text

… and we chose to optimize for the health of our database

Slide 30

Slide 30 text

Problem

Slide 31

Slide 31 text

Problem

Slide 32

Slide 32 text

The classic N+1 problem

Slide 33

Slide 33 text

N+1 Problem It's the problem caused when you need to make N + 1 SQL Queries, where N is the number of items.

Slide 34

Slide 34 text

N+1 in graphQL

Slide 35

Slide 35 text

By default, each association is lazily loaded table get all customers customer customer customer get 1 item item get 1 item item get 1 item item

Slide 36

Slide 36 text

N+1 in REST In REST, it is simpler because for each endpoint, you know exactly what the application needs and load only the resources that are needed Customer.includes(:items).where(ids: [...])

Slide 37

Slide 37 text

N+1 in graphQL - We don't know what is needed at the time of writing code as the client can ask for anything they want. - We can't load everything all the time, as it puts unnecessary load on the server and database Customer.includes(????????) Customer.includes(:items) # for every request

Slide 38

Slide 38 text

Solution?

Slide 39

Slide 39 text

graphql-batch github.com/Shopify/graphql-batch (A query batching executor for the graphql gem)

Slide 40

Slide 40 text

table get customers customer customer customer item item item graphQL-batch key1 key2 key3 get items using key1,2,3 How graphql-batch helps?

Slide 41

Slide 41 text

Customer Item 1 * Load items for a list of customers

Slide 42

Slide 42 text

Loader

Slide 43

Slide 43 text

Resolver

Slide 44

Slide 44 text

Result

Slide 45

Slide 45 text

Simple filters

Slide 46

Slide 46 text

Current loader

Slide 47

Slide 47 text

Add “where” param to loader

Slide 48

Slide 48 text

Use “where” param

Slide 49

Slide 49 text

Use “where” param

Slide 50

Slide 50 text

Use “where” param

Slide 51

Slide 51 text

No content

Slide 52

Slide 52 text

Complex Queries SORTING JOIN TABLE AGGREGATION LIKE “%text%” QUERIES GREATER / LESSER comparisons

Slide 53

Slide 53 text

The simple “where” param is not enough

Slide 54

Slide 54 text

Pass in ActiveRecord::Relation as param

Slide 55

Slide 55 text

Pass in ActiveRecord::Relation as param

Slide 56

Slide 56 text

Result

Slide 57

Slide 57 text

Result

Slide 58

Slide 58 text

N+1 issue starts again!!!

Slide 59

Slide 59 text

How does graphql-batch do grouping? customer customer customer item item item graphQL-batch key1 key2 key3 get items using key1,2,3

Slide 60

Slide 60 text

What did we change? # Old # New

Slide 61

Slide 61 text

We read all the source code!!!

Slide 62

Slide 62 text

How does graphql-batch do grouping? customer customer customer item item item graphQL-batch key1 key2 key3 get items using key1,2,3

Slide 63

Slide 63 text

Pass in ActiveRecord::Relation as param

Slide 64

Slide 64 text

Solution 1: make the query object the true same object QueryFactory Resolver graphql-batch query args same query object same query object

Slide 65

Slide 65 text

Query Factory

Slide 66

Slide 66 text

Query Factory

Slide 67

Slide 67 text

Query Factory

Slide 68

Slide 68 text

Query Factory - Implicit behaviour - Hard to enforce developers to use the QueryFactory 100% of the times

Slide 69

Slide 69 text

Solution 2: Overwrite the grouping function GraphQL::Batch::Loader (Parent method) Loaders::CustomValueLoader < GraphQL::Batch::Loader (New method)

Slide 70

Slide 70 text

Overwrite the grouping function

Slide 71

Slide 71 text

Overwrite the grouping function

Slide 72

Slide 72 text

Overwrite the grouping function

Slide 73

Slide 73 text

Overwrite the grouping function - Hacky (change default behaviour of the gem) - Hard for new members to understand

Slide 74

Slide 74 text

Solution 3: Let’s graphql-batch call QueryFactory QueryFactory Resolver graphql-batch query args - Require all query services to have standardized contracts - Suitable for newly created projects model model & query args Query service

Slide 75

Slide 75 text

Takeaways - graphQL is a good alternative of REST - Always think about performance when writing resolvers

Slide 76

Slide 76 text

Thank you! ankitagupta12 SeanNguyen