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

Building APIs in GraphQL

Building APIs in GraphQL

Leveraged by Facebook, Pinterest, GitHub and others, GraphQL is a new approach to API development. GraphQL is an application layer query language for your API that enables you to ensure that your clients are getting exactly what they asked for, it allows for predictable results.

It also offers a myriad of other benefits namely elegant data retrieval using a type system, service caching, better query efficiency and more! We will cover what GraphQL is, what the benefits of GraphQL are, how GraphQL's graph system works and how GraphQL's type system works.

With the basics covered, we will look at how to implement GraphQL in a real-world API using Python. We will look at how to implement Schemas in GraphQL, queries and query variables, mutations, validation and authentication as well as request budgeting. By the end, you will have enough knowledge to start building API's in Python using GraphQL.

Liam Norman

April 29, 2019
Tweet

More Decks by Liam Norman

Other Decks in Programming

Transcript

  1. Building API’s in GraphQL Liam Norman | DeveloperUG

  2. HELLO! I am Liam Norman • Software Engineer at Superbalist.com

    • Organiser of CT PHP • Blog at liamnorman.com • @liamjnorman 2 ✋
  3. What is GraphQL?

  4. “GraphQL is a query language for your API, and a

    server-side runtime for executing queries by using a type system you define for your data 4
  5. WHAT IS GRAPHQL? ▰ An API query language ▰ Developed

    by Facebook in 2012 ▰ Not only to be used with React! ▰ Open sourced by facebook in 2015 - https://graphql.github.io/graphql-spec/ 5
  6. Our first Query! 6 ▰ Using GraphiQL client

  7. Example GraphQL Query 7

  8. Query Arguments 8

  9. DESIGN PRINCIPLES ▰ Hierarchical ▰ Product Centric ▰ Strong Typing

    with Schemas ▰ Client-specified Queries ▰ Introspective 9
  10. Why GraphQL?

  11. PROBLEMS GRAPHQL SOLVES ▰ Declarative Data Fetching ▰ No Overfetching

    ▰ Solves Underfetching ▰ Language and framework independent 11
  12. HOW GRAPHQL WORKS 12

  13. GraphQL Adoption 13

  14. Thinking in Graphs 14

  15. “With GraphQL, you model your business domain as a graph.

    15
  16. Graph Theory 101 (in short) 16 ▰ G = (V,

    E) ▰ G = Graph , V = vertices or nodes and E equals edges ▰ Vertices = {1, 2, 3, 4} ▰ Edges = { {1, 2}, {1, 3} etc… }
  17. Graph Theory 101 (in short) 17

  18. Graph Theory 101 (in short) 18

  19. GraphQL Schemas 19

  20. GraphQL Schemas 20 ▰ Schema First Design ▰ GraphQL schemas

    are defined by GraphQL SDL (schema definition language) ▰ GraphQL Schema documents are text documents that define the types available in your application
  21. Defining Types 21 ▰ We are going to look at

    a podcast application called GraphPod ▰ We are going to design a few types for our application ▰ All schema types have the .graphql extension ▰ First let’s learn about GraphQL Schemas and the SDL
  22. 22 ▰ GraphQL has support for various scalar types ▰

    Int ▰ Float ▰ String ▰ Boolean ▰ ID - represents unique identifier ▰ ! represents required type Types 101 - Scalar Types
  23. Types 101 - Object Types and Fields 23 ▰ The

    core unit of a GraphQL schema is the type. A type represents a custom object and these objects describe your application’s features
  24. 24 ▰ There are two types that are special namely

    query and mutation, these are root types ▰ Query defines that we are going to be fetching data, think of it as similar to a GET HTTP request ▰ A mutation defines that we are going to be updating the data. Think of them as POST/PUT/DELETE HTTP requests Types 101 - Queries and Mutations
  25. 25

  26. 26

  27. Example GraphQL Mutation 27

  28. 28 ▰ We can define enums using the enum type

    Types 101 - Enumeration fields
  29. 29 ▰ A list isn’t a scalar but signifies an

    array of object types or scalars Types 101 - Lists
  30. 30 ▰ A interface is an abstract type that includes

    a certain set of fields that a type must implement if it implements the interface ▰ Similar to how interfaces work in Java, PHP etc… ▰ Interfaces are useful when you are returning objects of different types ▰ Interfaces force validation and common design among data types Types 101 - Interfaces
  31. 31 Types 101 - Interfaces

  32. 32 ▰ A union is similar to a interface except

    they don’t force to define common data types. Types 101 - Unions
  33. Designing our Schema 33

  34. Podcast Type 34

  35. User Type 35

  36. Station Type 36

  37. Favourite Type 37

  38. Building a GraphQL API 38

  39. 39 ▰ Python 3.7 ▰ Graphene 2.0 and Graphene-Django ▰

    Django 2.1 ▰ SQLite DB Our Toolkit
  40. Why Graphene? 40 ▰ Simple and powerful ▰ Extensible (usage

    with Django, SQLAlchemy etc…) ▰ Used by Yelp, Mozilla, dailymotion etc… ▰ Graphene handles the GraphQL Server
  41. 41 Installation

  42. Configuring Graphene with Django 42

  43. Creating our apps 43

  44. Crafting Stations - Model 44

  45. Schema Organisation 45 ▰ Each app will have a schema.py

    which correlates to what mutations and queries that application can serve to our GraphQL Server ▰ We first create a schema.py in /stations/ app ▰ We create a base schema.py as we defined in our settings which resolves all other queries and mutations.
  46. Base Schema File (as in settings.py) 46

  47. Crafting Stations - Query Stations 47

  48. GraphQL Resolvers Zoomed In 48 ▰ A resolver defines (to

    the server) how we return schema objects when queried
  49. 49

  50. Crafting Stations - Create Station 50

  51. 51

  52. 52

  53. 53

  54. Crafting Stations - Delete Station 54

  55. Crafting Stations - Delete Station 55

  56. Crafting Stations - Delete Station 56

  57. Crafting Stations - Delete Station 57

  58. 58

  59. Crafting Stations - Update Station 59

  60. Crafting Stations - Update Station 60

  61. Crafting Stations - Update Station 61

  62. 62

  63. CRUD Complete! 63 ▰ Just like that we have a

    CRUD complete API ▰ We will repeat the same for podcasts ▰ Users is handled using graphql-jwt. This is used to leverage JSON web tokens with GraphQL and Django’s user model ▰ Code can be found on https://github.com/LiamNorman/building-apis-in-graphql
  64. One-to-one connections modelling 64

  65. One-to-one connections modelling 65

  66. 66

  67. One-to-many connections - Favouriting Podcasts 67

  68. One-to-many connections - Favouriting Podcasts 68

  69. One-to-many connections - Favouriting Podcasts 69

  70. One-to-many connections - Favouriting Podcasts 70

  71. One-to-many connections - Favouriting Podcasts 71

  72. One-to-many connections - Querying Favourites 72

  73. 73 User is fetched from auth, implicit

  74. 74

  75. DEMO TIME

  76. WHEN NOT TO USE GRAPHQL 76 ▰ GraphQL is not

    a silver bullet ▰ Caching GraphQL queries can be difficult ▰ Useful in cases where you have complex datasets and evolving REST API’s ▰ Minimizes API calls but can hit database more (without caching layer)
  77. GITHUB REPO 77 ▰ Found at https://github.com/LiamNorman/building-apis-in-graphql

  78. RESOURCES - Learning GraphQL Book - GraphQL Queries and Mutations

    - Graphene Docs - How to GraphQL - Graphene Types - GraphQL Python Guide 78
  79. 79 THANKS! Any questions? You can find me on twitter

    at ▰ @liamjnorman
  80. CREDITS ▰ Presentation template by SlidesCarnival 80