Save 37% off PRO during our Black Friday Sale! »

Introduction to GraphQL

Introduction to GraphQL

Learn about GraphQL, how it works, and how to use it.

4cae070608be3489262fe419c03498dc?s=128

Adrián Matellanes

September 23, 2017
Tweet

Transcript

  1. Introduction to GraphQL

  2. GET https://2017.es.pycon.org/speakers/me { "name": "Adrián Matellanes", "roles": [ "Lead API

    Developer", "Málaga Python Organizer" ], "worksAt": "Ebury", "twitter": "@_amatellanes", "github": "github.com/amatellanes" }
  3. What’s GraphQL?

  4. A programming language

  5. A special type of database

  6. A library

  7. A Query Language for APIs

  8. Why GraphQL?

  9. Due to the nature of REST

  10. REST Problems

  11. GET /characters/1 { "name": "Jon Snow" }

  12. "url": "http://anapioficeandfire.com/api/characters/583", "name": "Jon Snow", "culture": "Northmen", "born": "In 283

    AC", "titles": [ "Lord Commander of the Night's Watch" ], "aliases": [ "Lord Snow", "Ned Stark's Bastard", "The Snow of Winterfell", "The Bastard of Winterfell", "Lord Crow" ], "allegiances": [ "http://anapioficeandfire.com/api/houses/362" ], "books": [ "http://anapioficeandfire.com/api/books/5" ], "povBooks": [ "http://anapioficeandfire.com/api/books/1", "http://anapioficeandfire.com/api/books/2", "http://anapioficeandfire.com/api/books/3", "http://anapioficeandfire.com/api/books/8"
  13. Round Trip and Repeat Trip Times

  14. None
  15. /episodes/1

  16. /episodes/1 /characters/345 /characters/563 /characters/802 /characters/441 /characters/674

  17. /episodes/1 /characters/345 /characters/563 /characters/802 /characters/441 /characters/674 /reviews/3512

  18. Over/Under Fetching

  19. /episodes/1?fields=characters(name,playedBy)

  20. /episode_with_all_stuff /episode_with_amazing_pictures /episode_with_stuff_i_need_today /episode_with_stuff_for_mobile_app /episode_with_actors_and_reviews_and_images /episode_with_stuff_for_mobile_app_v2 /episode_with_actors_and_reviews_but_no_images

  21. /episode_with_all_stuff /episode_with_amazing_pictures /episode_with_stuff_i_need_today /episode_with_stuff_for_mobile_app /episode_with_stuff_for_mobile_app_v2 /episode_with_actors_and_reviews_and_images /episode_with_actors_and_reviews_but_no_images

  22. None
  23. https://code.facebook.com/projects/

  24. Created July 1, 2015 Updated September 20, 2017 381 Forks

    6,078 Stars 302 Commits 82 Open Issues https://github.com/facebook/graphql
  25. None
  26. “You will receive what you ask for no more, no

    less.” - Mark Allen
  27. None
  28. None
  29. None
  30. None
  31. None
  32. None
  33. None
  34. None
  35. None
  36. None
  37. None
  38. GraphQL vs. REST

  39. REST Model

  40. Client /books /characters /houses Databases Services External APIs

  41. GraphQL Model

  42. Client Databases Services External APIs /graphql/

  43. Anatomy of a GraphQL

  44. { me { name } }

  45. { me { name } } { "me": { "name":

    "Jon Snow" } }
  46. Scalar types

  47. Int Float String Boolean ID Date enum

  48. GraphQL Schema Definition Language

  49. type Query { allCharacters: [Character]! character(id: ID!): Character }

  50. type Query { allCharacters: [Character]! character(id: ID!): Character } Root

    Type
  51. type Query { allCharacters: [Character]! character(id: ID!): Character } Root

    Fields
  52. type Query { allCharacters: [Character]! character(id: ID!): Character } Arguments

  53. type Query { allCharacters: [Character]! character(id: ID!): Character } Return

    Types
  54. type Character { name: String! house: House! } type House

    { name: String! words: String! members: [Character]! }
  55. type Character { name: String! house: House! } type House

    { name: String! words: String! members: [Character]! } Object Type
  56. type Character { name: String! house: House! } type House

    { name: String! words: String! members: [Character]! } Fields
  57. type Character { name: String! house: House! } type House

    { name: String! words: String! members: [Character]! } Return Types
  58. Queries for fetching data from the server

  59. query CharactersNameAndHouse { allCharacters { name house { words }

    } }
  60. query CharactersNameAndHouse { allCharacters { name house { words }

    } } Operation Type
  61. query CharactersNameAndHouse { allCharacters { name house { words }

    } } Operation Name
  62. query CharactersNameAndHouse { allCharacters { name house { words }

    } } Selection Set
  63. query CharactersNameAndHouse { allCharacters { name house { words }

    } } Fields
  64. query CharactersNameAndHouse { allCharacters { name house { words }

    } } { "data": { "allCharacters": [ { "name": "Daenerys Targaryen", "house": { "words": "Fire and Blood" } }, { "name": "Jon Snow", "house": { "words": "Winter Is Coming" } } ] } }
  65. query { character(name: "Hodor") { name house { words }

    } } Arguments
  66. query CharacterByName($name: String!) { character(name: $name) { name } }

    { "name": "Hodor" } Variables
  67. query { character(name: "Hodor") { ...CharacterFragment } } fragment CharacterFragment

    on Character { name house { words } } Fragments
  68. { character(name: "Hodor") { ... on Character { name house

    { words } } } } Inline Fragments
  69. Mutations for manipulating data and fetch the updated result

  70. type Mutation { createCharacter(name: String!): Character }

  71. mutation CharacterMutation { createCharacter(name: "Sansa Stark") { name } }

    { "data": { "createCharacter": { "name": "Sansa Stark" } } }
  72. Subscriptions for real-time updates

  73. type Subscription { newCharacter: Character! }

  74. subscription NewCharacterSubscription { newCharacter { name } } { "data":

    { "newCharacter": { "name": "Tyrion Lannister" } } }
  75. Query Execution

  76. { getHouse(id: 5) { name words members { name }

    } }
  77. name words members getHouse(id: 5) { name, words, members }

    [ { name }, { name } ] name name
  78. QUERY name words members getHouse(id: 5) { name, words, members

    } [ { name }, { name } ] name name
  79. QUERY getHouse(id: 5) { name, words, members } HOUSE name

    words members [ { name }, { name } ] name name
  80. QUERY getHouse(id: 5) { name, words, members } HOUSE name

    words members [ { name }, { name } ] CHARACTER name name
  81. Clients with Super Powers

  82. Introspection Queries

  83. { "data": { "__type": { "name": "Book", "kind": "OBJECT", "fields":

    [ { "name": "title", "type": { "name": "String" } } ] } } } { __type(name: "Book") { name kind fields { name type { name } } } }
  84. None
  85. GraphiQL

  86. None
  87. Graphene GraphQL framework for Python

  88. Created September 24, 2015 Updated September 20, 2017 229 Forks

    2,356 Stars 1,219 Commits 87 Open Issues https://github.com/graphql-python/graphene
  89. None
  90. None
  91. "It’s important to understand that it isn’t all or nothing.

    GraphQL is in our future, but it isn’t our exclusive future."
  92. Thank you!