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.

Avatar for Syrus Akbary

Syrus Akbary

July 18, 2016
Tweet

More Decks by Syrus Akbary

Other Decks in Technology

Transcript

  1. 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
  2. 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
  3. 6 Django (Python) BACKEND Server Side Django Templates Destktop webapps

    (React.js, Angular…) Mobile apps FRONTEND API
  4. 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
  5. 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
  6. 11 • /schedule • /talk/1 • /talk/2 • /talk/3 •

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

    • Data Underfetching / Overfetching • Network Errors • Network Latency Dealing with REST
  8. 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
  9. 14 • API Versioning • Input Validation • Output Validation

    • Data Underfetching / Overfetching • Network Errors • Network Latency Dealing with REST Glue everything together in one RPC endpoint.
  10. 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
  11. D E C L A R AT I V E

    D ATA F E T C H I N G
  12. 19 { "me": { "name": "Syrus Akbary" } } {

    me { name } } GraphQL Query GraphQL Response
  13. 21 { "me": { "name": "Syrus Akbary" "talks": [{ "title":

    "Graphene: A new…" "time": "2016-07-18T17:10" }] } } GraphQL Response { me { name talks { title time } } } GraphQL Query
  14. 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
  15. 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
  16. 31 { me { name talks { title time }

    } } class Query(graphene.ObjectType): me = graphene.Field(User) GraphQL Query Implementation
  17. 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
  18. 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
  19. 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()
  20. 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()
  21. 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
  22. 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
  23. 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