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

More Decks by António Monteiro

Other Decks in Programming


  1. Enter Reason — Syntax & toolchain for OCaml — (still

    OCaml) — github.com/facebook/reason — Goals: — newcomer friendly — extend OCaml's reach
  2. What do I like about Clojure? — Easily build fullstack

    apps that share code — .cljc helps so much here — EDN, Transit roundtripping — the Datomic experience (!)
  3. OCaml feature rundown ! — Pragmatic, strongly typed systems language

    — 30+ years of research & development — "80%" coverage / correctness — Multi-purpose, multi-target — Really Fast
  4. 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?
  5. ! + GraphQL — reconciles GraphQL and OCaml type systems

    — successful compilation => spec compliant schema
  6. 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
  7. 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...
  8. 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 } } |}];
  9. ! + GraphQL = " ? — What if we

    try to query a field that doesn't exist on the server? todos { title completed doesnt_exist }
  10. Parting Thoughts Clojure OCaml / Reason .cljc Compile to JS

    or Native, shared modules Immutability ✅ EDN / Transit roundtripping GraphQL auto-serialization / custom scalars Datomic Experience ¯\_(ϑ)_/¯
  11. 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