Get updates via , Github, or RSS | About

Episode 22: GraphQL Jan 26, 2017

GraphQL is exploding in popularity for Javascript applications, but it works equally well in the type-safe world of Elm. The elm-graphql node binary will introspect a GraphQL endpoint, read your .graphql file, and generate a tidy Elm module you can use anywhere in your application.

elm-graphql will recommend you install jahewson/elm-graphql-module, but at this time it is not updated for Elm 0.18. Instead, you should install base-dev/elm-graphql-module until the official version is updated.

Examples

StarWars.graphql

query queryFriends($id: String!) {
  human(id: $id) {
    name
    home_planet
    appears_in
    friends {
      id
      name
    }
  }
}

StarWars.elm

queryFriends :
    { id : String
    }
    -> Task Http.Error QueryFriends
queryFriends params =
    let
        graphQLQuery =
            """query queryFriends($id: String!) { human(id: $id) { name home_planet appears_in friends { id name } } }"""
    in
        let
            graphQLParams =
                Json.Encode.object
                    [ ( "id", Json.Encode.string params.id )
                    ]
        in
            GraphQL.query "GET" endpointUrl graphQLQuery "queryFriends" graphQLParams queryFriendsDecoder