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

OCaml, REborn: fullstack applications with ReasonML

OCaml, REborn: fullstack applications with ReasonML

OCaml is a systems language that’s been around for more than 20 years. Reason is a new syntax and toolchain that aims to make OCaml more accessible to everyone.

This talk aims to present Reason / OCaml as a viable alternative to building modern, fullstack, data-driven web applications that share the types of their business objects between the backend and the frontend (much like Clojure programmers are used to with CLJC).

I will also demonstrate how GraphQL, a data-driven query language for APIs, plays nicely with OCaml’s type system to achieve fully type-checked queries across the board, for instance disallowing clients to compile if they’re requesting a piece of data that the backend cannot provide.

From Small FP Conf 2018: https://clojutre.org/2018/#anmonteiro

António Monteiro

September 13, 2018
Tweet

More Decks by António Monteiro

Other Decks in Programming

Transcript

  1. OCaml, REborn
    Fullstack Applications with
    ReasonML
    Small FP Conf 2018
    @anmonteiro90

    View Slide

  2. youtube.com/watch?v=jH1oJiLV7_0

    View Slide

  3. Hello!
    !
    anmonteiro.com

    View Slide

  4. View Slide

  5. Back in 2016...

    View Slide

  6. View Slide

  7. Enter Reason
    — Syntax & toolchain for OCaml
    — (still OCaml)
    — github.com/facebook/reason
    — Goals:
    — newcomer friendly
    — extend OCaml's reach

    View Slide

  8. View Slide

  9. What do I like about Clojure?
    — Easily build fullstack apps
    that share code
    — .cljc helps so much here
    — EDN, Transit roundtripping
    — the Datomic experience (!)

    View Slide

  10. What does this integrated
    development experience look
    like in OCaml?

    View Slide

  11. OCaml feature rundown
    !
    — Pragmatic, strongly typed systems language
    — 30+ years of research & development
    — "80%" coverage / correctness
    — Multi-purpose, multi-target
    — Really Fast

    View Slide

  12. Several challenges
    — Types are awesome as long as I don't communicate
    with the external world
    — i.e. what happens at the boundary?
    — Type definitions that span across the stack
    — How do I share them?
    — How do I serialize them across the network?

    View Slide

  13. Let's take a detour...
    graphql.org

    View Slide

  14. The GraphQL type system
    GraphQL Type System

    View Slide

  15. !
    + GraphQL
    — reconciles GraphQL and OCaml type systems
    — successful compilation => spec compliant schema

    View Slide

  16. Example / Demo

    View Slide

  17. Example / Demo
    $ tree src
    src
    !"" client <--- Client specific code (uses Shared)
    # !"" App.re <--- Queries server GraphQL Schema
    # $"" Editor.re
    !"" server <--- Server specific code (uses Shared)
    # !"" main.re
    # !"" schema.re <--- GraphQL Schema
    # $"" server.re
    $"" shared
    $"" Shared.re <--- Where shared types live

    View Slide

  18. Defining / Using Data
    type todo = {
    title: string,
    completed: bool
    };
    — Server: uses todo type to build GraphQL Schema
    — Client: uses todo type to model UI
    — But network communication is untyped...

    View Slide

  19. Serializing data
    — OCaml-GraphQL-Server serializes to JSON
    — On the client, @bsRecord directive parses into a type in
    current scope
    module TodosQuery = [%graphql {|
    query getTodos {
    todos @bsRecord {
    title
    completed
    }
    }
    |}];

    View Slide

  20. !
    + GraphQL =
    "
    ?
    — What if we try to query a field that doesn't exist on
    the server?
    todos {
    title
    completed
    doesnt_exist
    }

    View Slide

  21. !
    + GraphQL =
    "

    View Slide

  22. Parting Thoughts
    Clojure OCaml / Reason
    .cljc Compile to JS or Native,
    shared modules
    Immutability ✅
    EDN / Transit
    roundtripping
    GraphQL auto-serialization
    / custom scalars
    Datomic Experience ¯\_(ϑ)_/¯

    View Slide

  23. Parting Thoughts
    — GraphQL not really required 1
    — What works for me might not work for you
    — Programs are a living organism
    [1]: github.com/mjambon/atd

    View Slide

  24. Thanks!
    !
    — reason-graphql-fullstack.now.sh
    — Playground at: reason-graphql-fullstack.now.sh/
    graphql
    — github.com/anmonteiro/reason-graphql-fullstack

    View Slide