Coder Social home page Coder Social logo

kutyel / servant-to-elm-example Goto Github PK

View Code? Open in Web Editor NEW

This project forked from haskell-to-elm/servant-to-elm-example

0.0 1.0 0.0 709 KB

Full-stack web app, built in a typesafe functional way, where servant-to-elm generates types, decoders/encoders, and fetching functions from Haskell types and Servant endpoint definition to Elm.๐Ÿค˜๐Ÿป

Haskell 48.70% Elm 38.99% HTML 2.25% CSS 10.07%

servant-to-elm-example's Introduction

servant-to-elm-example

servant-to-elm-example

This example is a full-stack web application, built in a typesafe functional way.

What's cool here is that servant-to-elm does the job of generating types and decoders/encoders from Haskell types and Servant definition to Elm, which not only catches regressions in the compile-time but also provides ready (and highly configurable) Elm functions to fetch necessary data from the server.

Prepare

  • Install Stack and Elm
  • Run code generation
    cd backend && stack run codegen
  • Install frontend dependencies (it's only elm-live)
    cd frontend && npm i

Set up editor (optional)

  • If you are using VSCode and you want to open both frontend and backend as a single workspace, use File > Open Workspace and choose servant-to-elm-example.code-workspace. In this scenario, VSCode will work correctly with both languages simultaneously. Also, the editor may recommend extensions, and installing them is a wise choice.

Run

  • Start the server on port 8080:
    cd backend && stack run server
  • Run frontend in dev mode (localhost:8000 opens in browser automatically):
    cd frontend && npm start

Domain model: Library

  • A book can have exactly 1 author
  • An author can have zero to many books
  • Book title must be unique per author
  • The author's name must be unique

This example does demonstrate:

  • How to fetch data and handle errors and "loading" state
  • How to post a new entity related to another, either new or existing one (submit a book with a new or existing author)

This example does not demonstrate:

  • User input validation parsing best practices
  • Client-side routing best practices
  • Debouncing user input
  • Client-side validation before submitting a form
  • Subscriptions via WebSockets (which could be implemented, but they are related more to Servant or Elm, rather than to code-generation)

Other choices

  • New entries that are in relation are submitted together and inserted transactionally.
  • Search is case-insensitive

Technical notes

  • This app was not tested on Windows systems yet. While is not a showcase of cross-platform compatibility, you can still open an issue or PR if something does not work as expected.

Adding new types

  • in backend/src/DomainModel.hs:
    • Define the type
    • Derive necessary instances, also specify Elm module name and Elm type (it's better to keep type name the same, but it's convenient to use one module for several coupled types - see Book, NewBook, and NewBookAuthor types and corresponding generated Elm module)
    • Add the type to typeDefinitions list - jsonDefinitions @YourNewType
  • Use your type in backend/src/Server.hs or wherever it's intended to
  • Run code-generation again

Design choices and alternatives

  • An obsolete generated Elm code can be removed before writing the new ones. Subdirectories that must be deleted are listed in backend/src/Codegen.hs. In this example, only the frontend/src/Api directory is removed before writing new files.
  • One of the alternatives to this approach is GraphQL, and there is a Haskell/Elm full-stack GraphQL example app. This example app was shamelessly inspired by that example.

servant-to-elm-example's People

Contributors

vladimirlogachev avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.