Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Elixir Brasil - Absinthe - Graphql + Phoenix + Elixir

Elixir Brasil - Absinthe - Graphql + Phoenix + Elixir

Ana Luiza Portello

February 03, 2018
Tweet

More Decks by Ana Luiza Portello

Other Decks in Programming

Transcript

  1. HTTP GET Songs Followers Ana Luiza Lorem Ipsum users/123/posts RESPONSE

    posts { { id: .. music: {..}, replies: {..}, likes: {..}, shares: {..}, … }, ... } Jovelli How I Feel
  2. HTTP GET Songs Followers Ana Luiza Lorem Ipsum users/123 RESPONSE

    a { name: .., accountName: .., nickname: Ana Luiza, email: {..}, bio: Lorem Ipsum avatar: {..}, birthday: .., city: .., location: {..} ... }
  3. HTTP GET users/123/followers RESPONSE followers { { name: .., accountName:

    .., Nickname: Vilmar, email: {..}, avantar: .. … }, ... } Songs 1.239 Jovelli How I Feel Ana Luiza Lorem Ipsum Followers
  4. HTTP POST graphql/ RESPONSE data { userName: .., avatar: ..,

    bio, followers [ {avatar: ..}, .. ] posts [ {artist: …, name:} ] } BODY { userName avatar bio followers(first: 3) { avatar } posts { artist name }
  5. • Construir e enviar request • Receber e parcear resposta

    do servidor • Salvar dados • Mostrar dados na UI • Descreve os dados necessários pra UI • Mostra dados na UI REST
  6. • CORRETO • FACIL DE USAR • IDIOMATICO • CONCORRENTE

    • PERFORMATICO • ORIENTADO A COMUNIDADES
  7. pipeline :api do plug :accepts, ["json"] end scope "/" do

    pipe_through :api forward "/graphql", Absinthe.Plug, schema: AppWeb.Schema forward "/graphiql", Absinthe.Plug.GraphiQL, schema: AppWeb.Schema, interface: :simple, context: %{pubsub: MusicWeb.Endpoint} end
  8. CLIENT SERVER GRAPHQL DATA RESOLVE BODY JSON { query: {..}

    variables:{..} } { data: {..} errors:{..} }
  9. TYPES object :track do field :id, :id field :title, :string

    field :artist, :artist field :album, :album field :producer, :integer field :duration, :integer end object :artist do field :id, :id field :name, :string field :albums, list_of(:album) field :tracks, list_of(:tracks) end
  10. @desc """ The lowercase string scalar """ scalar :lowstring do

    serialize &to_string/1 parse &parse_lowercase/1 end
  11. mutation { user(userName: "name", password: “123”) { id publishedAt }

    } object :artist do field :id, :id field :name, :string field :username, :lowstring end
  12. schema "artists" do field :name, :string has_many :albums, Album has_many

    :tracks, Track timestamps end schema "albums" do field :title, :string field :release_date, Ecto.Date belongs_to :artist, Artist many_to_many :genres, Genre, join_through: "albums_genres" has_many :tracks, Track timestamps end
  13. schema "artists" do field :name, :string has_many :albums, Album has_many

    :tracks, Track timestamps end schema "albums" do field :title, :string field :release_date, Ecto.Date belongs_to :artist, Artist many_to_many :genres, Genre, join_through: "albums_genres" has_many :tracks, Track timestamps end
  14. RESPONSE [ { ‘name’: “ABBA”, }, { ‘name’: “Amy Winehouse”

    , }, { ‘name’: “Ana Carolina” , } ] CORPO { artists { name } }
  15. RESPONSE { name: “Charlie Brown Jr.” albums: { { title:

    “Preço Curto Prazo Longo”, release_year: 1999 tracks { { title: “União” }, .. } } } REQUEST { artists { name albums { title tracks { title } } } }
  16. RESPONSE { ‘title’: “I will survive” , ‘duration’: 3000, }

    CORPO { tracks(id: 123422){ title duration } }
  17. RESPONSE { name: “Carolina de Oliveira Lourenço”, Bio: “Cantora e

    Compositora Brasileira” age: 24 } REQUEST { artist(name: “MC Carol”) { name bio age } }
  18. RESPONSE { popMusics { { name: ‘Swish Swish’ }, {

    name: ‘PokerFace’ }, ... } } REQUEST { popMusics: musics(genre: Pop){ name } }
  19. RESPONSE { user { name: “Ana” } } REQUEST query

    getUserNameById( $userId: String) { user(id: $userId): { name } } VARIABLE { userId: ‘21312321’ }
  20. REQUEST query UserWithFotos( $withPhotos = Boolean) { user { name,

    age, photos @include(if: $withPhotos) } } VARIABLE { ‘withPhotos’: true }
  21. REQUEST query UserWithFotos( $noPhotos = Boolean) { user { name,

    age, photos @skip(if: $noPhotos) } } VARIABLE { ‘noPhotos’: false }
  22. @defer { user { posts { author { name }

    comments @defer { author { name } } message } } }
  23. @stream { user { posts @stream { author { name

    } comments @defer { author { name } } message } } }
  24. @live { user { posts @live { author { name

    } comments @defer { author { name } } message } } }
  25. RESPONSE { ‘createPost’ { ‘id’: “123” ‘content’: { ... }

    } } REQUEST mutation createPost($post: Post) { createPost(content: $post){ id content } } VARIABLE { content { ... } }
  26. RESPONSE { ‘createPost’ { ‘content’: { ... } } }

    REQUEST subscription createPost($post: Post) { createPost(content: $post){ content } } VARIABLE { content { ... } }
  27. USER POST POST POST USER USER POST POST POST O

    único limite p/ percorrer um grafo são os próprios dados
  28. user { _id name post { userId content liked {

    _id name post { liked { name } } } }
  29. {:dataloader, "~> 1.0.0"} object :author do @desc "Author of the

    post" field :posts, list_of(:post), resolve: dataloader(Blog) end
  30. • DIFERENTES CLIENTES: Há a necessidade de criar várias versões

    de dados diferentes para múltiplos clientes. • PREPARAR DADOS: Há muito esforço do server preparar os dados e do cliente para parsea-los. • LINKAR DADOS: Quando os dados mudam com muita frequência e precisam estar linkados sempre.