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

Lessons Learned from Integrating Django & GraphQL

Lessons Learned from Integrating Django & GraphQL

A few lessons learned while working with GraphQL & Django.

Krzysztof Żuraw

January 26, 2018
Tweet

More Decks by Krzysztof Żuraw

Other Decks in Programming

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. LESSON 1: GRAPHQL LESSON 1: GRAPHQL IS COOL IS COOL

    IF YOU KNOW HOW TO USE IT IF YOU KNOW HOW TO USE IT
  4. ASK FOR WHAT YOU NEED ASK FOR WHAT YOU NEED

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

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

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

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

    { title } } { "data": { "updateFilm": { "title": "Totaly different title", } } }
  9. 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()
  10. { "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
  11. 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
  12. LESSON 3: GRAPHRELATED FIELD LESSON 3: GRAPHRELATED FIELD Why? Almost

    like ForeginKey in Django but without existence checks
  13. # 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
  14. 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
  15. 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.
  16. 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
  17. CONCLUSION CONCLUSION USING CACHING BY VARY HEADER AND USING CACHING

    BY VARY HEADER AND PREFETCHING OBJECTS IS GOOD IDEA PREFETCHING OBJECTS IS GOOD IDEA
  18. 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
  19. CONCLUSION CONCLUSION A LOT OF NEW LIBRARIES - NOT ALWAYS

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

    My blog: krzysztofzuraw.com My twitter: @krzysztof_zuraw