Introduction to GraphQL
in Scala
Yuki Katada
ScalaMatsuri 2017
Scalaで始めるGraphQL
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 :(
自己紹介
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
Users
Books
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
Users
Books
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 :)

Introduction to GraphQL in Scala (ScalaMatsuri 2017)

  • 1.
    Introduction to GraphQL inScala Yuki Katada ScalaMatsuri 2017 Scalaで始めるGraphQL
  • 2.
    About Me Name • YukiKatada (片田雄樹) 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 • Weare 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.
  • 5.
    What is GraphQL? GraphQLis 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.
  • 7.
    Multiple requests inREST Endpoints /users /books /companies . . . I want resources at “/users” and “/books” and “/companies” REST server client database Users Books Companies RESTでは複数リソースを取得するために複数のリクエストが 必要となります。
  • 8.
    Multiple requests inREST Resources Users Books Companies /users /books /companies Endpoints 複数のリソースに対して複数のエンドポイントが紐付いていま す。 REST
  • 9.
    REST requires nrequests to retrieve n different resources. So if website required 100 different resources…? RESTでは大量のリクエストが発生する可能性がある。
  • 10.
    Single request inGraphQL Endpoint /graphql I want resources at “/users” and “/books” and “/companies” GraphQL server client database Users Books Companies GraphQLでは単一のリクエストのみで複数のリソースが取得で きます。
  • 11.
    Single request inGraphQL Resources Users Books Companies /graphql Endpoint “query” : “{ user book company }” Request Body なので、1つのエンドポイントに複数のリソースが紐付いてい る状態です。 GraphQL
  • 12.
  • 13.
    GraphQL library forScala 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, Idefined 3 methods to retrieve each resource. DummyRepo.scala ダミーデータを取得するレポジトリクラスを定義します。
  • 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.
    When you retrieveresources 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 atmy sample code on Github for more detail. https://github.com/yuki-katada/scalamatsuri_sangria 詳細はGithubを参照してください。
  • 19.
  • 20.
    Pros • Less HTTPrequests (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 isvery 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 toocritical compared to pros… So we decided to replace GraphQL with REST :( 結果
  • 23.
    Thank you forlistening :)