Upload
yuki-katada
View
308
Download
7
Embed Size (px)
Citation preview
Introduction to GraphQL in Scala
Yuki KatadaScalaMatsuri 2017
Scalaで始める GraphQL
About MeName
• 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 :(自己紹介
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を使用しています。
What is GraphQL?
GraphQLとは?
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の代替としての立ち位置のクエリ言語です。
Why GraphQL?
なぜ GraphQLなのか?
Multiple requests in REST
Endpoints/users/books
/companies...
I want resources at “/users”
and“/books”
and“/companies”
REST
server client
database
UsersBooks
Companies
RESTでは複数リソースを取得するために複数のリクエストが必要となります。
Multiple requests in REST
Resources
Users
Books
Companies
/users
/books
/companies
Endpoints
複数のリソースに対して複数のエンドポイントが紐付いています。
REST
REST requires n requests to retrieve n different resources.So if website required 100 different resources…?
RESTでは大量のリクエストが発生する可能性がある。
Single request in GraphQL
Endpoint/graphql
I want resources at “/users”
and“/books”
and“/companies”
GraphQL
server client
database
UsersBooks
Companies
GraphQLでは単一のリクエストのみで複数のリソースが取得できます。
Single request in GraphQL
Resources
Users
Books
Companies
/graphql
Endpoint
“query” : “{ user book company }”
Request Body
なので、 1つのエンドポイントに複数のリソースが紐付いている状態です。
GraphQL
GraphQL in Scala
Scalaでの GraphQL
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」
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
今回の例では、ダミーデータを使用した例で説明します。
In DummyDataRepo class, I defined 3 methods to retrieve each resource.
DummyRepo.scala
ダミーデータを取得するレポジトリクラスを定義します。
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
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リクエストした場合の例です。
Please look at my sample code on Github for more detail.
https://github.com/yuki-katada/scalamatsuri_sangria
詳細は Githubを参照してください。
Pros/Cons
実際に使ってみて見えてきた長所と短所
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
長所
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
短所
Result
Cons are too critical compared to pros…
So we decided to replace GraphQL with REST :(
結果
Thank you for listening :)