Functional APIs with
Absinthe GraphQL
Zvi Avraham
zvi@nivertech.com
$ whoami
zvi
● Distributed Systems
○ Realtime Dataflow clusters 6PT/day
○ Scalable Messaging (3M sessions/node)
○ NoSQL Databases
● Crypto
○ Blockchain & Smart Contracts
○ Cryptoeconomics
○ Token Engineering
Agenda
● briefly introduce GraphQL
● Introduce Elixir
● Introduce Ecto
● give overview of Absinthe toolkit
● show an example of implementing GraphQL
API for Contract List app using Absinthe &
Phoenix
Assumptions
● Reading basic Elixir code
○ Similar to Ruby
● Know at least one modern web framework
○ Rails, Django, etc.
● Basic intuitive understanding of GraphQL
○ JSON QBE (Query-by-Example)
Why GraphQL?
Let’s assume the boss told us to use it ;)
Front end Back end
Front end Back end
Front end Back end
Front end Back end
Front end Back end
GraphiQL query tool Back end
GraphiQL query tool Back end
Front end Back end
GraphQL
schema
Schema - a contract between Backend & Frontend
Front end Back end
GraphQL
schema
Schema - a contract between Backend & Frontend
GraphQL Schema
● Schema != Data Model
● It’s a Logical Data Model using graph paradigm
● Your Physical Data Model can be very different
○ Database (Relational, K/V, GraphDB, etc.)
○ Fetching data from external APIs
○ Computed on demand
In Schema we define:
● Types
● Operations:
○ Queries
○ Mutations
○ Subscriptions
=
or
Elixir
● Functional
● Concurrent
● Ruby-like syntax
● Compiled to Erlang bytecode
● Built on battle-proven BEAM VM
Elixir
● Dynamically typed
○ optional static type analyzer
● Pattern matching & guards
● Pipe operator “|>”
● Structs (like tagged maps)
● Hygienic macros
● No preprocessor like in Erlang
○ use attributes & macros instead
Intro to Ecto
Ecto is not an ORM, it’s a Data Access library
https://boylesoftware.com/blog/orm-must-go/
Ecto in a nutshell
● Repo
○ the repository of data storage. The Repo hides implementation details
from models. It can provide access to different data storages such as
databases or in-memory storages.
● Migrations
○ CREATE/ALTER/DROP TABLE/INDEX
● Schema
○ a mapping between a DB table and its columns with the Elixir struct
● Changeset
○ cast attributes value into correct types, add validation rules & constraints.
● Query
○ Internal DSL wrapper in Elixir around composable SQL queries.
Following slides are based on:
● Phoenix & Elm, a real use case (parts 1-6)
○ by Ricardo García Vega, @bigardone
○ http://codeloveandboards.com/blog/2017/02/02/phoenix-and-elm-a-real-use-case-pt-1/
○ https://github.com/bigardone/phoenix-and-elm
○ Live demo: https://phoenix-and-elm.herokuapp.com/
● Migrating a Phoenix & Elm app from REST to GraphQL
○ by Paul Fioravanti, @paulfioravanti
○ https://paulfioravanti.com/blog/2018/03/06/migrating-a-phoenix-and-elm-app-from-rest-to-graphql/
○ https://github.com/paulfioravanti/phoenix-and-elm - branch graphql
Phoenix Project Layout
Ecto adds :id field for primary key
field(:id, :integer)
Timestamp macro expands into 2
fields:
field(:inserted_at, :date)
field(:updated_at, :date)
The GraphQL toolkit for Elixir
https://absinthe-graphql.org/
Source: Craft GraphQL APIs in Elixir with Absinthe
https://pragprog.com/book/wwgraphql/craft-graphql-apis-in-elixir-with-absinthe
Source: Craft GraphQL APIs in Elixir with Absinthe
https://pragprog.com/book/wwgraphql/craft-graphql-apis-in-elixir-with-absinthe
Phoenix with Absinthe is simpler
We don’t need usual
Phoenix functionality:
● No controllers
● No views
● No templates
We only need to:
● Define schemas
● Define custom types
● Write resolvers
Ecto and Phoenix: Doing web with Elixir - Yurii Bodarev
https://www.slideshare.net/Elixir-Meetup/ecto-and-phoenix-doing-web-with-elixir-yurii-bodarev
Thank You! Any Questions?
zvi@nivertech.com
Slides, screenshots and code are based on:
● Phoenix & Elm, a real use case (parts 1-6)
○ by Ricardo García Vega, @bigardone
○ http://codeloveandboards.com/blog/2017/02/02/phoenix-and-elm-a-real-use-case-pt-1/
○ https://github.com/bigardone/phoenix-and-elm
○ Live demo: https://phoenix-and-elm.herokuapp.com/
● Migrating a Phoenix & Elm app from REST to GraphQL
○ by Paul Fioravanti, @paulfioravanti
○ https://paulfioravanti.com/blog/2018/03/06/migrating-a-phoenix-and-elm-app-from-rest-to-graphql/
○ https://github.com/paulfioravanti/phoenix-and-elm - branch graphql

Functional APIs with Absinthe GraphQL

  • 1.
    Functional APIs with AbsintheGraphQL Zvi Avraham zvi@nivertech.com
  • 2.
    $ whoami zvi ● DistributedSystems ○ Realtime Dataflow clusters 6PT/day ○ Scalable Messaging (3M sessions/node) ○ NoSQL Databases ● Crypto ○ Blockchain & Smart Contracts ○ Cryptoeconomics ○ Token Engineering
  • 3.
    Agenda ● briefly introduceGraphQL ● Introduce Elixir ● Introduce Ecto ● give overview of Absinthe toolkit ● show an example of implementing GraphQL API for Contract List app using Absinthe & Phoenix
  • 4.
    Assumptions ● Reading basicElixir code ○ Similar to Ruby ● Know at least one modern web framework ○ Rails, Django, etc. ● Basic intuitive understanding of GraphQL ○ JSON QBE (Query-by-Example)
  • 5.
    Why GraphQL? Let’s assumethe boss told us to use it ;)
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
    Front end Backend GraphQL schema Schema - a contract between Backend & Frontend
  • 16.
    Front end Backend GraphQL schema Schema - a contract between Backend & Frontend
  • 17.
    GraphQL Schema ● Schema!= Data Model ● It’s a Logical Data Model using graph paradigm ● Your Physical Data Model can be very different ○ Database (Relational, K/V, GraphDB, etc.) ○ Fetching data from external APIs ○ Computed on demand
  • 18.
    In Schema wedefine: ● Types ● Operations: ○ Queries ○ Mutations ○ Subscriptions
  • 19.
  • 20.
    Elixir ● Functional ● Concurrent ●Ruby-like syntax ● Compiled to Erlang bytecode ● Built on battle-proven BEAM VM
  • 21.
    Elixir ● Dynamically typed ○optional static type analyzer ● Pattern matching & guards ● Pipe operator “|>” ● Structs (like tagged maps) ● Hygienic macros ● No preprocessor like in Erlang ○ use attributes & macros instead
  • 25.
  • 26.
    Ecto is notan ORM, it’s a Data Access library https://boylesoftware.com/blog/orm-must-go/
  • 27.
    Ecto in anutshell ● Repo ○ the repository of data storage. The Repo hides implementation details from models. It can provide access to different data storages such as databases or in-memory storages. ● Migrations ○ CREATE/ALTER/DROP TABLE/INDEX ● Schema ○ a mapping between a DB table and its columns with the Elixir struct ● Changeset ○ cast attributes value into correct types, add validation rules & constraints. ● Query ○ Internal DSL wrapper in Elixir around composable SQL queries.
  • 32.
    Following slides arebased on: ● Phoenix & Elm, a real use case (parts 1-6) ○ by Ricardo García Vega, @bigardone ○ http://codeloveandboards.com/blog/2017/02/02/phoenix-and-elm-a-real-use-case-pt-1/ ○ https://github.com/bigardone/phoenix-and-elm ○ Live demo: https://phoenix-and-elm.herokuapp.com/ ● Migrating a Phoenix & Elm app from REST to GraphQL ○ by Paul Fioravanti, @paulfioravanti ○ https://paulfioravanti.com/blog/2018/03/06/migrating-a-phoenix-and-elm-app-from-rest-to-graphql/ ○ https://github.com/paulfioravanti/phoenix-and-elm - branch graphql
  • 34.
  • 41.
    Ecto adds :idfield for primary key field(:id, :integer) Timestamp macro expands into 2 fields: field(:inserted_at, :date) field(:updated_at, :date)
  • 45.
    The GraphQL toolkitfor Elixir https://absinthe-graphql.org/
  • 46.
    Source: Craft GraphQLAPIs in Elixir with Absinthe https://pragprog.com/book/wwgraphql/craft-graphql-apis-in-elixir-with-absinthe
  • 47.
    Source: Craft GraphQLAPIs in Elixir with Absinthe https://pragprog.com/book/wwgraphql/craft-graphql-apis-in-elixir-with-absinthe
  • 48.
    Phoenix with Absintheis simpler We don’t need usual Phoenix functionality: ● No controllers ● No views ● No templates We only need to: ● Define schemas ● Define custom types ● Write resolvers
  • 65.
    Ecto and Phoenix:Doing web with Elixir - Yurii Bodarev https://www.slideshare.net/Elixir-Meetup/ecto-and-phoenix-doing-web-with-elixir-yurii-bodarev
  • 75.
    Thank You! AnyQuestions? zvi@nivertech.com
  • 76.
    Slides, screenshots andcode are based on: ● Phoenix & Elm, a real use case (parts 1-6) ○ by Ricardo García Vega, @bigardone ○ http://codeloveandboards.com/blog/2017/02/02/phoenix-and-elm-a-real-use-case-pt-1/ ○ https://github.com/bigardone/phoenix-and-elm ○ Live demo: https://phoenix-and-elm.herokuapp.com/ ● Migrating a Phoenix & Elm app from REST to GraphQL ○ by Paul Fioravanti, @paulfioravanti ○ https://paulfioravanti.com/blog/2018/03/06/migrating-a-phoenix-and-elm-app-from-rest-to-graphql/ ○ https://github.com/paulfioravanti/phoenix-and-elm - branch graphql