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. OCaml, REborn Fullstack Applications with ReasonML Small FP Conf 2018

    @anmonteiro90
  2. youtube.com/watch?v=jH1oJiLV7_0

  3. Hello! ! anmonteiro.com

  4. None
  5. Back in 2016...

  6. None
  7. Enter Reason — Syntax & toolchain for OCaml — (still

    OCaml) — github.com/facebook/reason — Goals: — newcomer friendly — extend OCaml's reach
  8. None
  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 (!)
  10. What does this integrated development experience look like in OCaml?

  11. OCaml feature rundown ! — Pragmatic, strongly typed systems language

    — 30+ years of research & development — "80%" coverage / correctness — Multi-purpose, multi-target — Really Fast
  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?
  13. Let's take a detour... graphql.org

  14. The GraphQL type system GraphQL Type System

  15. ! + GraphQL — reconciles GraphQL and OCaml type systems

    — successful compilation => spec compliant schema
  16. Example / Demo

  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
  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...
  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 } } |}];
  20. ! + GraphQL = " ? — What if we

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

  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 ¯\_(ϑ)_/¯
  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
  24. Thanks! ! — reason-graphql-fullstack.now.sh — Playground at: reason-graphql-fullstack.now.sh/ graphql —

    github.com/anmonteiro/reason-graphql-fullstack