Se ha denunciado esta presentación.
Utilizamos tu perfil de LinkedIn y tus datos de actividad para personalizar los anuncios y mostrarte publicidad más relevante. Puedes cambiar tus preferencias de publicidad en cualquier momento.

GraphQL & Ratpack

237 visualizaciones

Publicado el

Slides of my presentation at https://gr8conf.eu/ about developing GraphQL applications with Groovy and Ratpack (https://ratpack.io/)

Publicado en: Software
  • Sé el primero en comentar

  • Sé el primero en recomendar esto

GraphQL & Ratpack

  1. 1. GraphQL & Ratpack@marioggar 1
  2. 2. Hey Hi Mario Garcia Twitter: @marioggar Github: mariogarcia Currently working for: kaleidos.net Member of Madrid GUG 2 . 1
  3. 3. Why this talk ? 2 . 2
  4. 4. 3 . 1
  5. 5. Today… GraphQL theory GraphQL + Groovy GraphQL + Ratpack Summary 3 . 2
  6. 6. GraphQL Theory 4 . 1
  7. 7. What is GraphQL ? Is a query language It uses a type system to de ned those queries 4 . 2
  8. 8. What is NOT ? It’s not a web framework It doesn’t have to do with HTTP necessarily And de nitely… GraphQL is not REST 4 . 3
  9. 9. 5 . 1
  10. 10. To exec your 1st GraphQL query De ne Data Types De ne Query Types De ne Schema Execute queries against the de ned schema 5 . 2
  11. 11. De ne Data Types type Contestant { name: String } type Raffle { id: String title: String contestants: [Contestant] noWinners: Int } http://facebook.github.io/graphql/ 5 . 3
  12. 12. De ne Queries Query is just another type It de nes all possible queries over de ned types Queries can use other types or scalars Queries may have arguments type Query { list: [Raffle] getWinners(raffleId: String!): [Contestant] } 5 . 4
  13. 13. De ne Schema Which is the type for queries Which is the type for mutations type schema { query: Query mutation: Mutation } 5 . 5
  14. 14. Execute a query query: "give me (id, title) of all raf es result: list of available raf es { list { id title } } { "data": { "list": [ { "id": "pfoqjfq09w8jf", "title": "A Greach T-Shirt" } ] } } 5 . 6
  15. 15. Validation raf eId was mandatory Invalid query type Query { ... winners(raffleId: String!): [Contestant] ... } { winners { name } } 5 . 7
  16. 16. 5 . 8
  17. 17. GraphQL and JVM https://github.com/graphql-java 5 . 9
  18. 18. 5 . 10
  19. 19. First Groovy example Execute a simple query GQL ( ) DSL on top of https://github.com/grooviter/gql GraphQL-Java 5 . 11
  20. 20. GOTO code 5 . 12
  21. 21. Time to expose our schema through HTTP 5 . 13
  22. 22. 6 . 1
  23. 23. HTTP + GraphQL 6 . 2
  24. 24. Resources vs Queries (I) 6 . 3
  25. 25. Resources FrontEnd Has to coordinate data coming from different resources Several calls to different endpoints 6 . 4
  26. 26. Resources Backend New controller/handler for every new resource Implement different data views Input formal validation 6 . 5
  27. 27. Resources vs Queries (II) 6 . 6
  28. 28. Faster interaction w Front End Queries and Mutations can be batched Queries are executed in parallel No more controllers / handlers GraphQL engine takes care of input formal validation 6 . 7
  29. 29. 6 . 8
  30. 30. Ratpack + GraphQL http://ratpack.io 6 . 9
  31. 31. Why Ratpack ? Is lightweight It is a lean and powerful foundation, not an all- encompassing framework. Very good performance Netty + Ratpack’s execution model 6 . 10
  32. 32. Exposing GraphQL with Ratpack gql-ratpack Module of GQL project It provides: GraphQL endpoint G hiQ l https://grooviter.github.io/gql/docs/html5/index.html#_ratpack 6 . 11
  33. 33. Documentation: GraphiQL Single Page Application (React) Tool for prototyping queries Connects to a existing GraphQL/HTTP engine 6 . 12
  34. 34. Documentation: Alternatives https://nordicapis.com/10-graphql-consoles-in-action/ 6 . 13
  35. 35. GOTO code 6 . 14
  36. 36. 7 . 1
  37. 37. Ratpack's execution model 7 . 2
  38. 38. Ratpack's execution model (II) 7 . 3
  39. 39. Honor Ratpack's executors CompletableFuture can be used as a service return type Access executors via ratpack.exec.Execution Execute CompletableFuture using the proper executor Create your own abstractions 7 . 4
  40. 40. Abstractions @Override CompletableFuture<List<Map>> listCookies(DataFetchingEnvironment env) { Selectors.ListCookiesParams params = Selectors.listCookiesParams(env) return Futures.blocking { // <-- blocking operation repository.list(params) } } 7 . 5
  41. 41. 8 . 1
  42. 42. Error Handling REST ⇒ HTTP codes GraphQL ⇒ rich messages 8 . 2
  43. 43. Error vs Exception graphql.GraphQLError has meaninful error data to the client graphql.GraphQLException has more information for developers 8 . 3
  44. 44. Error { "data": null, "errors": [ { "message": "Validation error of type MissingFieldArgument: Missing "locations": [ { "line": 2, "column": 3 } ], "description": "Missing field argument raffleId", "validationErrorType": "MissingFieldArgument", "errorType": "ValidationError", "path": null, "extensions": null } 8 . 4
  45. 45. Exception { "data": { "winners": null }, "errors": [ { "message": "Exception while fetching data (/winners) : null", "path": [ "winners" ], "exception": { "cause": { "cause": null, "stackTrace": [ { "methodName": "newInstance0", "fileName": "NativeConstructorAccessorImpl.java", 8 . 5
  46. 46. Error >> Exception Throwing an exception is expensive Returning an error is just a simple data structure 8 . 6
  47. 47. Error >> Exception (II) You need to be aware of instrumentations 8 . 7
  48. 48. 8 . 8
  49. 49. Instrumentations Middleware executed before/after elds and data fetchers Error handling, security, tracing… you name it! Can be chained Can modify the behavior of the execution ow 8 . 9
  50. 50. Instrumentations 8 . 10
  51. 51. A good example… security 8 . 11
  52. 52. 9 . 1
  53. 53. Security Did I mention that GraphQL is a query language ? It knows nothing about security mechanisms We have to make the engine aware of security… Using the Context 9 . 2
  54. 54. Context Can carry information from outside to the GraphQL execution ow Can be any type of data structure (Map, List…) Available through all the execution ow (instrumentations and data fetchers) 9 . 3
  55. 55. Two strategies 9 . 4
  56. 56. Using Instrumentation More control Harder to integrate with Ratpack’s execution model 9 . 5
  57. 57. Using REST + Instrumentation You can use already implemented integrations (Pac4j) Easier to integrate with Ratpack’s execution runtime 9 . 6
  58. 58. Using REST + Instrumentation… gql-ratpack embraces this choice gql.ratpack.pac4j.GraphQLHandler 9 . 7
  59. 59. GOTO code 9 . 8
  60. 60. What is left ? 10 . 1
  61. 61. 10 . 2
  62. 62. A lot but… 10 . 3
  63. 63. Relay GraphQL good practices https://facebook.github.io/relay/ http://graphql.org/learn/best-practices/ 10 . 4
  64. 64. Alternatives to Ratpack + GQL gorm-graphql with/without Grails, Micronaut… If you like GORM, and ts your plan, use it! graphql-java DIY https://github.com/grails/gorm-graphql h // h l j d h d i / /l / 10 . 5
  65. 65. 11 . 1
  66. 66. Summary: GraphQL Is not a silver bullet Is an improvement between back/front GraphQL It’s not limited to HTTP As a query language, is still underestimated 11 . 2
  67. 67. Questions ? 11 . 3
  68. 68. Thanks! 11 . 4

×