I built this package because I wanted to have something that:
- Gives you type-safe GraphQL queries (if it compiles, it's valid according to the schema),
- Creates decoders for you in a seamless and failsafe way, and
- 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.
graphqelm generates Elm code that allows you to build up type-safe GraphQL requests. Here are the steps to setup graphqelm.
-
Add the
Graphqelmelm package as a dependency in yourelm-package.json.elm package install dillonkearns/graphqelm
-
Install the
graphqelmcommand line tool through npm. This is what you will use to generate Elm code for your API. It is recommended that you save thegraphqelmcommand line tool as a dev dependency so that everyone on your project is using the same version.npm install --save-dev graphqelm # you can now run it locally with the ./node_modules/.bin/graphqelm binary, # or by calling it through an npm script as in this project's package.json
-
Run the
graphqelmcommand line tool installed above to generate your code. If you used the--save-devmethod above, you can simply create a script in your package.json like the following:{ "name": "star-wars-graphqelm-project", "version": "1.0.0", "scripts": { "api": "graphqelm https://graphqelm.herokuapp.com/api --base StarWars" }, ... -
With the above in your
package.json, runningnpm run apiwill generate Graphqelm code for you to call in./src/StarWars/. You can now use the generated code as in this Ellie example or in theexamplesfolder.
By default, deprecated Fields and Enums are not included in the generated
GraphqElm code. Use the --includeDeprecated flag when running the graphqelm
commandline tool if you would like to include them.
Thank you Mario Martinez (martimatix) for all your feedback and for the elm-format PR!
Thank you Mike Stock (mikeastock) for setting up Travis CI!
Type-safe Input Objects are planned but not yet supported. Otherwise all core functionality has been implemented. I would also like to investigate supporting subscriptions.