Graphene: A new look into APIs

Graphene: A new look into APIs

My keynote from DjangoCon US 2016.

Cf18e42df2acc7a1fc1700bcd8e76487?s=128

Syrus Akbary

July 18, 2016
Tweet

Transcript

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

  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
  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
  4. 4 Django (Python) BACKEND Server Side Django Templates FRONTEND

  5. 5 Django (Python) BACKEND Server Side Django Templates Destktop webapps

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

    (React.js, Angular…) Mobile apps FRONTEND API
  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
  8. 8 REST Example Let’s create a Conference App!

  9. 9 User Talk Speaker • Title • Time schedule •

    Name • Avatar Talks
  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
  11. 11 • /schedule • /talk/1 • /talk/2 • /talk/3 •

    /user/1 • /user/2 • /user/3 HTTP Requests
  12. 12 • API Versioning • Input Validation • Output Validation

    • Data Underfetching / Overfetching • Network Errors • Network Latency Dealing with REST
  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
  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.
  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
  16. D E C L A R AT I V E

    D ATA F E T C H I N G
  17. 17 GraphQL A new look into APIs

  18. 18 { me { name } } GraphQL Query

  19. 19 { "me": { "name": "Syrus Akbary" } } {

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

    } } GraphQL Query
  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
  22. 22 { me { name talks { title time }

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

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

    } } Talk
  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
  26. 26 GraphQL implementations

  27. 27 Graphene The Pythonic way to GraphQL

  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
  29. 29 User Talk Speaker • Title • Talk schedule •

    Name • Avatar Talks
  30. 30 { me { name talks { title time }

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

    } } class Query(graphene.ObjectType): me = graphene.Field(User) GraphQL Query Implementation
  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
  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
  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()
  35. P L AY G R O U N D

  36. 36 graphene-python.org/playground

  37. 37 Django + Graphene Graphene schema from Django Models

  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()
  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
  40. 40 class User(DjangoObjectType): class Meta: model = models.User class Talk(DjangoObjectType):

    class Meta: model = models.Talk
  41. 41 Demo Time!

  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
  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
  44. Q U E S T I O N S ?

    : )