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:
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).
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") {
namehomePlanet
}
}
would look like this
in dillonkearns/elm-graphql (the code in this example that is prefixed with StarWars is auto-generated)
importGraphql.Operationexposing (RootQuery)
importGraphql.SelectionSetasSelectionSetexposing (SelectionSet)
importStarWars.ObjectimportStarWars.Object.HumanasHumanimportStarWars.QueryasQueryimportStarWars.Scalarexposing (Id(..))
query:SelectionSet (MaybeHumanData) RootQueryquery =Query.human { id =Id"1001"} humanSelection
type alias HumanData={ name :String, homePlanet :MaybeString}humanSelection:SelectionSetHumanDataStarWars.Object.HumanhumanSelection =SelectionSet.map2 HumanDataHuman.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:
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
(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.
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.
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
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
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:
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.
请发表评论