This is an alternative site for discovering Elm packages. You may be looking for the official Elm package site instead.

GraphQL.Request.Builder.Arg

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.

type Value vars = Value AST.ArgumentValue (List (Variable.Variable vars))

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.

variable : Variable.Variable vars -> Value vars

Construct a variable argument value.

int : Int -> Value vars

Construct a constant GraphQL Int argument value from an Elm Int.

float : Float -> Value vars

Construct a constant GraphQL Float argument value from an Elm Float.

string : String -> Value vars

Construct a constant GraphQL String argument value from an Elm String.

bool : Bool -> Value vars

Construct a constant GraphQL Boolean argument value from an Elm Bool.

true : Value vars

The GraphQL true value.

false : Value vars

The GraphQL false value.

datetime : Date -> Value vars

Construct a constant GraphQL DateTime argument value from an Elm Date.

date : Date -> Value vars

Construct a constant GraphQL DateTime argument value from an Elm Date. Serialized value does not contains time data.

null : Value vars

The GraphQL null value.

enum : String -> Value vars

Construct a GraphQL Enum value from a String.

object : List ( String, Value vars ) -> Value vars

Constructs a GraphQL Input Object value from a list of key-value pairs.

list : List (Value vars) -> Value vars

Constructs a GraphQL List from an Elm List of Values.

getAST : Value vars -> AST.ArgumentValue

Returns the AST (abstract syntax tree) representation of a Value.

getVariables : Value vars -> List (Variable.Variable vars)

Returns a List of any Variables 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