The functions in this module let you construct argument values that you can pass to fields and directives using the functions in GraphQL.Request.Builder
.
An argument value, which might be either a constant or a variable. The vars
parameter is the type of Elm value that variables will extract their values from.
Construct a variable argument value.
Construct a constant GraphQL Int
argument value from an Elm Int
.
Construct a constant GraphQL Float
argument value from an Elm Float
.
Construct a constant GraphQL String
argument value from an Elm String
.
Construct a constant GraphQL Boolean
argument value from an Elm Bool
.
The GraphQL true
value.
The GraphQL false
value.
Construct a constant GraphQL DateTime
argument value from an Elm Date
.
Construct a constant GraphQL DateTime
argument value from an Elm Date
. Serialized value does not contains time data.
The GraphQL null
value.
Construct a GraphQL Enum value from a String
.
Constructs a GraphQL Input Object value from a list of key-value pairs.
Constructs a GraphQL List from an Elm List
of Value
s.
Returns the AST (abstract syntax tree) representation of a Value
.
Returns a List
of any Variable
s used in the given Value
.
module GraphQL.Request.Builder.Arg
exposing
( Value
, variable
, int
, float
, string
, bool
, true
, false
, datetime
, date
, null
, enum
, object
, list
, getAST
, getVariables
)
{-| The functions in this module let you construct argument values that you can pass to fields and directives using the functions in [`GraphQL.Request.Builder`](GraphQL-Request-Builder).
@docs Value, variable, int, float, string, bool, true, false, datetime, date, null, enum, object, list, getAST, getVariables
-}
import Date exposing (Date)
import GraphQL.Request.Document.AST as AST
import GraphQL.Request.Builder.Variable as Variable
import GraphQL.Request.Builder.Variable.Util as VarUtil
{-| An argument value, which might be either a constant or a variable. The `vars` parameter is the type of Elm value that variables will extract their values from.
-}
type Value vars
= Value AST.ArgumentValue (List (Variable.Variable vars))
{-| Construct a variable argument value.
-}
variable : Variable.Variable vars -> Value vars
variable var =
Value (AST.VariableValue () (Variable.name var)) [ var ]
{-| Construct a constant GraphQL `Int` argument value from an Elm `Int`.
-}
int : Int -> Value vars
int x =
Value (AST.IntValue x) []
{-| Construct a constant GraphQL `Float` argument value from an Elm `Float`.
-}
float : Float -> Value vars
float x =
Value (AST.FloatValue x) []
{-| Construct a constant GraphQL `String` argument value from an Elm `String`.
-}
string : String -> Value vars
string x =
Value (AST.StringValue x) []
{-| Construct a constant GraphQL `Boolean` argument value from an Elm `Bool`.
-}
bool : Bool -> Value vars
bool x =
Value (AST.BooleanValue x) []
{-| The GraphQL `true` value.
-}
true : Value vars
true =
bool True
{-| The GraphQL `false` value.
-}
false : Value vars
false =
bool False
{-| Construct a constant GraphQL `DateTime` argument value from an Elm `Date`.
-}
datetime : Date -> Value vars
datetime dt =
Value (AST.DateTimeValue dt) []
{-| Construct a constant GraphQL `DateTime` argument value from an Elm `Date`. Serialized value does not contains time data.
-}
date : Date -> Value vars
date d =
Value (AST.DateValue d) []
{-| The GraphQL `null` value.
-}
null : Value vars
null =
Value AST.NullValue []
{-| Construct a GraphQL Enum value from a `String`.
-}
enum : String -> Value vars
enum symbol =
Value (AST.EnumValue symbol) []
valueVariablesFoldStep : Value vars -> List (Variable.Variable vars) -> List (Variable.Variable vars)
valueVariablesFoldStep =
getVariables >> VarUtil.mergeVariables
{-| Constructs a GraphQL Input Object value from a list of key-value pairs.
-}
object : List ( String, Value vars ) -> Value vars
object pairs =
Value
(AST.ObjectValue (pairs |> List.map (\( k, Value ast _ ) -> ( k, ast ))))
(pairs
|> List.foldr (Tuple.second >> valueVariablesFoldStep) []
)
{-| Constructs a GraphQL List from an Elm `List` of `Value`s.
-}
list : List (Value vars) -> Value vars
list values =
Value
(AST.ListValue (values |> List.map (\(Value ast _) -> ast)))
(values
|> List.foldr valueVariablesFoldStep []
)
{-| Returns the AST (abstract syntax tree) representation of a `Value`.
-}
getAST : Value vars -> AST.ArgumentValue
getAST (Value ast _) =
ast
{-| Returns a `List` of any `Variable`s used in the given `Value`.
-}
getVariables : Value vars -> List (Variable.Variable vars)
getVariables (Value _ vars) =
vars