Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Introduction to GraphQL in Scala

282 views

Published on

http://2017.scalamatsuri.org/

Published in: Software
  • Be the first to comment

  • Be the first to like this

Introduction to GraphQL in Scala

  1. 1. Introduction to GraphQL in Scala Yuki Katada ScalaMatsuri 2017 Scalaで始めるGraphQL
  2. 2. About Me Name • Yuki Katada (片田雄樹) Affiliation • CyberAgent, Inc. • Media Development Headquarters (メディアディベロップメント事業本部) Scala Experience • One and a half years Follow Me • On Twitter @ponyoky ← very few ppl are following me :( 自己紹介
  3. 3. Our Job • We are developing Advertising Network, which delivers Ads to multiple media such as CyberAgent “Ameba”. • Approximate 700 users are using our product. • Our product has more than 600 million incoming traffics per day • We are using GraphQL at Admin Page, which requires complex queries 我々のプロダクトはAmebaメディアに対する広告を配信するシ ステムで、その管理画面部分でGraphQLを使用しています。
  4. 4. What is GraphQL? GraphQLとは?
  5. 5. What is GraphQL? GraphQL is a query language, which is developed by Facebook. It provides an alternative to REST. (Not Graph DB) Some GraphQL libraries are provided as following: • Javascript • Python • Scala • and more. GraphQLとはRESTの代替としての立ち位置のクエリ言語です。
  6. 6. Why GraphQL? なぜGraphQLなのか?
  7. 7. Multiple requests in REST Endpoints /users /books /companies . . . I want resources at “/users” and “/books” and “/companies” REST server client database Users Books Companies RESTでは複数リソースを取得するために複数のリクエストが 必要となります。
  8. 8. Multiple requests in REST Resources Users Books Companies /users /books /companies Endpoints 複数のリソースに対して複数のエンドポイントが紐付いていま す。 REST
  9. 9. REST requires n requests to retrieve n different resources. So if website required 100 different resources…? RESTでは大量のリクエストが発生する可能性がある。
  10. 10. Single request in GraphQL Endpoint /graphql I want resources at “/users” and “/books” and “/companies” GraphQL server client database Users Books Companies GraphQLでは単一のリクエストのみで複数のリソースが取得で きます。
  11. 11. Single request in GraphQL Resources Users Books Companies /graphql Endpoint “query” : “{ user book company }” Request Body なので、1つのエンドポイントに複数のリソースが紐付いてい る状態です。 GraphQL
  12. 12. GraphQL in Scala ScalaでのGraphQL
  13. 13. GraphQL library for Scala Sangria ● The most famous Scala GraphQL library ● Github Repo ○ https://github.com/sangria-graphql/sangria ● There is an awesome documentation ○ http://sangria-graphql.org/ Scalaで実装されたGraphQLライブラリ「Sangria」
  14. 14. In my example, I prepared dummy data. These resources are same as a previous example; users, books, and companies In general, DummyData would be a database or data source. DummyData.scala 今回の例では、ダミーデータを使用した例で説明します。
  15. 15. In DummyDataRepo class, I defined 3 methods to retrieve each resource. DummyRepo.scala ダミーデータを取得するレポジトリクラスを定義します。
  16. 16. GraphQLの根幹となる、オブジェクト構造を定義したクラスの 例です。 This is SchemaDefinition object, which defines Single Endpoint, Arguments, and Object Structures. In GraphQL, we have to define Object Structures to map onto query AST of request body. SchemaDefinition.scala
  17. 17. When you retrieve resources from this API, you need to access in HTTP Post method. In this example, I requests user data with id = 1, and the right side JSON shows response. GraphQLクエリでAPIリクエストした場合の例です。
  18. 18. Please look at my sample code on Github for more detail. https://github.com/yuki-katada/scalamatsuri_sangria 詳細はGithubを参照してください。
  19. 19. Pros/Cons 実際に使ってみて見えてきた長所と短所
  20. 20. Pros • Less HTTP requests (less API access) compared to REST • Additional server side implementation is not required in many cases – Only client side modification to GraphQL query is required • GraphiQL makes tests/debugs easier – An interactive in-browser IDE for GraphQL 長所
  21. 21. Cons • It is very hard to design Domain-Specific architecture • Client Side is required to write very long request body – Who wants to write string formatted & type-unsafe request body…? • Many people struggle with GraphQL query – It is hard to understand for many people 短所
  22. 22. Result Cons are too critical compared to pros… So we decided to replace GraphQL with REST :( 結果
  23. 23. Thank you for listening :)

×