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

Real World Graphene

Real World Graphene

Adding a full-fledged GraphQL API on top of a Django app using Graphene can be daunting. I want to share a few techniques that bring consistency in the implementation and shape of API, solve common API design issues, and overcome some of the framework’s shortcomings.

Marcin Gębala

October 30, 2019
Tweet

More Decks by Marcin Gębala

Other Decks in Programming

Transcript

  1. COPYRIGHT © 2009–2019 MIRUMEE SOFTWARE Real World Graphene Lessons learned

    from building a GraphQL API on top of a Django app Marcin Gebala
  2. COPYRIGHT © 2009–2019 MIRUMEE SOFTWARE Hi, I’m Marcin @maarcingebala •

    Wrocław, Poland • Python developer at Mirumee • Development lead at Saleor
  3. COPYRIGHT © 2009–2019 MIRUMEE SOFTWARE Saleor GraphQL API • 50

    queries, 200 mutations, 500 types • JWT authentication & permissions • Service accounts & webhooks • Filtering, sorting & search capabilities
  4. COPYRIGHT © 2009–2019 MIRUMEE SOFTWARE • Most popular GraphQL framework

    in Python • Code-first approach ◦ building types and operations in Python ◦ schema is generated • Support for popular Python web frameworks like Django, Flask & Tornado Graphene
  5. COPYRIGHT © 2009–2019 MIRUMEE SOFTWARE • Effective if you’re building

    GraphQL on top of existing codebase. • Control over resolvers • Declarative and class-based approach of Graphene is similar to Django Mapping models to types
  6. COPYRIGHT © 2009–2019 MIRUMEE SOFTWARE • No input data validation

    • No unified errors • A lot of boilerplate if you have many similar mutations Example mutation in Graphene
  7. COPYRIGHT © 2009–2019 MIRUMEE SOFTWARE • Additional method that acts

    as a resolver and handles business logic Unified errors
  8. COPYRIGHT © 2009–2019 MIRUMEE SOFTWARE • Additional method that acts

    as a resolver and handles business logic • try..except to handle mutation errors Unified errors
  9. COPYRIGHT © 2009–2019 MIRUMEE SOFTWARE Unified errors • Unified errors

    field in all mutations • All errors are converted to a common type
  10. COPYRIGHT © 2009–2019 MIRUMEE SOFTWARE CLEAN INPUT DATA CRUD API

    pattern A lot of mutations follow the same pattern
  11. COPYRIGHT © 2009–2019 MIRUMEE SOFTWARE CLEAN INPUT DATA CRUD API

    pattern A lot of mutations follow the same pattern CREATE MODEL INSTANCE
  12. COPYRIGHT © 2009–2019 MIRUMEE SOFTWARE CLEAN INPUT DATA CRUD API

    pattern A lot of mutations follow the same pattern CREATE MODEL INSTANCE VALIDATE
  13. COPYRIGHT © 2009–2019 MIRUMEE SOFTWARE CLEAN INPUT DATA CRUD API

    pattern A lot of mutations follow the same pattern CREATE MODEL INSTANCE VALIDATE SAVE CHANGES IN DATABASE
  14. COPYRIGHT © 2009–2019 MIRUMEE SOFTWARE • Django was designed for

    request-response pattern • It’s internally synchronous (e.g. database access) • Possible to support async requests with Django Channels ◦ @database_sync_to_async • Requires third-party libraries Subscriptions
  15. COPYRIGHT © 2009–2019 MIRUMEE SOFTWARE • JWT authentication and permissions

    • Apollo Federation • File upload You need additional libraries to build a fully-fledged API: • Query cost analysis But some features are not implemented at all:
  16. COPYRIGHT © 2009–2019 MIRUMEE SOFTWARE Ariadne is a Python library

    for implementing schema-first GraphQL servers. It is simple to use and open for extension. • Schema-first approach • Asynchronous execution • Inspired by Apollo Server ariadnegraphql.org