Upload
grand-parade-poland
View
118
Download
0
Embed Size (px)
Citation preview
Thinking in GraphsGraphQL problems and more
Maciej Rybaniec
Plan
1) GraphQL
2) Problemy związane z GraphQL
3) Nowe narzędzia
4) Relay Modern
GraphQL
GraphQL misconceptions
NOT library or storage system
NOT tied with specific language
NOT tied with specific framework
NOT required specific storage system
How it works ?
GraphQL Schema
What I can fetch ?
Client
Back-end Databases, APIs, Services
MongoDB Redis Facebook API
Sposób komunikacji z API
Podstawowe cele
No Overfetching
Be Declarative
Product Development
Autonomy
Versioning Support
Podstawoweoperacje
Queries
Query Response
Allows to fetch data in a declarative way
QueryA GraphQL query is just an instruction for traversing the graph in a specific way, resulting in a tree.
Mutations
Mutation Response
Allows to mutate data in server and fetch new state after change
Problemy
Best practices
Single endpoint
Authorization
Obsługa dostępu w przypadku
zagnieżdżonych relacji
Error handling
Obsługa błędów w przypadku gdy
zapytania mogą być złożone
REST
HTTP Request
Warstwa danych
Middleware
Route Controller
Użytkownik
GraphQLNo middleware and afterware
Resolvers – responsible for handling portions of a request
Resolvers
Sample field resolver
Deliver data based on contract
Can contain arbitrary code
Problem definition
Handle permissions on edges.
Basic Solution
Multiple edges can lead to a particular node
Permissions on edges
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
Delegate authorization logic to the business logic layer
Facebook Solution
Sample authorization
Bad practice
Single source of truthBusiness logic layer should act as the single source
of truth for enforcing business domain rules.
1) Move authorization responsibility toresolvers.
2) Client can understand errors without proper status code.
3) Masked Internal errors.
4) DRY
Apollo Solution
Abstract Resolvers
Base root resolver
Chain of resolvers to satisfy individual parts of the overall problem
Abstract Resolvers
Auth Resolver
HTTP Request
Base Resolver
GraphQL Server
Remove User Mutation Resolver
User Query Resolver
getUserByIdremoveUserById
is allowed
pass through
handleerror
handleerror
Base Resolver
Auth Resolver
SubscriptionsLive Queries vs Event-based vs Polling
Real-time APIs
Pull:- Polling
Push:- Live Queries- Subscriptions
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.
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
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
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
Kontrakt
Minimum
Fixed
GraphQL Subscription
Optymalizacja
Optymalizacja
Sample resolver
Resolving lists or complicated structures
Expensive Queries
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.
Example
Batch Operations
Defer query part
Stream query field
Nowe narzędzia
Sample lodash directives
Allows to modify response shape by providing lodash methods.
Introspection Query
GraphQL VoyagerRepresent any GraphQL API as an interactive graph
Relay Modern
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
Dziękuje za uwagę