thinking in graphs - graphql problems and more - maciej rybaniec (23.06.2017)

60
Thinking in Graphs GraphQL problems and more Maciej Rybaniec

Upload: grand-parade-poland

Post on 21-Jan-2018

117 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: Thinking in Graphs - GraphQL problems and more - Maciej Rybaniec (23.06.2017)

Thinking in GraphsGraphQL problems and more

Maciej Rybaniec

Page 2: Thinking in Graphs - GraphQL problems and more - Maciej Rybaniec (23.06.2017)

Plan

1) GraphQL

2) Problemy związane z GraphQL

3) Nowe narzędzia

4) Relay Modern

Page 3: Thinking in Graphs - GraphQL problems and more - Maciej Rybaniec (23.06.2017)

GraphQL

Page 4: Thinking in Graphs - GraphQL problems and more - Maciej Rybaniec (23.06.2017)

GraphQL misconceptions

NOT library or storage system

NOT tied with specific language

NOT tied with specific framework

NOT required specific storage system

Page 5: Thinking in Graphs - GraphQL problems and more - Maciej Rybaniec (23.06.2017)

How it works ?

GraphQL Schema

What I can fetch ?

Client

Back-end Databases, APIs, Services

MongoDB Redis Facebook API

Page 6: Thinking in Graphs - GraphQL problems and more - Maciej Rybaniec (23.06.2017)

Sposób komunikacji z API

Page 7: Thinking in Graphs - GraphQL problems and more - Maciej Rybaniec (23.06.2017)

Podstawowe cele

Page 8: Thinking in Graphs - GraphQL problems and more - Maciej Rybaniec (23.06.2017)

No Overfetching

Page 9: Thinking in Graphs - GraphQL problems and more - Maciej Rybaniec (23.06.2017)

Be Declarative

Page 10: Thinking in Graphs - GraphQL problems and more - Maciej Rybaniec (23.06.2017)

Product Development

Autonomy

Page 11: Thinking in Graphs - GraphQL problems and more - Maciej Rybaniec (23.06.2017)

Versioning Support

Page 12: Thinking in Graphs - GraphQL problems and more - Maciej Rybaniec (23.06.2017)

Podstawoweoperacje

Page 13: Thinking in Graphs - GraphQL problems and more - Maciej Rybaniec (23.06.2017)

Queries

Query Response

Allows to fetch data in a declarative way

Page 14: Thinking in Graphs - GraphQL problems and more - Maciej Rybaniec (23.06.2017)

QueryA GraphQL query is just an instruction for traversing the graph in a specific way, resulting in a tree.

Page 15: Thinking in Graphs - GraphQL problems and more - Maciej Rybaniec (23.06.2017)

Mutations

Mutation Response

Allows to mutate data in server and fetch new state after change

Page 16: Thinking in Graphs - GraphQL problems and more - Maciej Rybaniec (23.06.2017)

Problemy

Page 17: Thinking in Graphs - GraphQL problems and more - Maciej Rybaniec (23.06.2017)

Best practices

Page 18: Thinking in Graphs - GraphQL problems and more - Maciej Rybaniec (23.06.2017)

Single endpoint

Page 19: Thinking in Graphs - GraphQL problems and more - Maciej Rybaniec (23.06.2017)

Authorization

Obsługa dostępu w przypadku

zagnieżdżonych relacji

Page 20: Thinking in Graphs - GraphQL problems and more - Maciej Rybaniec (23.06.2017)

Error handling

Obsługa błędów w przypadku gdy

zapytania mogą być złożone

Page 21: Thinking in Graphs - GraphQL problems and more - Maciej Rybaniec (23.06.2017)

REST

HTTP Request

Warstwa danych

Middleware

Route Controller

Użytkownik

Page 22: Thinking in Graphs - GraphQL problems and more - Maciej Rybaniec (23.06.2017)

GraphQLNo middleware and afterware

Resolvers – responsible for handling portions of a request

Page 23: Thinking in Graphs - GraphQL problems and more - Maciej Rybaniec (23.06.2017)

Resolvers

Sample field resolver

Deliver data based on contract

Can contain arbitrary code

Page 24: Thinking in Graphs - GraphQL problems and more - Maciej Rybaniec (23.06.2017)

Problem definition

Page 25: Thinking in Graphs - GraphQL problems and more - Maciej Rybaniec (23.06.2017)

Handle permissions on edges.

Basic Solution

Page 26: Thinking in Graphs - GraphQL problems and more - Maciej Rybaniec (23.06.2017)

Multiple edges can lead to a particular node

Permissions on edges

Page 27: Thinking in Graphs - GraphQL problems and more - Maciej Rybaniec (23.06.2017)

Authorization logic in nodes – one function per node.

1) Called by every resolve function of a field that leads to a node of specific type.

2) Called in every field on the specific type before returning anything.

Permissions on nodes

Page 28: Thinking in Graphs - GraphQL problems and more - Maciej Rybaniec (23.06.2017)

Delegate authorization logic to the business logic layer

Facebook Solution

Sample authorization

Bad practice

Page 29: Thinking in Graphs - GraphQL problems and more - Maciej Rybaniec (23.06.2017)

Single source of truthBusiness logic layer should act as the single source

of truth for enforcing business domain rules.

Page 30: Thinking in Graphs - GraphQL problems and more - Maciej Rybaniec (23.06.2017)

1) Move authorization responsibility toresolvers.

2) Client can understand errors without proper status code.

3) Masked Internal errors.

4) DRY

Apollo Solution

Page 31: Thinking in Graphs - GraphQL problems and more - Maciej Rybaniec (23.06.2017)

Abstract Resolvers

Base root resolver

Chain of resolvers to satisfy individual parts of the overall problem

Page 32: Thinking in Graphs - GraphQL problems and more - Maciej Rybaniec (23.06.2017)

Abstract Resolvers

Auth Resolver

HTTP Request

Base Resolver

GraphQL Server

Remove User Mutation Resolver

User Query Resolver

getUserByIdremoveUserById

is allowed

pass through

handleerror

handleerror

Page 33: Thinking in Graphs - GraphQL problems and more - Maciej Rybaniec (23.06.2017)

Base Resolver

Page 34: Thinking in Graphs - GraphQL problems and more - Maciej Rybaniec (23.06.2017)

Auth Resolver

Page 35: Thinking in Graphs - GraphQL problems and more - Maciej Rybaniec (23.06.2017)
Page 36: Thinking in Graphs - GraphQL problems and more - Maciej Rybaniec (23.06.2017)
Page 37: Thinking in Graphs - GraphQL problems and more - Maciej Rybaniec (23.06.2017)

SubscriptionsLive Queries vs Event-based vs Polling

Page 38: Thinking in Graphs - GraphQL problems and more - Maciej Rybaniec (23.06.2017)

Real-time APIs

Pull:- Polling

Push:- Live Queries- Subscriptions

Page 39: Thinking in Graphs - GraphQL problems and more - Maciej Rybaniec (23.06.2017)

GraphQL SubscriptionsClients send the server a GraphQL query and query variables. The server maps these inputs to an event stream and executes the query when the events trigger.

Page 40: Thinking in Graphs - GraphQL problems and more - Maciej Rybaniec (23.06.2017)

PollingPeriodically issues a request to check on the state of the data it cares about.

When updates are infrequent/unpredictable, polling is wasteful.

1 2 3 4

1 3 4 4

client value over time

server value

Page 41: Thinking in Graphs - GraphQL problems and more - Maciej Rybaniec (23.06.2017)

Live Queries

Query Fragment

Subscribe to a query, and push update every time the result of that query changes.

Too much events – causing push

1 2 3 4

client value over time

server value

1 2 3 4

Page 42: Thinking in Graphs - GraphQL problems and more - Maciej Rybaniec (23.06.2017)

Event-based subscriptionsClient tells the server that it cares about one or more events. Whenever those events trigger, the server notifies the client.

2 3

client value over time

server value

2 3

1

Page 43: Thinking in Graphs - GraphQL problems and more - Maciej Rybaniec (23.06.2017)

Kontrakt

Page 44: Thinking in Graphs - GraphQL problems and more - Maciej Rybaniec (23.06.2017)

Minimum

Fixed

Page 45: Thinking in Graphs - GraphQL problems and more - Maciej Rybaniec (23.06.2017)

GraphQL Subscription

Page 46: Thinking in Graphs - GraphQL problems and more - Maciej Rybaniec (23.06.2017)

Optymalizacja

Page 47: Thinking in Graphs - GraphQL problems and more - Maciej Rybaniec (23.06.2017)

Optymalizacja

Sample resolver

Resolving lists or complicated structures

Page 48: Thinking in Graphs - GraphQL problems and more - Maciej Rybaniec (23.06.2017)

Expensive Queries

Page 49: Thinking in Graphs - GraphQL problems and more - Maciej Rybaniec (23.06.2017)

Facebook DataLoaderWithout a caching or batching mechanism, it's easy for a naive GraphQL server to issue new database requests each time a field is resolved.

Page 50: Thinking in Graphs - GraphQL problems and more - Maciej Rybaniec (23.06.2017)

Example

Page 51: Thinking in Graphs - GraphQL problems and more - Maciej Rybaniec (23.06.2017)

Batch Operations

Page 52: Thinking in Graphs - GraphQL problems and more - Maciej Rybaniec (23.06.2017)

Defer query part

Page 53: Thinking in Graphs - GraphQL problems and more - Maciej Rybaniec (23.06.2017)

Stream query field

Page 54: Thinking in Graphs - GraphQL problems and more - Maciej Rybaniec (23.06.2017)

Nowe narzędzia

Page 55: Thinking in Graphs - GraphQL problems and more - Maciej Rybaniec (23.06.2017)

Sample lodash directives

Allows to modify response shape by providing lodash methods.

Page 56: Thinking in Graphs - GraphQL problems and more - Maciej Rybaniec (23.06.2017)

Introspection Query

Page 57: Thinking in Graphs - GraphQL problems and more - Maciej Rybaniec (23.06.2017)

GraphQL VoyagerRepresent any GraphQL API as an interactive graph

Page 58: Thinking in Graphs - GraphQL problems and more - Maciej Rybaniec (23.06.2017)

Relay Modern

Page 59: Thinking in Graphs - GraphQL problems and more - Maciej Rybaniec (23.06.2017)

Relay Modern

New things

New version of Relay designed from the ground up to be easier to use

Garbage CollectionRelay EnviromentFlow Type GenerationSubscriptions & Live QueriesRelay Compiler

Page 60: Thinking in Graphs - GraphQL problems and more - Maciej Rybaniec (23.06.2017)

Dziękuje za uwagę