is graphql the end of reststlye apis? (joakim lundborg)

44
Is GraphQL the end of RESTful APIs? Joakim Lundborg, Wrapp

Upload: nordic-apis

Post on 09-Jan-2017

85 views

Category:

Technology


2 download

TRANSCRIPT

Page 1: Is GraphQL the end of RESTstlye APIs? (Joakim Lundborg)

Is GraphQL the end of

RESTful APIs?Joakim Lundborg, Wrapp

Page 2: Is GraphQL the end of RESTstlye APIs? (Joakim Lundborg)

Is GraphQL the end of

RESTful APIs?Maybe, let’s take a look

(the dominance of)

(and other similar technologies)

Page 3: Is GraphQL the end of RESTstlye APIs? (Joakim Lundborg)

Joakim [email protected]/cortex

Team Shopper @Wrapp

Page 4: Is GraphQL the end of RESTstlye APIs? (Joakim Lundborg)

The Sapir-Whorf hypothesis Image from the article “Language and Science”, originally published in the MIT Technology Review, 1940. Image copyright MIT Press.

Page 5: Is GraphQL the end of RESTstlye APIs? (Joakim Lundborg)

An API is a language in which we converse with a system

Page 6: Is GraphQL the end of RESTstlye APIs? (Joakim Lundborg)

Languages structure our thinkingAPIs

Page 7: Is GraphQL the end of RESTstlye APIs? (Joakim Lundborg)

RESTful APIsEverything is a resource

Page 8: Is GraphQL the end of RESTstlye APIs? (Joakim Lundborg)

REST nounsPoints out a single resource

Page 9: Is GraphQL the end of RESTstlye APIs? (Joakim Lundborg)

REST verbsGET “give me this resource

PUT “here is a resource”

DELETE “remove this document”

POST “create a document” “JUST DO SOMETHING”

Page 10: Is GraphQL the end of RESTstlye APIs? (Joakim Lundborg)

HATEOASHypermedia drives state

Page 11: Is GraphQL the end of RESTstlye APIs? (Joakim Lundborg)

...sounds great, why would anyone want to change this?

Page 12: Is GraphQL the end of RESTstlye APIs? (Joakim Lundborg)
Page 13: Is GraphQL the end of RESTstlye APIs? (Joakim Lundborg)

Fat clients & ever changing data

Page 14: Is GraphQL the end of RESTstlye APIs? (Joakim Lundborg)

Round trip time...maybe HTTP/2 will fix this

Page 15: Is GraphQL the end of RESTstlye APIs? (Joakim Lundborg)

Type safetyMIME if you are lucky,

roll your own otherwise

(openapi, JSON-schema)

Page 16: Is GraphQL the end of RESTstlye APIs? (Joakim Lundborg)

Over/underfetchingUnnecessary bloat, or extra round trips, pick any two

Page 17: Is GraphQL the end of RESTstlye APIs? (Joakim Lundborg)
Page 18: Is GraphQL the end of RESTstlye APIs? (Joakim Lundborg)

VersioningHave fun!

Page 19: Is GraphQL the end of RESTstlye APIs? (Joakim Lundborg)

Versions are great,let’s make more of them

$ cat endpoints.py | grep route @app.route('/v3/fail', methods=['GET']) @app.route('/v3/client-upgrade-required', methods=['GET']) @app.route('/v3/ping', methods=['GET']) @app.route('/v3/status', methods=['GET']) @app.route("/v3/auth/sms/request-code", methods=['POST']) @app.route("/v3/auth/sms", methods=['POST']) @app.route('/v3/auth/session-token', methods=['POST']) @app.route('/v1/users/me/validate', methods=['POST']) @app.route('/v3/users/me/register', methods=['POST']) @app.route('/v3/users/me/discover', methods=['GET']) @app.route('/v4/users/me/discover', methods=['GET']) @app.route('/v4/users/me/nearby-offers', methods=['GET']) @app.route('/v3/offers/<string:offer_id>', methods=['GET']) @app.route('/v4/offers/<string:offer_id>/activate', methods=['POST']) @app.route('/v3/offers/<string:offer_id>/activate', methods=['POST']) @app.route('/v3/offers/<string:offer_id>/like', methods=['POST']) @app.route('/v3/offers/<string:offer_id>/unlike', methods=['POST']) @app.route('/v3/merchants/<string:merchants_id>/punchcard', methods=['GET']) @app.route('/v3/impressions', methods=['POST']) @app.route('/v3/users/me', methods=['GET']) @app.route('/v3/users/me/entercard-requested-confirmed', methods=['POST']) @app.route("/v3/purchases/<string:purchase_id>", methods=['GET']) @app.route('/v3/purchases/<string:purchase_id>/location', methods=['POST']) @app.route('/v3/users/me/events', methods=['GET']) @app.route('/v3/users/me/purchases', methods=['GET']) @app.route('/v3/users/me/dashboard', methods=['GET']) @app.route('/v3/users/me/last-purchase', methods=['GET']) @app.route('/v3/users/me/profile', methods=['GET']) @app.route('/v3/users/me/category-status', methods=['GET']) @app.route('/v3/users/me/stats', methods=['GET']) @app.route('/v3/users/me/payouts', methods=['GET']) @app.route('/v3/users/me/bank-wish/<string:bank_id>', methods=['post']) @app.route('/v3/integrations/<integration>/register', methods=['POST']) @app.route('/v5/integrations', methods=['GET']) @app.route('/v4/terms', methods=['GET']) @app.route('/v3/integrations/new-nordea/terms', methods=['GET']) @app.route('/v3/integrations/nordea/terms', methods=['GET']) @app.route('/v3/integrations/nordea/supported-bins', methods=['GET']) @app.route('/v3/integrations/active', methods=['POST']) @app.route('/v4/integrations/active', methods=['POST']) @app.route('/v3/users/me/integration-cards', methods=['GET']) @app.route('/v3/users/me/cards', methods=['GET']) @app.route('/v3/users/me/cards/<string:issuer>/<string:last_four>', methods=['DELETE']) @app.route('/v4/users/me/cards/<string:issuer>/<string:card_id>', methods=['DELETE']) @app.route('/v3/users/me/push-settings', methods=['GET']) @app.route('/v3/users/me/push-settings', methods=['POST']) @app.route('/v3/metrics', methods=['POST']) @app.route("/v3/users/me/pushtokens", methods=['POST']) @app.route("/v3/users/me/pushtokens", methods=['DELETE']) @app.route("/v3/users/me/invites", methods=['GET']) @app.route("/v3/users/me/invites/<string:invite_code>/validate", methods=['GET']) @app.route("/v3/users/me/codes/<string:code>/use", methods=['POST']) @app.route("/v3/users/me/user_codes/<string:code>/use", methods=['POST']) @app.route("/v3/users/me/event_codes/<string:code>/use", methods=['POST']) @app.route("/v3/users/me/codes", methods=['GET']) @app.route("/v3/users/me/invites/<string:invite_code>/mark-as-sent", methods=['POST']) @app.route("/v3/activation-steps", methods=['GET']) @app.route("/v3/merchants/<string:merchant_id>/stores/nearby/<string:position>", methods=['GET']) @app.route("/v3/mailchimp/events", methods=['POST', 'GET']) @app.route("/v3/auth/bankid/request", methods=['POST']) @app.route("/v3/auth/bankid", methods=['POST']) @app.route("/v3/sms/answer", methods=['POST'])

Page 20: Is GraphQL the end of RESTstlye APIs? (Joakim Lundborg)

Thin and fat clientsHATEOAS Fat clients

Backend services

API

Client

API

Client

Backend services

Page 21: Is GraphQL the end of RESTstlye APIs? (Joakim Lundborg)

GraphQLEverything is a graph

Page 22: Is GraphQL the end of RESTstlye APIs? (Joakim Lundborg)

GraphQL is a declarative query language

Page 23: Is GraphQL the end of RESTstlye APIs? (Joakim Lundborg)

DeclarativeSay what you want to get, let the server figure it out

Page 24: Is GraphQL the end of RESTstlye APIs? (Joakim Lundborg)

A simple example{ me { name role company handle url }}

{ "data": { "me": { "name": "Joakim Lundborg", "role": "Team lead", "company": "Wrapp", "handle": "@joakimlundborg", "url": "/speakers/joakim-lundborg/" } }}

GET /users/me { "name": "Joakim Lundborg", "role": "Team lead", "company": "Wrapp", "handle": "@joakimlundborg", "url": "/speakers/joakim-lundborg/"}

Page 25: Is GraphQL the end of RESTstlye APIs? (Joakim Lundborg)

GraphQL can do mutation toomutation me { changeRole(“GraphQL evangelist){

namerole

}}

{ "data": { "me": { "name": "Joakim Lundborg", "role": "GraphQL evangelist", } }}

POST /users/me { "name": "Joakim Lundborg", "role": "Team lead", "company": "Wrapp", "handle": "@joakimlundborg", "url": "/speakers/joakim-lundborg/"}

Page 26: Is GraphQL the end of RESTstlye APIs? (Joakim Lundborg)

Summary

REST GraphQL

Round trips Many Few

Type safety No Yes

Discoverability HATEOAS/Swagger/etc.. Yes

Performance Proxies! Caching! CDN! You’d better have good servers & engineers

Architecture Thin client/fat Server Fat client/Fat server

Page 27: Is GraphQL the end of RESTstlye APIs? (Joakim Lundborg)

GraphQLAs a language

Page 28: Is GraphQL the end of RESTstlye APIs? (Joakim Lundborg)

GraphQLIs in some ways simpler than REST

Page 29: Is GraphQL the end of RESTstlye APIs? (Joakim Lundborg)

Two verbsQuery and Mutation

Page 30: Is GraphQL the end of RESTstlye APIs? (Joakim Lundborg)

A sophisticated type systemWith introspection

Page 31: Is GraphQL the end of RESTstlye APIs? (Joakim Lundborg)

This declarative thing seems familiar...

Page 32: Is GraphQL the end of RESTstlye APIs? (Joakim Lundborg)

SQLEverything is a table relation

(at least the result set)

Page 33: Is GraphQL the end of RESTstlye APIs? (Joakim Lundborg)

GraphQL vs SQLGraphQL is much simpler,

maps to afamiliar tree structure

Page 34: Is GraphQL the end of RESTstlye APIs? (Joakim Lundborg)

JSON is just a transport formatAnything that can express a tree-structure would work

Page 35: Is GraphQL the end of RESTstlye APIs? (Joakim Lundborg)

Selectors can be seen as pathsExcept you don’t pay for the roundtrips

Page 36: Is GraphQL the end of RESTstlye APIs? (Joakim Lundborg)

DEMOhttps://github.com/cortex/graphql-nordicapis

npm install

node init.js

Page 37: Is GraphQL the end of RESTstlye APIs? (Joakim Lundborg)

GraphQL and the future

Page 38: Is GraphQL the end of RESTstlye APIs? (Joakim Lundborg)

Microservices <3 GraphQLUntangling a micro-web

Page 39: Is GraphQL the end of RESTstlye APIs? (Joakim Lundborg)

GraphQL as a libraryEven fewer roundtrips

Page 40: Is GraphQL the end of RESTstlye APIs? (Joakim Lundborg)

The open webHow do you crawl a GraphQL service?

Page 41: Is GraphQL the end of RESTstlye APIs? (Joakim Lundborg)

Open DataA great fit for GraphQL

Page 42: Is GraphQL the end of RESTstlye APIs? (Joakim Lundborg)

GraphQL aggregator proxies?A future direction for open data APIs?

Page 43: Is GraphQL the end of RESTstlye APIs? (Joakim Lundborg)

Is GraphQL the end of

RESTful APIs?Yes, but we will probably see more churn innovation

(the dominance of)

(and other similar technologies)

Page 44: Is GraphQL the end of RESTstlye APIs? (Joakim Lundborg)

Questions