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

90614ad13f6637ae5c2e1eafcbe51ab1?s=128

António Monteiro

September 13, 2018
Tweet

Transcript

  1. 4.
  2. 6.
  3. 7.

    Enter Reason — Syntax & toolchain for OCaml — (still

    OCaml) — github.com/facebook/reason — Goals: — newcomer friendly — extend OCaml's reach
  4. 8.
  5. 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 (!)
  6. 11.

    OCaml feature rundown ! — Pragmatic, strongly typed systems language

    — 30+ years of research & development — "80%" coverage / correctness — Multi-purpose, multi-target — Really Fast
  7. 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?
  8. 15.

    ! + GraphQL — reconciles GraphQL and OCaml type systems

    — successful compilation => spec compliant schema
  9. 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
  10. 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...
  11. 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 } } |}];
  12. 20.

    ! + GraphQL = " ? — What if we

    try to query a field that doesn't exist on the server? todos { title completed doesnt_exist }
  13. 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 ¯\_(ϑ)_/¯
  14. 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