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

Graphene: A new look into APIs

Graphene: A new look into APIs

My keynote from DjangoCon US 2016.

Syrus Akbary

July 18, 2016
Tweet

More Decks by Syrus Akbary

Other Decks in Technology

Transcript

  1. GRAPHENE
    A new look into API’s
    Syrus Akbary
    @syrusakbary

    View Slide

  2. 2
    • Full Stack developer.
    • Working at Affirm, responsible of interservice
    communication with GraphQL… we’re hiring! ;)
    • pyjade, validate_email, interpy, jsjinja, promise, gdom…
    github.com/syrusakbary

    View Slide

  3. T Y P I C A L D J A N G O P R O J E C T S T R U C T U R E

    View Slide

  4. 4
    Django (Python)
    BACKEND
    Server Side Django Templates
    FRONTEND

    View Slide

  5. 5
    Django (Python)
    BACKEND
    Server Side Django Templates
    Destktop webapps (React.js, Angular…)
    Mobile apps
    FRONTEND

    View Slide

  6. 6
    Django (Python)
    BACKEND
    Server Side Django Templates
    Destktop webapps (React.js, Angular…)
    Mobile apps
    FRONTEND
    API

    View Slide

  7. D O M I N A N T D ATA S Y N C H R O N I Z AT I O N : R E S T

    View Slide

  8. 8
    REST Example
    Let’s create a Conference App!

    View Slide

  9. 9
    User
    Talk
    Speaker
    • Title
    • Time schedule
    • Name
    • Avatar
    Talks

    View Slide

  10. 10
    • Get all the talks
    • What is the title for each talk?
    • What is the name and avatar of the speaker in the talk?
    • …
    Create

    View Slide

  11. 11
    • /schedule
    • /talk/1
    • /talk/2
    • /talk/3
    • /user/1
    • /user/2
    • /user/3
    HTTP Requests

    View Slide

  12. 12
    • API Versioning
    • Input Validation
    • Output Validation
    • Data Underfetching / Overfetching
    • Network Errors
    • Network Latency
    Dealing with REST

    View Slide

  13. 13
    • API Versioning
    • Input Validation
    • Output Validation
    • Data Underfetching / Overfetching
    • Network Errors
    • Network Latency
    Dealing with REST
    Partially solved by serializers and
    views in Django REST Framework

    View Slide

  14. 14
    • API Versioning
    • Input Validation
    • Output Validation
    • Data Underfetching / Overfetching
    • Network Errors
    • Network Latency
    Dealing with REST
    Glue everything together in one RPC
    endpoint.

    View Slide

  15. 15
    • /all_talks_with_user_name_and_avatar
    “Could you please add an option to
    get the data back without this field
    but with this extra field for that new
    view that I’m making?”
    • Logic of fetching is moved
    to the server
    • Each time the client
    changes any data
    fetching requirements the
    server has to be updated
    (even if the schema/
    models remain constant)
    • Difficult to scale

    View Slide

  16. D E C L A R AT I V E D ATA F E T C H I N G

    View Slide

  17. 17
    GraphQL
    A new look into APIs

    View Slide

  18. 18
    {
    me {
    name
    }
    }
    GraphQL Query

    View Slide

  19. 19
    {
    "me": {
    "name": "Syrus Akbary"
    }
    }
    {
    me {
    name
    }
    }
    GraphQL Query GraphQL Response

    View Slide

  20. 20
    {
    me {
    name
    talks {
    title
    time
    }
    }
    }
    GraphQL Query

    View Slide

  21. 21
    {
    "me": {
    "name": "Syrus Akbary"
    "talks": [{
    "title": "Graphene: A new…"
    "time": "2016-07-18T17:10"
    }]
    }
    }
    GraphQL Response
    {
    me {
    name
    talks {
    title
    time
    }
    }
    }
    GraphQL Query

    View Slide

  22. 22
    {
    me {
    name
    talks {
    title
    time
    }
    }
    }
    Query

    View Slide

  23. 23
    {
    me {
    name
    talks {
    title
    time
    }
    }
    }
    User

    View Slide

  24. 24
    {
    me {
    name
    talks {
    title
    time
    }
    }
    }
    Talk

    View Slide

  25. 25
    • Query validation
    • Strictly typed: input and output
    • No data Underfetching / Overfetching
    • Introspection
    • Resolver Context
    • One roundtrip for data fetching
    • Backend agnostic
    Advantages of GraphQL

    View Slide

  26. 26
    GraphQL implementations

    View Slide

  27. 27
    Graphene
    The Pythonic way to GraphQL

    View Slide

  28. 28
    • Most starred GraphQL framework (excluding FB
    implementation) - 750 stars… much like!
    • Used by 20+ companies in production (Affirm included)
    • Large community
    • Supports Python 2.7+ and 3.2+
    • Fully compatible with Django 1.6+
    Some Graphene info

    View Slide

  29. 29
    User
    Talk
    Speaker
    • Title
    • Talk schedule
    • Name
    • Avatar
    Talks

    View Slide

  30. 30
    {
    me {
    name
    talks {
    title
    time
    }
    }
    }
    GraphQL Query

    View Slide

  31. 31
    {
    me {
    name
    talks {
    title
    time
    }
    }
    }
    class Query(graphene.ObjectType):
    me = graphene.Field(User)
    GraphQL Query Implementation

    View Slide

  32. 32
    {
    me {
    name
    talks {
    title
    time
    }
    }
    }
    class Query(graphene.ObjectType):
    me = graphene.Field(User)
    class User(graphene.ObjectType):
    name = graphene.String()
    talks = graphene.List(Talk)
    GraphQL Query Implementation

    View Slide

  33. 33
    {
    me {
    name
    talks {
    title
    time
    }
    }
    }
    class Query(graphene.ObjectType):
    me = graphene.Field(User)
    class User(graphene.ObjectType):
    name = graphene.String()
    talks = graphene.List(Talk)
    class Talk(graphene.ObjectType):
    title = graphene.String()
    time = DateTime()
    GraphQL Query Implementation

    View Slide

  34. 34
    class Query(graphene.ObjectType):
    me = graphene.Field(User)
    class User(graphene.ObjectType):
    name = graphene.String()
    talks = graphene.List(Talk)
    class Talk(graphene.ObjectType):
    title = graphene.String()
    time = DateTime()

    View Slide

  35. P L AY G R O U N D

    View Slide

  36. 36
    graphene-python.org/playground

    View Slide

  37. 37
    Django + Graphene
    Graphene schema from Django Models

    View Slide

  38. 38
    class Talk(models.Model):
    title = models.CharField(max_length=50)
    time = models.DateTimeField()
    speaker = models.ForeignKey(User, related_name='talks')
    class User(models.Model):
    name = models.CharField(max_length=50)
    avatar = models.ImageField()

    View Slide

  39. A U TO M AT I C M A P P I N G F R O M
    D J A N G O M O D E L S

    View Slide

  40. 40
    class User(DjangoObjectType):
    class Meta:
    model = models.User
    class Talk(DjangoObjectType):
    class Meta:
    model = models.Talk

    View Slide

  41. 41
    Demo Time!

    View Slide

  42. W H Y U S E G R A P H E N E I N Y O U R B A C K E N D

    View Slide

  43. 43
    • Easier to maintain than REST API’s
    • 1-click documentation and UI (GraphiQL)
    • Quick integration in Frontend with React thanks to Relay
    • Seamless integration with Django
    • Much faster development process
    Why Graphene/GraphQL is better

    View Slide

  44. Q U E S T I O N S ? : )

    View Slide