ghivert/elm-graphql/1.0.1

NOTE: this package is not compatible with Elm 0.19.1

GraphQl

GraphQL queries made easy in Elm! This package provides easier way to deal with GraphQL queries. This package aims to partially mimic the Json Encoders and the HTTP API. Every user of Elm should not be lost using this package.

import GraphQl

-- Define the request.
userRequest : GraphQl.Value
userRequest =
  GraphQl.object
    [ GraphQl.field "user"
      |> GraphQl.withArgument "id" (GraphQl.variable "id")
      |> GraphQl.withSelectors
        [ GraphQl.field "id"
        , GraphQl.field "name"
          |> GraphQl.withSelectors
            [ GraphQl.field "first_name"
            , GraphQl.field "last_name"
            ]
        ]
    , GraphQl.field "address"
      |> GraphQl.withArgument "city" (GraphQl.string "Paris")
      |> GraphQl.withArgument "id" (GraphQl.int 12)
      |> GraphQl.withArgument "type" (GraphQl.type_ "LOFT")
      |> GraphQl.withSelectors
        [ GraphQl.field "street"
        , GraphQl.field "neighborhood"
        ]
    ]
    |> GraphQl.withVariable "id" "INT!"

-- And Send It!
sendRequest : Int -> Decoder a -> Cmd msg
sendRequest id decoder =
  GraphQl.query "/example_endpoint" userRequest decoder
    |> GraphQl.addVariables [ ("id", Encode.int id) ]
    |> GraphQl.send

Value

type Value

Handle GraphQL values.

Constructors

object : List Value -> Value

Generate a Value, from a list of fields.

object
  [ field "user" ]

Turns into:

query {
  user
}
named : String -> List Value -> Value

Generate a Value with a name.

named "MySuperRequest"
  [ field "user" ]

Turns into:

query MySuperRequest {
  user
}
field : String -> Value

Generate a field.

Modifiers

withArgument : String -> Argument -> Value -> Value

Add an argument to a Field.

field "user"
  |> withArgument "id" (GraphQl.string "12")
  |> withSelectors
    [ field "id"
    , field "first_name"
    , field "last_name"
    ]

Turns into:

user(id: "12") {
  id
  first_name
  last_name
}
withVariable : String -> String -> Value -> Value

Add a variable to a Field.

field "user"
  |> withVariable "id" "id"

Turns into:

user(id: $id)
withAlias : String -> Value -> Value

Add an alias to a Field.

field "user"
  |> withAlias "currentUser"
  |> withSelectors
    [ field "id"
    , field "first_name"
    , field "last_name"
    ]

Turns into:

currentUser: user {
  id
  first_name
  last_name
}
withSelectors : List Value -> Value -> Value

Add selectors to a Field.

field "user"
  |> withSelectors
    [ field "id"
    , field "first_name"
    , field "last_name"
    ]

Turns into:

user {
  id
  first_name
  last_name
}

Arguments

Handle arguments on GraphQL fields.

int : Int -> Argument

Generate an argument, to use with withArgument.

fied "user"
  |> withArgument "id" (GraphQl.int 12)

Turns into:

user(id: 12)
string : String -> Argument

Generate an argument, to use with withArgument.

fied "user"
  |> withArgument "id" (GraphQl.string "12")

Turns into:

user(id: "12")
type_ : String -> Argument

Generate an argument, to use with withArgument. Generate a type in GraphQL.

fied "user"
  |> withArgument "id" (GraphQl.type_ "INT")

Turns into:

user(id: INT)
variable : String -> Argument

Generate an argument, to use with withArgument. You don't have to handle the $ sign.

fied "user"
  |> withArgument "id" (GraphQl.variable "id")

Turns into:

user(id: $id)

Requests

type Request a

Requests contains the query and the variables of each GraphQl requests.

query : String -> Value -> Decoder a -> Request a

Entry of every GraphQL values to turn them into requests, which can be launched!

object []
  |> flip (query "https://example.com") decoder
  |> send msg
addVariables : List ( String, Value ) -> Request a -> Request a

Add variables to a requests. Useful when defining variables in your GraphQL request.

object []
  |> flip (query "https://example.com") decoder
  |> addVariables []
  |> send msg
send : (Result Error a -> msg) -> Request a -> Cmd msg

Actually send the GraphQL request! Generates a Cmd, to give to Elm in your update.