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. 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を使用しています。
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の代替としての立ち位置のクエリ言語です。
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では複数リソースを取得するために複数のリクエストが
必要となります。
9. REST requires n requests to retrieve n different resources.
So if website required 100 different resources…?
RESTでは大量のリクエストが発生する可能性がある。
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. Single request in GraphQL
Resources
Users
Books
Companies
/graphql
Endpoint
“query” :
“{
user
book
company
}”
Request Body
なので、1つのエンドポイントに複数のリソースが紐付いてい
る状態です。
GraphQL
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. 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. In DummyDataRepo class,
I defined 3 methods to
retrieve each resource.
DummyRepo.scala
ダミーデータを取得するレポジトリクラスを定義します。
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. Please look at my sample code on Github for more detail.
https://github.com/yuki-katada/scalamatsuri_sangria
詳細はGithubを参照してください。
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. 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. Result
Cons are too critical compared to pros…
So we decided to replace GraphQL with REST :(
結果