• Stars
    star
    778
  • Rank 58,431 (Top 2 %)
  • Language
    Elm
  • License
    BSD 3-Clause "New...
  • Created about 7 years ago
  • Updated 6 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Autogenerate type-safe GraphQL queries in Elm.

dillonkearns/elm-graphql

Build Status Elm package npm

elm-graphql logo

Why use this package over the other available Elm GraphQL packages? This is the only one that generates type-safe code for your entire schema. Check out this blog post, Type-Safe & Composable GraphQL in Elm, to learn more about the motivation for this library. (It's also the only type-safe library with Elm 0.18 or 0.19 support, see this discourse thread).

I built this package because I wanted to have something that:

  1. Gives you type-safe GraphQL queries (if it compiles, it's valid according to the schema),
  2. Creates decoders for you in a seamless and failsafe way, and
  3. Eliminates GraphQL features in favor of Elm language constructs where possible for a simpler UX (for example, GraphQL variables & fragments should just be Elm functions, constants, lets).

See an example in action on Ellie. See more end-to-end example code in the examples/ folder.

Overview

dillonkearns/elm-graphql is an Elm package and accompanying command-line code generator that creates type-safe Elm code for your GraphQL endpoint. You don't write any decoders for your API with dillonkearns/elm-graphql, instead you simply select which fields you would like, similar to a standard GraphQL query but in Elm. For example, this GraphQL query

query {
  human(id: "1001") {
    name
    homePlanet
  }
}

would look like this in dillonkearns/elm-graphql (the code in this example that is prefixed with StarWars is auto-generated)

import Graphql.Operation exposing (RootQuery)
import Graphql.SelectionSet as SelectionSet exposing (SelectionSet)
import StarWars.Object
import StarWars.Object.Human as Human
import StarWars.Query as Query
import StarWars.Scalar exposing (Id(..))


query : SelectionSet (Maybe HumanData) RootQuery
query =
    Query.human { id = Id "1001" } humanSelection


type alias HumanData =
    { name : String
    , homePlanet : Maybe String
    }


humanSelection : SelectionSet HumanData StarWars.Object.Human
humanSelection =
    SelectionSet.map2 HumanData
        Human.name
        Human.homePlanet

GraphQL and Elm are a perfect match because GraphQL is used to enforce the types that your API takes as inputs and outputs, much like Elm's type system does within Elm. elm-graphql simply bridges this gap by making your Elm code aware of your GraphQL server's schema. If you are new to GraphQL, graphql.org/learn/ is an excellent way to learn the basics.

After following the installation instructions to install the @dillonkearns/elm-graphql NPM package and the proper Elm packages (see the Setup section for details). Once you've installed everything, running the elm-graphql code generation tool is as simple as this:

npx @dillonkearns/elm-graphql https://elm-graphql.onrender.com --base StarWars --output examples/src

If headers are required, such as a Bearer Token, the --header flag can be supplied.

npx @dillonkearns/elm-graphql https://elm-graphql.onrender.com --base StarWars --output examples/src --header 'headerKey: header value'

Learning Resources

  • There is a thorough tutorial in the SelectionSet docs. SelectionSets are the core concept in this library, so I recommend reading through the whole page (it's not very long!).

  • The examples/ folder is another great place to start.

  • If you want to learn more GraphQL basics, this is a great tutorial, and a short read: graphql.org/learn/

  • My Elm Conf 2018 talk goes into the philosophy behind dillonkearns/elm-graphql

Types Without Borders Elm Conf Talk

(Skip to 13:06 to go straight to the dillonkearns/elm-graphql demo).

  • My 10-minute video tutorial on how to leverage Custom Scalars in elm-graphql using the Scalar Codecs feature. Scalar Codecs Tutorial

If you're wondering why code is generated a certain way, you're likely to find an answer in the Frequently Asked Questions (FAQ).

There's a very helpful group of people in the #graphql channel in the Elm Slack. Don't hesitate to ask any questions about getting started, best practices, or just general GraphQL in there!

Setup

dillonkearns/elm-graphql generates Elm code that allows you to build up type-safe GraphQL requests. Here are the steps to setup dillonkearns/elm-graphql.

  1. Add the dillonkearns/elm-graphql elm package as a dependency in your elm.json. You will also need to make sure that elm/json is a dependency of your project since the generated code has lots of JSON decoders in it.

    elm install dillonkearns/elm-graphql
    elm install elm/json
  2. Install the @dillonkearns/elm-graphql command line tool through npm. This is what you will use to generate Elm code for your API. It is recommended that you save the @dillonkearns/elm-graphql command line tool as a dev dependency so that everyone on your project is using the same version.

    npm install --save-dev @dillonkearns/elm-graphql
    # you can now run it locally using `npx elm-graphql`,
    # or by calling it through an npm script as in this project's package.json
  3. Run the @dillonkearns/elm-graphql command line tool installed above to generate your code. If you used the --save-dev method above, you can simply create a script in your package.json like the following:

    {
      "name": "star-wars-elm-graphql-project",
      "version": "1.0.0",
      "scripts": {
        "api": "elm-graphql https://elm-graphql.onrender.com/api --base StarWars"
      }
    
  4. With the above in your package.json, running npm run api will generate dillonkearns/elm-graphql code for you to call in ./src/StarWars/. You can now use the generated code as in this Ellie example or in the examples folder.

Subscriptions Support

You can do real-time APIs using GraphQL Subscriptions and dillonkearns/elm-graphql. Just wire in the framework-specific JavaScript code for opening the WebSocket connection through a port. Here's a live demo and its source code. The demo server is running Elixir/Absinthe.

Contributors

Thank you Mario Martinez (martimatix) for all your feedback, the elm-format PR, and for the incredible logo design!

Thank you Mike Stock (mikeastock) for setting up Travis CI!

Thanks for the reserved words pull request @madsflensted!

A huge thanks to @xtian for doing the vast majority of the 0.19 upgrade work! 🎉

Thank you Josh Adams (@knewter) for the code example for Subscriptions with Elixir/Absinthe wired up through Elm ports!

Thank you Romario for adding OptionalArgument.map!

Thank you Aaron White for your pull request to improve the performance and stability of the elm-format step! 🎉

Roadmap

All core features are supported. That is, you can build any query or mutation with your dillonkearns/elm-graphql-generated code, and it is guaranteed to be valid according to your server's schema.

dillonkearns/elm-graphql will generate code for you to generate subscriptions and decode the responses, but it doesn't deal with the low-level details for how to send them over web sockets. To do that, you will need to use custom code or a package that knows how to communicate over websockets (or whichever protocol) to setup a subscription with your particular framework. See this discussion for why those details are not handled by this library directly.

I would love to hear feedback if you are using GraphQL Subscriptions. In particular, I'd love to see live code examples to drive any improvements to the Subscriptions design. Please ping me on Slack, drop a message in the #graphql channel, or open up a Github issue to discuss!

I would like to investigate generating helpers to make pagination simpler for Connections (based on the Relay Cursor Connections Specification). If you have ideas on this chime in on this thread.

See the full roadmap on Trello.

More Repositories

1

elm-pages

Hybrid Elm framework with full-stack and static routes.
Elm
655
star
2

mobster

Pair and mob programming timer for Mac, Windows, and Linux.
Elm
308
star
3

elm-typescript-interop

Generate TypeScript declaration files for your elm ports!
Elm
165
star
4

elm-markdown

Extensible markdown parser with custom rendering, in pure Elm.
Elm
106
star
5

elm-review-html-to-elm

Turn HTML into Elm. With support for elm-tailwind-modules.
Elm
96
star
6

elm-pages-starter

Starter blog for elm-pages
Elm
95
star
7

idiomatic-elm-package-guide

Everything you need to know to publish a useful, idiomatic Elm package!
67
star
8

elm-cli-options-parser

Build type-safe command-line utilities in Elm!
Elm
54
star
9

elm-ts-json

Elm
42
star
10

elm-typescript-starter

Boilerplate for Elm web apps with safe TypeScript interop and hot module replacement.
JavaScript
35
star
11

elm-electron-starter

Build native cross-platform desktop apps in Elm
TypeScript
35
star
12

talks

Elm
23
star
13

elm-pages-v3-beta

Elm
22
star
14

elm-electron

Type-safe interprocess communication for Electron apps built with Elm.
Elm
19
star
15

elm-publish-action

TypeScript
19
star
16

elm-form

Standalone version of the elm-pages Form API.
Elm
19
star
17

elm-package-starter

Starter template for an Elm package.
Elm
13
star
18

graphqelm-demo

Demo package to support Ellie examples of Graphqelm.
Elixir
11
star
19

elm-pages-3-alpha-starter

Elm
9
star
20

elm-decoder-koans

Learn about elm decoders by filling in the blanks in test cases.
Elm
8
star
21

elm-radio.com

Elm
7
star
22

elm-pages-tailwind-starter

Elm
7
star
23

elm-ts-interop-starter

Elm
7
star
24

elm-rss

Generate RSS feeds with Elm.
Elm
7
star
25

elm-pages-realworld

Realworld implementation with elm-pages v3.
Elm
6
star
26

elm-ical

Elm
5
star
27

gitbook-elm-graphql

5
star
28

incrementalelm.com

Elm
4
star
29

elm-katas

Placeholder for elm exercises
Elm
4
star
30

graphqelm

This package has been moved to dillonkearns/elm-graphql
Elm
4
star
31

sb-farmar

Elm
4
star
32

atom-vim-mode-plus-exchange

Exchange two text areas in vim-mode-plus
JavaScript
4
star
33

automated-testing-wiki

Automated Testing Wiki - A Community Guide to Effective TDD
3
star
34

legit

A collection of scripts for common git tasks to simplify and improve workflow.
Ruby
3
star
35

elm-view-transitions

A proof of concept of the View Transitions API in Elm.
Elm
3
star
36

elm-snapshot-test

JavaScript
3
star
37

prisma-example

Elm
3
star
38

elm-program-test-katas

Elm
2
star
39

fishbowl

Elm
2
star
40

ellie-app-cli

JavaScript
2
star
41

elm-pages-init

Elm
2
star
42

elm-form-mdl

elm-mdl helpers for the elm-forms library
Elm
2
star
43

Test-Runner

Python
2
star
44

the_lean_cafe

Elixir
2
star
45

chess-vision

Elm
1
star
46

elm-koan-runner

Elm
1
star
47

elm-pages-blog-tutorial

Elm
1
star
48

elm-oembed

Elm
1
star
49

elm-ts-netlify-starter

JavaScript
1
star
50

advent-posts

1
star
51

elm-sitemap

Generate sitemaps in elm.
Elm
1
star
52

elm-program-test-experiment

Elm
1
star
53

dotfile-linker

Ruby
1
star
54

we-connect

Elm
1
star
55

elm-ts-interop.com

Elm
1
star
56

elm-pages-netlify-cms-starter

Starter kit for elm-pages and Netlify CMS.
Elm
1
star