Lessons Learned from Integrating Django & GraphQL

Lessons Learned from Integrating Django & GraphQL

A few lessons learned while working with GraphQL & Django.

6bb80be88a0b26faf2287eeb409d1322?s=128

Krzysztof Żuraw

January 26, 2018
Tweet

Transcript

  1. LESSONS LEARNED LESSONS LEARNED FROM INTEGRATING DJANGO & FROM INTEGRATING

    DJANGO & GRAPHQL GRAPHQL Created by Krzysztof Żuraw
  2. WHO AM I? WHO AM I? PYTHON AND JAVASCRIPT DEVELOPER

    PYTHON AND JAVASCRIPT DEVELOPER AT AT BASED IN WROCŁAW. BASED IN WROCŁAW. STXNEXT STXNEXT
  3. None
  4. LESSON 1: GRAPHQL LESSON 1: GRAPHQL IS COOL IS COOL

    IF YOU KNOW HOW TO USE IT IF YOU KNOW HOW TO USE IT
  5. TECHNOLOGY STACK TECHNOLOGY STACK

  6. None
  7. None
  8. None
  9. ASK FOR WHAT YOU NEED ASK FOR WHAT YOU NEED

    GET RESULTS GET RESULTS { allActors{ lastName } } { "data": { "allActors": [ { "lastName": "Travolta" }, { "lastName": "Jackson" }, } }
  10. GET MANY RESOURCES IN A GET MANY RESOURCES IN A

    SINGLE REQUEST SINGLE REQUEST { film(id: 1) { title actors { firstName lastName } } }
  11. { "data": { "film": { "title": "Pulp Fiction", "actors": [

    { "firstName": "John", "lastName": "Travolta" }, { "firstName": "Samuel L.", "lastName": "Jackson" }, ]}}
  12. DATA MUTATION DATA MUTATION mutation CreateFilm { createFilm(film: { title:

    "My cool title" }) { title id } } { "data": { "createFilm": { "title": "My cool title", "id": 123 } } }
  13. mutation UpdateFilm { updateFilm(film: { title: "Totaly different title" })

    { title } } { "data": { "updateFilm": { "title": "Totaly different title", } } }
  14. None
  15. None
  16. LESSON 2: GRAPHENE-PYTHON LESSON 2: GRAPHENE-PYTHON class Film(graphene.ObjectType): class Meta:

    interfaces = (relay.Node, ) title = graphene.String() actors = graphene.List(Actor) air_date = graphene_datetime.DateTime() rating = graphene.Int()
  17. { "name": "film", "description": "The ID of the object", "args":

    [ { "name": "id", "description": null, "type": { "kind": "NON_NULL", "name": null, "ofType": { "kind": "SCALAR", "name": "ID", "ofType": null } }, "defaultValue": null } ], "type": { "kind": "OBJECT", "name": "Film", "ofType": null }, "isDeprecated": false
  18. CONCLUSION CONCLUSION PYTHON HAS A GREAT GRAPHQL PYTHON HAS A

    GREAT GRAPHQL LIBRARY LIBRARY BUT BUT You have to be aware that it is a new library
  19. None
  20. LESSON 3: GRAPHRELATED FIELD LESSON 3: GRAPHRELATED FIELD Why? Almost

    like ForeginKey in Django but without existence checks
  21. # one microservice class Film(models.Model): title = models.CharField(max_length=100) air_date =

    models.DateField() actor = GraphRelatedField('Actor') # another microservice class Actor(models.Model): name = models.CharField(max_length=100) # usage actor_name = film.actor.name
  22. CONCLUSION CONCLUSION WE WANT TO HAVE BETTER WE WANT TO

    HAVE BETTER COMMUNICATION BETWEEN COMMUNICATION BETWEEN MICROSERVICES MICROSERVICES JUST TO JUST TO Merge them together
  23. None
  24. LESSON 4: NUMBER OF LESSON 4: NUMBER OF REQUESTS REQUESTS

    Inspired by Django ORM `prefetch_related` Fetch list of objects using one HTTP query and place them in thread-safe attribute inside a context.
  25. CACHING CACHING HTTP Headers caching

  26. SET CACHING HEADERS TO SET CACHING HEADERS TO INFORM INFORM

    For how long cache the response? When there is need for refetch data for different users using the `Vary` header
  27. CACHE POLICY CACHE POLICY Short cache times - less than

    1 minute
  28. CONCLUSION CONCLUSION USING CACHING BY VARY HEADER AND USING CACHING

    BY VARY HEADER AND PREFETCHING OBJECTS IS GOOD IDEA PREFETCHING OBJECTS IS GOOD IDEA
  29. None
  30. LESSON 5: AUTHENTICATION LESSON 5: AUTHENTICATION What? JWT Why?

  31. CONCLUSION CONCLUSION CHOOSING JWT WAS A GOOD IDEA - IT

    CHOOSING JWT WAS A GOOD IDEA - IT ALLOWS US TO STORE ADDITIONAL INFO ALLOWS US TO STORE ADDITIONAL INFO
  32. LESSON 6: FRONTEND TOOLING LESSON 6: FRONTEND TOOLING

  33. CONCLUSION CONCLUSION A LOT OF NEW LIBRARIES - NOT ALWAYS

    A LOT OF NEW LIBRARIES - NOT ALWAYS MATURE MATURE
  34. None
  35. THIS IS THE END THIS IS THE END Thank you

    My blog: krzysztofzuraw.com My twitter: @krzysztof_zuraw