Fireship · @fireship

GraphQL Explained in 100 Seconds

Uploaded February 13, 2021 · Length 2:22

GraphQL Explained in 100 Seconds

Summary

graphql explained as a query language for reading and mutating data in apis with a type system and schema.

  • graphql
  • api design
  • query language
  • schema definition
  • type system

Transcript preview

The first 2 minutes of the transcript. Select a timestamp to watch that moment on YouTube.

  1. 0:00

    graphql a query language for reading and

  2. 0:03

    mutating data in apis

  3. 0:05

    as a back-end developer graphql provides

  4. 0:08

    a type system

  5. 0:09

    where you can describe a schema for your

  6. 0:11

    data in turn

  7. 0:12

    this gives front-end consumers of the

  8. 0:14

    api the power to explore

  9. 0:16

    and request the exact data they need

  10. 0:18

    traditionally

  11. 0:19

    web developers have consumed apis using

  12. 0:21

    rest where data entities

  13. 0:23

    live on a bunch of urls on a server when

  14. 0:25

    a request is received

  15. 0:26

    the api responds with the full data

  16. 0:28

    payload of that entity

  17. 0:30

    that sounds simple enough but there are

  18. 0:31

    two potential drawbacks here

  19. 0:33

    we may need multiple entities at one

  20. 0:35

    time in which case

  21. 0:36

    each request is under fetching the

  22. 0:38

    actual data we want

  23. 0:39

    in other cases we may only want a small

  24. 0:42

    subset of a data entity

  25. 0:43

    in which case we need to over fetch from

  26. 0:45

    the api and that of course is bad for

  27. 0:48

    the environment

  28. 0:48

    how dare you instead of multiple urls a

  29. 0:51

    graphql api

  30. 0:52

    has a single entry point data is queried

  31. 0:55

    or fetched

  32. 0:56

    by describing it with a syntax that

  33. 0:58

    mirrors its return shape in json

  34. 1:00

    the front-end developer describes the

  35. 1:02

    data they want while the back-end

  36. 1:03

    developer

  37. 1:04

    writes code to resolve the request and

  38. 1:06

    it all happens in a syntax that can work

  39. 1:08

    with any programming language

  40. 1:09

    we can start defining a schema with our

  41. 1:11

    own custom objects using the type

  42. 1:13

    keyword

  43. 1:14

    a type can have multiple fields like a

  44. 1:16

    unique id

  45. 1:17

    and we'll make that required with a bang

  46. 1:19

    let's also give it integer and string

  47. 1:21

    values

  48. 1:21

    then create a relationship with another

  49. 1:23

    type a creator can have many videos

  50. 1:26

    which we can represent by wrapping the

  51. 1:27

    type in brackets on the flip side

  52. 1:29

    a video belongs to a creator now every

  53. 1:32

    graphql api

  54. 1:33

    has a query type which is the main entry

  55. 1:35

    point for a consumer of the api

  56. 1:38

    we can query a list of videos or an

  57. 1:40

    individual user based on their id

  58. 1:42

    that's how a consumer reads data but

  59. 1:43

    they may also want to mutate data

  60. 1:46

    in which case we implement a mutation

  61. 1:48

    type that defines how data can be

  62. 1:49

    modified on the api

  63. 1:51

    from there we can define code to resolve

  64. 1:53

    this data in any programming language

  65. 1:55

    once deployed any developer consuming

  66. 1:57

    this api will be able to explore it with

  67. 1:59

    a complete understanding