Diese Präsentation wurde erfolgreich gemeldet.
Wir verwenden Ihre LinkedIn Profilangaben und Informationen zu Ihren Aktivitäten, um Anzeigen zu personalisieren und Ihnen relevantere Inhalte anzuzeigen. Sie können Ihre Anzeigeneinstellungen jederzeit ändern.

TechEvent Introduction to GraphQL

37 Aufrufe

Veröffentlicht am

Introduction to GraphQL

Veröffentlicht in: Technologie
  • Als Erste(r) kommentieren

  • Gehören Sie zu den Ersten, denen das gefällt!

TechEvent Introduction to GraphQL

  1. 1. BASEL BERN BRUGG DÜSSELDORF FRANKFURT A.M. FREIBURG I.BR. GENEVA HAMBURG COPENHAGEN LAUSANNE MUNICH STUTTGART VIENNA ZURICH Introduction to GraphQL Alexander Laukert, Colin Hoffmann
  2. 2. Introduction to GraphQL3 10/3/2018 Problem
  3. 3. RPC and RESTish Introduction to GraphQL4 03.10.2018 HTTP GET HTTP GET HTTP GET /users/<id> /users/<id>/posts /posts/<id>/comments /users/<id> /users/<id>/posts /posts/<id>/comments /users/<id> /users/<id>/posts /posts/<id>/comments 100 ms n x 100 ms 100 ms (n) RESTish (RPC)
  4. 4. RPC and RESTish Introduction to GraphQL5 03.10.2018 HTTP GET HTTP GET ? /users/<id> /users/<id>/posts /posts/<id>/comments /users/<id>/withAll /users/<id> /users/<id>/posts /posts/<id>/comments /users/<id>/withAll /users/<id>/withPosts /users/<id> /users/<id>/posts /posts/<id>/comments /users/<id>/withAll
  5. 5. query { User(id: 123) { name posts { title body creationDate } } } "data": { "User": { "name": “John Doe", "posts": [ { "title": "Learn GraphQL today", "body": "…", "creationDate": "2018-09-14" }, { … } ] } } GraphQL Introduction to GraphQL6 03.10.2018 HTTP POST
  6. 6. Introduction to GraphQL7 10/3/2018 What is GraphQL?
  7. 7. Queries Introduction to GraphQL8 10/3/2018 GraphQL is a data query and manipulation language "data": { "user": { "fullName": "John Doe", "posts": [ { "id": 2, "body": "Lorem Ipsum", "comments": [ { "body": "Great Post!" } ] } ] } } user(id: 123) { fullName posts { id body comments(first: 1) { body } } }
  8. 8. Mutations and Subscriptions Introduction to GraphQL9 10/3/2018 GraphQL provides ways to query, mutate or subscribe to data mutation CreateComment( $postId: ID = 123, $data: CommentInput!) { createComment( postId: $postId, comment: $data) { id creationDate } } subscription UserPostSubscription { userPosts(userId:123) { id title body } }
  9. 9. Schema Introduction to GraphQL10 10/3/2018 GraphQL APIs have a strongly typed schema schema { query: Query } type Query { user(id: ID!): User } type User { id: ID! fullName: String! email: String posts: [Post!]! } type Post { id: ID! title: String! body: String comments(first: Int): [Comment!]! } type Comment { id: ID! author: User! authorName: String! @deprecated body: String stars: Int }
  10. 10. Introspection Introduction to GraphQL11 10/3/2018 GraphQL offers built-in introspection support __type(name: "User") { name kind fields { name type { name kind } } } "data": { "__type": { "name": "User", "kind": "OBJECT", "fields": [ { "name": "id", "type": { "name": "Int", "kind": "SCALAR" } }, { "name": "email", "type": { "name": "String", "kind": "SCALAR" } } ...
  11. 11. Introduction to GraphQL12 10/3/2018 How does it work?
  12. 12. Resolver Introduction to GraphQL13 03.10.2018 µ-Service DB Legacy query { User(id: 123) { name legacy_id posts { title body } } } UserResolver ?
  13. 13. query { User(id: 123) { name posts(first: 3) { title body comments { text rating } } } } Dynamic SQL-Queries Introduction to GraphQL14 03.10.2018 User PK id name Post PK id FK user_id title body Comment PK id FK post_id text rating SELECT user FROM User user WHERE user.id = :id SELECT post FROM Post post WHERE post.user_id = :userId // query.setMaxResults(3) SELECT c FROM Comment c WHERE c.post_id = :postId 1 n 1 n UserResolver PostResolver CommentResolver 50 ms 50 ms n x 50 ms (n)
  14. 14. Fetching Strategies Introduction to GraphQL15 03.10.2018 Resolver Resolver Resolver Resolver Resolver Resolver naive caching batching concurrent foresightful n 0-nn n 1n n n* 1 0
  15. 15. query { User(id: 123) { name posts(first: 3) { title body comments { text rating } } } } Batching Strategy Introduction to GraphQL16 03.10.2018 User PK id name Post PK id FK user_id title body Comment PK id FK post_id text rating SELECT user FROM User user WHERE user.id = :id SELECT post FROM Post post WHERE post.user_id = :userId // query.setMaxResults(3) SELECT c FROM Comment c WHERE c.post_id IN :postIds 1 n 1 n UserResolver PostResolver CommentResolver 50 ms 50 ms 50 ms (n) SELECT c FROM Comment c WHERE c.post_id = :postId n x 50 ms
  16. 16. SELECT c FROM Comment c WHERE c.post_id IN :postIds SELECT post FROM Post post LEFT JOIN FETCH Comment c WHERE post.user_id = :userId query { User(id: 123) { name posts(first: 3) { title body comments { text rating } } } } Foresightful Strategy Introduction to GraphQL17 03.10.2018 User PK id name Post PK id FK user_id title body Comment PK id FK post_id text rating SELECT user FROM User user WHERE user.id = :id SELECT post FROM Post post WHERE post.user_id = :userId // query.setMaxResults(3) 1 n 1 n UserResolver PostResolver CommentResolver 50 ms 50 ms 50 ms (n) // query.setMaxResults(3)
  17. 17. Introduction to GraphQL18 10/3/2018 Conclusion
  18. 18. Conclusion Name Presentation19 03.10.2018 • GraphQL solves under-/overfetching and n + 1 problem while keeping the API clean • Rapid product iterations on frontend due to schema-driven development • GraphQL facilitates CRUD operations as well as subscriptions to data changes • Strongly typed contracts mitigate error-proneness and form ideal documentation • Resolvers are dynamically invoked to retrieve data from single/multiple sources • Different (combinable) strategies exists to retrieve data in the most efficient way
  19. 19. Introduction to GraphQL20 10/3/2018 Further information… APIs You Won‘t Hate • Understanding RPC, REST and GraphQL • GraphQL vs REST: Overview • Picking the right API Paradigm GraphQL • Github (Learn) • GraphQL Tutorial • GraphQL-Java
  20. 20. Questions and answers… Alexander Laukert Tel. +41 58 459 51 76 alexander.laukert@trivadis.com 03.10.2018 Introduction to GraphQL21 Colin Hoffmann Tel. +41 58 459 51 97 colin.hoffmann@trivadis.com
  21. 21. Session Feedback – now TechEvent September 201822 14.09.2018 Please use the Trivadis Events mobile app to give feedback on each session Use "My schedule" if you have registered for a session Otherwise use "Agenda" and the search function If the mobile app does not work (or if you have a Windows smartphone), use your smartphone browser – URL: http://trivadis.quickmobileplatform.eu/ – User name: <your_loginname> (such as "svv") – Password: sent by e-mail...

×