Fields are automatically generated by the @dillonkearns/elm-graphql CLI command.
You can use Graphql.Field.map to transform a value.
...OrFail) TransformationsWarning 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.
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 Time exposing (Posix)
import Github.Object
import Github.Object.Repository
import Github.Scalar
-- NOTE: Iso8601 comes from an external dependency in Elm >= 0.19:
-- https://package.elm-lang.org/packages/rtfeldman/elm-iso8601-date-strings/latest/
import Iso8601
import Graphql.Field as Field exposing (Field)
type alias Timestamps =
{ created : Posix
, updated : Posix
}
timestampsSelection : SelectionSet Timestamps Github.Object.Repository
timestampsSelection =
Repository.selection Timestamps
|> with (Repository.createdAt |> mapToDateTime)
|> with (Repository.updatedAt |> mapToDateTime)
mapToDateTime : Field Github.Scalar.DateTime typeLock -> Field Posix typeLock
mapToDateTime =
Field.mapOrFail
(\(Github.Scalar.DateTime value) ->
Iso8601.toTime value
|> Result.mapError (\_ -> "Failed to parse " ++ value ++ " as Iso8601 DateTime.")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!
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.
Maps the data coming back from the GraphQL endpoint. In this example,
User.nameis a function that the@dillonkearns/elm-graphqlCLI tool created which tells us that thenamefield on aUserobject is a String according to your GraphQL schema.You can also map to values of a different type (
String -> Int, for example), seeexamples/StarWars.elmfor more advanced example.