Pro Yearly is on sale from $80 to $50! »

GraphQL From The Ground Up - SoftGeeks, Online, October 2020

GraphQL From The Ground Up - SoftGeeks, Online, October 2020

Video: Coming soon!

Abstract: You've heard a lot of people talking about GraphQL lately, but what IS it? What are the problems it's supposed to be solving - and what are the problems it's _not_ supposed to be solving? How can you use it to improve your iOS development process? Ellen Shapiro, who maintains the Apollo GraphQL iOS SDK, will help untangle all this and more.

C4861b1dfdf3bbb21faec4a1acdf183d?s=128

Ellen Shapiro
PRO

October 21, 2020
Tweet

Transcript

  1. GRAPHQL FROM THE GROUND UP SOFTGEEKS | THE INTERNETS |

    OCTOBER 2020 ELLEN SHAPIRO | @DESIGNATEDNERD | APOLLOGRAPHQL.COM
  2. None
  3. None
  4. None
  5. WTF IS GRAPHQL?

  6. None
  7. WHAT IS A GRAPH?

  8. None
  9. WHAT IS A GRAPH? (WHEN WE TALK ABOUT GRAPHQL)

  10. ! GRAPH THEORY

  11. In computer science, graphs are used to represent networks of

    communication, data organization, computational devices, the flow of computation, etc. https://en.wikipedia.org/wiki/Graph_theory#Computer_science
  12. None
  13. Source: https://neo4j.com/blog/graph-theory-predictive-modeling/

  14. Source: https://neo4j.com/blog/graph-theory-predictive-modeling/

  15. Source: https://neo4j.com/blog/graph-theory-predictive-modeling/

  16. Source: https://neo4j.com/blog/graph-theory-predictive-modeling/

  17. DIRECTED GRAPH Source: https://neo4j.com/blog/graph-theory-predictive-modeling/

  18. None
  19. UNDIRECTED GRAPH

  20. NODES INDIVIDUAL PIECES OF DATA

  21. EDGES RELATIONSHIPS BETWEEN PIECES OF DATA

  22. None
  23. SO IS A RELATIONAL DATABASE A GRAPH? !

  24. NOT QUITE.

  25. None
  26. None
  27. None
  28. None
  29. RELATIONAL DATABASES ARE FOCUSED ON SINGLE RELATIONSHIPS

  30. GRAPHS HELP YOU TO TRACK MANY RELATIONSHIPS

  31. user.father.bestFriend.children[0].middleName

  32. ✨ GRAPHQL!

  33. GRAPH QUERY LANGUAGE

  34. HOW DO I GRAPHQL?

  35. SAMPLE SERVER HTTPS://APOLLO-FULLSTACK-TUTORIAL.HEROKUAPP.COM/

  36. THE SCHEMA

  37. THE SCHEMA WHAT IS IT POSSIBLE TO ASK FOR?

  38. SCHEMA DEFINITION LANGUAGE

  39. SCHEMA DEFINITION LANGUAGE (BASICALLY, TYPESCRIPT)

  40. None
  41. None
  42. None
  43. None
  44. SCALAR TYPES

  45. SCALAR TYPES THE BUILDING BLOCKS OF THE TYPE SYSTEM

  46. OPTIONALS?!

  47. ✍ POP QUIZ!

  48. Q. IS THIS TYPE OPTIONAL?

  49. Q. IS THIS TYPE OPTIONAL? String

  50. Q. IS THIS TYPE OPTIONAL? String A: IT DEPENDS

  51. None
  52. None
  53. IS OPTIONAL SWIFT TYPE GRAPHQL TYPE

  54. IS OPTIONAL SWIFT TYPE GRAPHQL TYPE No

  55. IS OPTIONAL SWIFT TYPE GRAPHQL TYPE No String

  56. IS OPTIONAL SWIFT TYPE GRAPHQL TYPE No String String!

  57. !

  58. GRAPHQL TYPES ARE NULLABLE BY DEFAULT

  59. IS OPTIONAL SWIFT TYPE GRAPHQL TYPE No String String!

  60. IS OPTIONAL SWIFT TYPE GRAPHQL TYPE No String String! Yes

  61. IS OPTIONAL SWIFT TYPE GRAPHQL TYPE No String String! Yes

    String?
  62. IS OPTIONAL SWIFT TYPE GRAPHQL TYPE No String String! Yes

    String? String
  63. None
  64. OPERATIONS

  65. OPERATIONS WHAT ARE THE ENTRY POINTS TO THE GRAPH?

  66. QUERY: PLEASE GIVE ME THIS DATA

  67. None
  68. MUTATION: PLEASE CHANGE THIS DATA

  69. SUBSCRIPTION: PLEASE UPDATE ME IF DATA CHANGES

  70. None
  71. ✅ HOW THINGS ARE RELATED

  72. ✅ HOW THINGS ARE RELATED ✅ WHAT YOU CAN ASK

    FOR
  73. ✅ HOW THINGS ARE RELATED ✅ WHAT YOU CAN ASK

    FOR ➡ HOW YOU ASK FOR IT
  74. GRAPHIQL

  75. None
  76. None
  77. None
  78. None
  79. None
  80. None
  81. None
  82. None
  83. None
  84. None
  85. None
  86. None
  87. None
  88. None
  89. None
  90. None
  91. None
  92. None
  93. HOW WOULD REST DO THIS?

  94. None
  95. None
  96. None
  97. None
  98. None
  99. None
  100. None
  101. VALIDATION

  102. None
  103. !

  104. VALIDATION IS WHAT I'M TRYING TO ASK FOR EVEN POSSIBLE?

  105. None
  106. None
  107. None
  108. VALIDATION + TYPES = CODE GENERATION

  109. APOLLO IOS

  110. None
  111. ⬆ (IT ME)

  112. None
  113. None
  114. A VERY SHORTENED VERSION OF APOLLO'S GENERATED CODE

  115. public final class LaunchInfoQuery: GraphQLQuery { public init(id: GraphQLID) {

    self.id = id } public struct Data: GraphQLSelectionSet { public var launch: Launch? { public struct Launch: GraphQLSelectionSet { public var site: String? public var mission: Mission? public var rocket: Rocket? public struct Mission: GraphQLSelectionSet { public var name: String? } public struct Rocket: GraphQLSelectionSet { public var name: String? } } } }
  116. public final class LaunchInfoQuery: GraphQLQuery { public init(id: GraphQLID) {

    self.id = id } public struct Data: GraphQLSelectionSet { public var launch: Launch? { public struct Launch: GraphQLSelectionSet { public var site: String? public var mission: Mission? public var rocket: Rocket? public struct Mission: GraphQLSelectionSet { public var name: String? } public struct Rocket: GraphQLSelectionSet { public var name: String? } } } }
  117. public final class LaunchInfoQuery: GraphQLQuery { public init(id: GraphQLID) {

    self.id = id } public struct Data: GraphQLSelectionSet { public var launch: Launch? { public struct Launch: GraphQLSelectionSet { public var site: String? public var mission: Mission? public var rocket: Rocket? public struct Mission: GraphQLSelectionSet { public var name: String? } public struct Rocket: GraphQLSelectionSet { public var name: String? } } } }
  118. public final class LaunchInfoQuery: GraphQLQuery { public init(id: GraphQLID) {

    self.id = id } public struct Data: GraphQLSelectionSet { public var launch: Launch? { public struct Launch: GraphQLSelectionSet { public var site: String? public var mission: Mission? public var rocket: Rocket? public struct Mission: GraphQLSelectionSet { public var name: String? } public struct Rocket: GraphQLSelectionSet { public var name: String? } } } }
  119. None
  120. public final class LaunchInfoQuery: GraphQLQuery { public init(id: GraphQLID) {

    self.id = id } public struct Data: GraphQLSelectionSet { public var launch: Launch? { public struct Launch: GraphQLSelectionSet { public var site: String? public var mission: Mission? public var rocket: Rocket? public struct Mission: GraphQLSelectionSet { public var name: String? } public struct Rocket: GraphQLSelectionSet { public var name: String? } } } }
  121. public final class LaunchInfoQuery: GraphQLQuery { public init(id: GraphQLID) {

    self.id = id } public struct Data: GraphQLSelectionSet { public var launch: Launch? { public struct Launch: GraphQLSelectionSet { public var site: String? public var mission: Mission? public var rocket: Rocket? public struct Mission: GraphQLSelectionSet { public var name: String? } public struct Rocket: GraphQLSelectionSet { public var name: String? } } } }
  122. YOU DON'T HAVE TO WRITE THIS CODE

  123. YOU DON'T HAVE TO WRITE THIS CODE (TIMES THE NUMBER

    OF OPERATIONS)
  124. YOU DON'T HAVE TO WRITE A URLSESSION WRAPPER

  125. None
  126. YOU DO HAVE TO WRITE SOME CODE

  127. apolloClient.fetch(query: LaunchInfoQuery(id: "80")) { result in switch result { case

    .failure(let error): print("Error: \(error)") case .success(let graphQLResult): if let errors = graphQLResult.errors { print("Errors: \(errors)") } if let missionName = graphQLData?.data?.launch?.mission?.name { print("Mission name: \(String(describing: missionName)") } } }
  128. apolloClient.fetch(query: LaunchInfoQuery(id: "80")) { result in switch result { case

    .failure(let error): print("Error: \(error)") case .success(let graphQLResult): if let errors = graphQLResult.errors { print("Errors: \(errors)") } if let missionName = graphQLData?.data?.launch?.mission?.name { print("Mission name: \(String(describing: missionName)") } } }
  129. apolloClient.fetch(query: LaunchInfoQuery(id: "80")) { result in switch result { case

    .failure(let error): print("Error: \(error)") case .success(let graphQLResult): if let errors = graphQLResult.errors { print("Errors: \(errors)") } if let missionName = graphQLData?.data?.launch?.mission?.name { print("Mission name: \(String(describing: missionName)") } } }
  130. apolloClient.fetch(query: LaunchInfoQuery(id: "80")) { result in switch result { case

    .failure(let error): print("Error: \(error)") case .success(let graphQLResult): if let errors = graphQLResult.errors { print("Errors: \(errors)") } if let missionName = graphQLData?.data?.launch?.mission?.name { print("Mission name: \(missionName) } } }
  131. None
  132. PARTIAL RESULTS

  133. apolloClient.fetch(query: LaunchInfoQuery(id: "80")) { result in switch result { case

    .failure(let error): print("Error: \(error)") case .success(let graphQLResult): if let errors = graphQLResult.errors { print("Errors: \(errors)") } if let missionName = graphQLData?.data?.launch?.mission?.name { print("Mission name: \(missionName) } } }
  134. apolloClient.fetch(query: LaunchInfoQuery(id: "80")) { result in switch result { case

    .failure(let error): print("Error: \(error)") case .success(let graphQLResult): if let errors = graphQLResult.errors { print("Errors: \(errors)") } if let missionName = graphQLData?.data?.launch?.mission?.name { print("Mission name: \(missionName) } } }
  135. USE SWIFT EXTENSIONS TO HANDLE NULLABILITY GRACEFULLY

  136. extension LaunchInfoQuery.Data.Launch { var missionName: String { self.mission?.name ?? "(Unknown)"

    } }
  137. None
  138. WHAT IS GRAPHQL GOOD FOR?

  139. GET WHAT YOU WANT

  140. GET WHAT YOU WANT NOT WHAT FRONTEND, ANDROID, 3RD PARTY

    APIS, AND YOUR GRANDMA WANT
  141. YOU CAN ADD STUFF TO A GRAPH WAY FASTER THAN

    YOU CAN WITH A REST API
  142. A REST PLAY Me: Can we add this field that's

    on endpoint A to endpoint B? Backend: Sure, we'll put it in the backlog.
  143. None
  144. ANYTHING IN THE SCHEMA CAN BE ACCESSED VIA THE RELATIONSHIPS

    DEFINED IN THE GRAPH
  145. user.father.bestFriend.children[0].middleName

  146. user.father?.bestFriend.children?[0].middleName

  147. WHAT IS GRAPHQL NOT GOOD FOR?

  148. UPLOADING FILES

  149. None
  150. BETTER GRAPHQL UPLOADS

  151. BETTER GRAPHQL UPLOADS 1. Upload to something that returns a

    URL
  152. BETTER GRAPHQL UPLOADS 1. Upload to something that returns a

    URL 2. Send the URL to your graph using GraphQL
  153. BETTER GRAPHQL UPLOADS 1. Upload to something that returns a

    URL 2. Send the URL to your graph using GraphQL 3. Tear out way less hair than trying to upload with GQL
  154. HIGHLY OPTIMIZED QUERIES

  155. GQL TOOLING HELPS FIND BOTTLENECKS

  156. GQL TOOLING HELPS FIND BOTTLENECKS (BUT IT CAN'T ELIMINATE THEM)

  157. BEING ABLE TO ! COMPLETELY REMOVE THINGS QUICKLY

  158. GOOD NEWS: NO VERSIONING

  159. BAD NEWS: NO VERSIONING

  160. @deprecated

  161. None
  162. ! → " →

  163. SIMPLE SECURITY

  164. YOU CAN ASK FOR ANYTHING YOU WANT!

  165. DEVELOPERS

  166. SECURITY TEAM

  167. None
  168. None
  169. YOU'RE GONNA HAVE TO THINK MORE GRANULARLY ABOUT SECURITY

  170. MAYBE DON'T LEAVE SCHEMA INTROSPECTION UNPROTECTED

  171. OBLIGATORY SUMMARY SLIDE

  172. OBLIGATORY SUMMARY SLIDE ▸ A Graph is a way of

    conceptualizing data based on pieces of data (nodes) and the ways those pieces are connected (edges).
  173. OBLIGATORY SUMMARY SLIDE ▸ A Graph is a way of

    conceptualizing data based on pieces of data (nodes) and the ways those pieces are connected (edges). ▸ GraphQL is a way of querying your data and how it's connected
  174. OBLIGATORY SUMMARY SLIDE ▸ A Graph is a way of

    conceptualizing data based on pieces of data (nodes) and the ways those pieces are connected (edges). ▸ GraphQL is a way of querying your data and how it's connected ▸ The Schema defines what you can ask for (with types!)
  175. OBLIGATORY SUMMARY SLIDE ▸ A Graph is a way of

    conceptualizing data based on pieces of data (nodes) and the ways those pieces are connected (edges). ▸ GraphQL is a way of querying your data and how it's connected ▸ The Schema defines what you can ask for (with types!) ▸ Operations let you ask for and receive only the data you want
  176. OBLIGATORY SUMMARY SLIDE ▸ A Graph is a way of

    conceptualizing data based on pieces of data (nodes) and the ways those pieces are connected (edges). ▸ GraphQL is a way of querying your data and how it's connected ▸ The Schema defines what you can ask for (with types!) ▸ Operations let you ask for and receive only the data you want ▸ GraphQL is a real cool hammer - not every problem is a nail
  177. WANT TO TRY APOLLO-IOS? https://www.apollographql.com/docs/ios/tutorial/ THANK YOU!