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