dillonkearns/elm-graphql/1.3.0

NOTE: the latest version is 5.0.12

Graphql.Field

Fields are automatically generated by the @dillonkearns/elm-graphql CLI command. You can use Graphql.Field.map to transform a value.

Safe Transformations

map :
(decodesTo -> mapsTo)
-> Field decodesTo typeLock
-> Field mapsTo typeLock

Maps the data coming back from the GraphQL endpoint. In this example, User.name is a function that the @dillonkearns/elm-graphql CLI tool created which tells us that the name field on a User object is a String according to your GraphQL schema.

import Api.Object
import Api.Object.User as User
import Graphql.Field as Field
import Graphql.SelectionSet exposing (SelectionSet, with)


human : SelectionSet String Api.Object.User
human =
    User.selection identity
        |> with
            (User.name
                |> Field.map String.toUpper
            )

You can also map to values of a different type (String -> Int, for example), see examples/StarWars.elm for more advanced example.

Result (...OrFail) Transformations

Warning When you use these functions, you lose the guarantee that the server response will decode successfully.

These helpers, though convenient, will cause your entire decoder to fail if it ever maps to an Err instead of an Ok Result.

(decodesTo -> Result String mapsTo)
-> Field decodesTo typeLock
-> Field mapsTo typeLock

If the map function provided returns an Ok Result, it will map to that value. If it returns an Err, the entire response will fail to decode.

import Date exposing (Date)
import Github.Object
import Github.Object.Repository
import Github.Scalar
import Graphql.Field as Field exposing (Field)


createdAt : Field Date Github.Object.Repository
createdAt =
    Github.Object.Repository.createdAt
        |> Field.mapOrFail
            (\(Github.Scalar.DateTime dateTime) ->
                Date.fromString dateTime
            )
nonNullOrFail : Field (Maybe decodesTo) typeLock -> Field decodesTo typeLock

Effectively turns an attribute that is String => String!, or User => User! (if you're not familiar with the GraphQL type language notation, learn more here).

This will cause your entire decoder to fail if the field comes back as null. It's far better to fix your schema then to use this escape hatch!

Field (List (Maybe decodesTo)) typeLock
-> Field (List decodesTo) typeLock

Effectively turns a field that is [String] => [String!], or [User] => [User!] (if you're not familiar with the GraphQL type language notation, learn more here).

This will cause your entire decoder to fail if any elements in the list for this field comes back as null. It's far better to fix your schema then to use this escape hatch!

Often GraphQL schemas will contain things like [String] (i.e. a nullable list of nullable strings) when they really mean [String!]! (a non-nullable list of non-nullable strings). You can chain together these nullable helpers if for some reason you can't go in and fix this in the schema, for example:

releases : SelectionSet (List Release) Github.Object.ReleaseConnection
releases =
    Github.Object.ReleaseConnection.selection identity
        |> with
            (Github.Object.ReleaseConnection.nodes release
                |> Field.nonNullOrFail
                |> Field.nonNullElementsOrFail
            )

Without the Field.nonNull... transformations here, the type would be SelectionSet (Maybe (List (Maybe Release))) Github.Object.ReleaseConnection.

Types

type Field decodesTo typeLock
= Field RawField (Decoder decodesTo)