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

Introduction to Django and GraphQL - DjangoCon US 2018

Introduction to Django and GraphQL - DjangoCon US 2018

GraphQL has grown a lot over time, but it seems to still be a new “thing” in the Python and Django World. This talk will be an introduction to GraphQL, explaining why it has been created and how you can use it in Python and Django.

Demo code: https://github.com/patrick91/graphene-demos/

Patrick Arminio

October 16, 2018
Tweet

More Decks by Patrick Arminio

Other Decks in Technology

Transcript

  1. • Patrick Arminio • Chairperson @ Python Italia • Live

    in London • Interested in backend and frontend • @patrick91 online Who am I
  2. { "name": "Patrick", "friends": [ { "name": "Fiorella" }, {

    "name": "Marco" }, { "name": "Marta" }, { "name": “Angela" } ], "avatar": "/images/123" } @patrick91
  3. http GET /user_with_friends/1 http GET /user_with_friends_and_images/1 http GET /user_and_images/1 http

    GET /user_and_small_images/1 http GET /user_with_friends_and_small_images/1 @patrick91
  4. http GET /user_with_friends/1 http GET /user_with_friends_and_images/1 http GET /user_and_images/1 http

    GET /user_and_small_images/1 http GET /user_with_friends_and_small_images/1 http GET /page-1 @patrick91
  5. http GET /user_with_friends/1 http GET /user_with_friends_and_images/1 http GET /user_and_images/1 http

    GET /user_and_small_images/1 http GET /user_with_friends_and_small_images/1 http GET /page-1 http GET /page-2 @patrick91
  6. Source: Coursera [...] At the time, we had over 1,000

    different REST endpoints at Coursera (and now we have many more) [...] https://dev-blog.apollodata.com/courseras-journey-to-graphql-a5ad3b77f39a @patrick91
  7. { "name": "Patrick", "friends": [ { "name": “Angela", "friends": ["/users/2",

    "/users/3", "/users/4"], "avatar": { "url": "//cdn.x.com/123.jpg", "width": 400, "height": 300 } }, { "name": "Simone", "friends": ["/users/2", "/users/3", "/users/4"], "avatar": { "url": "//cdn.x.com/123.jpg", "width": 400, "height": 300 @patrick91
  8. • Int • Float • String • Boolean • ID

    • Any user defined scalar Scalar @patrick91
  9. Object Types are the objects defined in your GraphQL API.

    They are objects that have fields that can of scalar types or other object types. Object Types @patrick91
  10. type User { name: String! email: String! friends: [Friend]! }

    type Friend { name: String! } @patrick91
  11. Query Allows you to request data from the server. Mutation

    Allows you to modify/create data on the server. But it is not limited to data, can be used to run anything with side effects. Subscription Similar to a query, but with real time data. @patrick91
  12. class Query(graphene.ObjectType): hello = graphene.String() def resolve_hello(self, info): return 'hi

    djangocon!' schema = graphene.Schema(query=Query) schema.execute(''' query { hello } ''') @patrick91
  13. # settings.py INSTALLED_APPS += [ 'graphene_django', ] GRAPHENE = {

    'SCHEMA': 'api.schema.schema' } @patrick91
  14. # urls.py from django.conf.urls import path from graphene_django.views import GraphQLView

    urlpatterns = [ path('graphql/', GraphQLView.as_view(graphiql=True)), ] @patrick91
  15. class IngredientType(DjangoObjectType): class Meta: model = Ingredient class Query(graphene.ObjectType): all_ingredients

    = graphene.List(IngredientType) def resolve_all_ingredients(self, info): return Ingredient.objects.all() schema = graphene.Schema(query=Query) @patrick91
  16. class Poll(models.Model): question = models.CharField(max_length=200) def __str__(self): return self.question class

    Choice(models.Model): poll = models.ForeignKey(Poll, on_delete=models.PROTECT) choice_text = models.CharField(max_length=200) votes = models.IntegerField(default=0) def __str__(self): return self.choice_text @patrick91
  17. from graphene_django.types import DjangoObjectType from .models import Choice, Poll class

    ChoiceType(DjangoObjectType): class Meta: model = Choice class PollType(DjangoObjectType): class Meta: model = Poll @patrick91
  18. mutation CreatePoll { createPoll( question: "Is this cool?", choices: ["yes",

    "no"] ) { poll { question choiceSet { choiceText } } } } @patrick91
  19. class CreatePoll(graphene.Mutation): poll = graphene.Field(PollType) class Arguments: question = graphene.String(required=True)

    choices = graphene.List(graphene.String, required=True) def mutate(self, info, question, choices): # non-production code poll = Poll.objects.create(question=question) for choice in choices: Choice.objects.create(poll=poll, choice_text=choice) return CreatePoll(poll) class PollsMutations: create_poll = CreatePoll.Field() @patrick91
  20. Django Session Standard session based authentication, no need to pass

    any custom header or argument Headers Passing an authorization header so that the backend can have access to the current user Params Pass a token to all the queries and mutation that require the user to be logged in @patrick91
  21. • No built in features, yet • Permissions on field

    level • Public / Private fields Permissions @patrick91
  22. query SinglePoll { poll(id: "ABC") { question choiceSet { votes

    choiceText poll { question choiceSet { votes choiceText poll { question choiceSet { votes choiceText } } } @patrick91