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 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
)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.