ghivert/elm-graphql/2.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 Root
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 -> (Result Http.Error a -> msg) -> Decoder a -> Cmd msg
sendRequest id msg decoder =
  GraphQl.query "/example_endpoint" userRequest decoder
    |> GraphQl.addVariables [ ("id", Encode.int id) ]
    |> GraphQl.send msg

Value

type Value a

Handle GraphQL values.

type Root
type Field

Constructors

object : List (Value Field) -> Value Root

Generate a Value, from a list of fields.

object
  [ field "user" ]

Turns into:

query {
  user
}
named : String -> List (Value Field) -> Value Root

Generate a Value with a name.

named "MySuperRequest"
  [ field "user" ]

Turns into:

query MySuperRequest {
  user
}
field : String -> Value Field

Generate a field.

Modifiers

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

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 Root -> Value Root

Add a variable to a Field.

named "UserRequest"
  [ field "user" ]
  |> withVariable "id" "id"

Turns into:

query UserRequest(id: $id) {
  user
}
withAlias : String -> Value Field -> Value Field

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 Field) -> Value Field -> Value Field

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