Upgrade to Pro — share decks privately, control downloads, hide ads and more …

The AJAX, The REST and The GraphQL

The AJAX, The REST and The GraphQL

Иван Чернов (Ostrovok.ru) @ MoscowPython Meetup 47

"Рассмотрим историю того, как люди писали API для современного веба: как всё было весело и просто в начале, с какими трудностями сталкивались и какие паттерны применяли. После чего плавно подведем почему GraphQL является логичный эволюцией на возникшие трудности и посокрушаемся, что решение это не для всех".

Видео: http://www.moscowpython.ru/meetup/47/the-ajax-the-rest-and-the-graphql/

Moscow Python Meetup

July 27, 2017
Tweet

More Decks by Moscow Python Meetup

Other Decks in Programming

Transcript

  1. About me • Python developer @ Ostrovok.ru • Maintain and

    develop: ◦ Site backend ◦ Email marketing platform ◦ Booking service
  2. About me • Python developer @ Ostrovok.ru • Maintain and

    develop: ◦ Site backend ◦ Email marketing platform ◦ Booking service • Past three month also work on frontend (:
  3. Agenda • Brief history • REST • GraphQL ◦ Overview

    ◦ Comparison to REST ◦ Practical notes
  4. REST is an architecture pattern • Client-Server • Layered System

    • Stateless • Cacheability • Uniform Interface
  5. REST is an architecture pattern • Client-Server • Layered System

    • Stateless • Cacheability • Uniform Interface
  6. REST applied to Web service • Two URL type ◦

    https://my.cool.blog/posts/ ◦ https://my.cool.blog/posts/1/ • HTTP verbs for manipulation ◦ Read - GET ◦ Create/Update/Delete - POST/PUT/DELETE
  7. GraphQL • Query language for your API ◦ Backend defines

    scheme & queries ◦ Client sends a request with the queries
  8. GraphQL • Query language for your API ◦ Backend defines

    scheme & queries ◦ Client sends a request with the queries ◦ And gets the data in single response
  9. type User { id: ID username: String avatarUrl: String }

    type Comment { post: Post text: String rating: Int author: User createdAt: Date } Describe your schema type Post { id: ID title: String body: String preview: String author: User comments: [Comment] }
  10. Django graphene I # app/blog/schema.py import graphene from graphene_django.types import

    DjangoObjectType from .models import Post class PostType(DjangoObjectType): class Meta: model = Post
  11. Django graphene II class Query(graphene.AbstractType): post = graphene.Field(PostType, id=graphene.Int(required=True)) def

    resolve_post(self, args, context, info): return Post.objects.prefetch_related(“comments”).filter(id=args[“id”])
  12. GraphQL - get post title Request { post(id: 1) {

    title } } Response { “post”: { “title”: “Hello!” } }
  13. REST - get post title Request GET /posts/1/ Response {

    “post”: { “title”: “Hello!”, “body”: “...”, “user_id”: 1, ... } }
  14. REST - get post title Request GET /posts/1/?fields=title Response {

    “post”: { “title”: “Hello!” } }
  15. GraphQL - post & comments Request { post(id: 1) {

    title comments { text } } } Response { “post”: { “title”: “Hello!”, “comments”: [{ “text”: “Hi!” }] } }
  16. REST - post & comments Request GET /posts/1/?fields=title Response {

    “post”: { “title”: “Hello!” } }
  17. REST - post & comments Request GET /posts/1/comments/ Response {

    “comments”: [{ “text”: “Hi!” }] }
  18. REST - post & comments Request GET /posts/1/?with=comment s Response

    { “post”: { “title”: “Hello!”, “comments”: [{ “text”: “Hi!” }] } }
  19. Django graphene III class Query(graphene.AbstractType): post = graphene.Field(PostType, id=graphene.Int(required=True)) me

    = graphene.Field(UserType) def resolve_post(self, args, context, info): return Post.objects.prefetch_related(“comments”).filter(id=args[“id”]) def resolve_me(self, args, context, info): # context is same as request if context.user.is_authenicated(): return context.user
  20. GraphQL - profile, post & comments Request { me {

    username }, post(id: 1) { ... } } Response { “me”: { “username”: “Ivan” }, “post”: { ... } }
  21. REST - profile, post & comments Request GET /posts/1/?with=comment s

    Response { “post”: { “title”: “Hello!”, “comments”: [{ “text”: “Hi!” }] } }
  22. REST - profile, post & comments Request GET /me/ Response

    { “me”: { “username”: “Ivan” } }
  23. Query language (Query def) Request query PostAndComments($postId: Int) { post(id:

    $postId) { title comments { text } } Variables { “postId”: 1 }
  24. Practical notes I • Libraries are not good ◦ No

    stable libs for some langs (golang) ◦ Not all features are implemented (python)
  25. Practical notes II • If you have microservices ◦ Don’t

    implement GraphQL in each of them ◦ Do API gateway with GraphQL to REST ▪ Apollo Server (node.js)
  26. Practical notes III • GraphQL schema != database schema ◦

    Database schema describes how you store the data
  27. Practical notes III • GraphQL schema != database schema ◦

    Database schema describes how you store the data ◦ GraphQL schema describes how clients use the data
  28. Practical notes III • GraphQL schema != database schema ◦

    Database schema describes how you store the data ◦ GraphQL schema describes how clients use the data ◦ P.S. blog was an example
  29. GraphQL Summary Pros 1. Strongly typed 2. Automated docs 3.

    Flexible Cons 1. Lack of control 2. Query optimization
  30. GraphQL Summary Pros 1. Strongly typed 2. Automated docs 3.

    Flexible Cons 1. Lack of control 2. Query optimization 3. Rate limit
  31. Links 1. Read a. http://graphql.org b. http://docs.graphene-python.org/en/latest/ c. http://engineering.remind.com/graphql-initiative-at-remind/ d.

    https://developer.github.com/v4/guides/intro-to-graphql/ 2. Try a. https://github.com/vanadium23/mp-graphql-blog/ b. https://developer.github.com/v4/explorer/