ghivert/elm-graphql/3.3.0

NOTE: this package is not compatible with Elm 0.19.1

GraphQl

GraphQL queries and mutations made easy in Elm! This package provides easier way to deal with GraphQL queries and mutations. 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 exposing (Mutation, Query, Variables, Named, Operation)
import Http
import Json.Decode as Decode exposing (Decoder)
import Json.Encode as Encode

-- Define the request.
userRequest : Operation Query Variables
userRequest =
  GraphQl.named "MySuperQuery"
    [ 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.withArgument "id" (GraphQl.type_ "INT!")
        ]
    , 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.withVariables [ ("id", "INT!") ]

userModifying : Operation Mutation Named
userModifying =
  GraphQl.named "MySuperMutation"
    [ GraphQl.field "CreateUser"
      |> GraphQl.withArgument "user"
        (GraphQl.input
          [ ("first", GraphQl.string "John")
          , ("last", GraphQl.string "Doe")
          ]
        )
    ]

-- 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 Operation a b

Handle GraphQL operations

type alias Value a =
Value a

Handle GraphQL values.

type Named
type Query

Constructors

object : List (Value a) -> Operation a Anonymous

Generates a Value, from a list of fields.

object
  [ field "user" ]

Turns into:

{
  user
}
named : String -> List (Value a) -> Operation a Named

Generates a Value with a name.

named "MySuperRequest"
  [ field "user" ]

Turns into:

MySuperRequest {
  user
}
field : String -> Value a

Generates a field.

Modifiers

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

Adds 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
}
List ( String, String )
-> Operation a Named
-> Operation a Variables

Adds variables to an Operation.

"UserRequest"
  [ field "user" ]
  |> withVariables [ ("id", "id") ]

Turns into:

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

Adds 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 a) -> Value a -> Value a

Adds selectors to a Field.

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

Turns into:

user {
  id
  first_name
  last_name
}

Arguments

type Argument a

Handle arguments on GraphQL fields.

int : Int -> Argument a

Generates an argument, to use with withArgument.

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

Turns into:

user(id: 12)
bool : Bool -> Argument a

Generates an argument, to use with withArgument.

field "user"
  |> withArgument "admin" (GraphQl.bool False)

Turns into:

user(id: false)
float : Float -> Argument a

Generates an argument, to use with withArgument.

field "user"
  |> withArgument "score" (GraphQl.float 12.1)

Turns into:

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

Generates an argument, to use with withArgument.

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

Turns into:

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

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

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

Turns into:

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

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

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

Turns into:

user(id: $id)
input : List ( String, Argument Mutation ) -> Argument Mutation

Generates an argument, to use with 'withArgument'.

field "CreateUser"
  |> withArgument "user"
    (GraphQl.input
      [ ("first", (GraphQl.string "John"))
      , ("last",  (GraphQl.string "Doe"))
      ]
    )

Turns into:

CreateUser(user: {first: "John", last: "Doe"})
nestedInput : List (List ( String, Argument Mutation )) -> Argument Mutation

Generates an argument, to use with 'withArgument'.

field "CreateUser"
  |> withArgument "users"
    (GraphQl.nestedInput
      [ [ ("first", (GraphQl.string "John"))
        , ("last", (GraphQl.string "Doe"))
        ]
      , [ ("first", (GraphQl.string "Jane"))
        , ("last", (GraphQl.string "Smith"))
        ]
      ]
    )

Turns into:

CreateUsers(users: [
  {first: "John", last: "Doe"},
  {first: "Jane", last: "Smith"}
])
queryArgs : List ( String, Argument Query ) -> Argument Query

Generates a query argument, to use with 'withArgument'. Works like 'input' but for querys. field "users" |> withArgument "user" (GraphQl.queryArgs [ ("name", (GraphQl.string "John")) , ("last", (GraphQl.string "Doe")) ] ) Turns into: users(user: {name: "John", last: "Doe"})

Requests

type Request a b c

Requests contains the query or the mutation and the variables of each GraphQL requests. The variables can't be used with an anonymous Request, due to the nature of GraphQL.

query : String -> Operation Query a -> Decoder b -> Request Query a b

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

object []
  |> flip (query "https://example.com") decoder
  |> send msg
mutation : String -> Operation Mutation a -> Decoder b -> Request Mutation a b

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

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

Add variables to a request. Useful when defining variables in your GraphQL request. If no variables has been defined in your operation, no variables can be added: the compiler will reject it.

named [ field "user" |> withArgument "id" (variable "id") ]
  |> withVariables [ ("id", "INT") ]
  |> flip (query "https://example.com") decoder
  |> addVariables [ ("id", Encode.int 12) ]
  |> send msg
send : (Result Error c -> msg) -> Request a b c -> Cmd msg

Sends the GraphQL request! Generates a Cmd, to feed the runtime in your update.